携程第三题 get+put

#include<iostream>
#include<set>
#include<string>
#include<fstream>
#include<sstream>
#include<vector>
using namespace std;
struct dd {
    int x, y;
    mutable int tc;
    dd(int x1, int y1) {
        x = x1;
        y = y1;
        tc = 0;
    }
    bool gg(int xx)const {
        if (x != xx)
            return false;
        tc++;
        return true;
    }
    bool operator<(const dd&ss)const {
        return x < ss.x;
    }
};
int main(void) {
    set<dd>storage;
    int num;
    cin >> num;  //总大小
    cin.get();
    string ss;
    stringstream out;
    vector<int>sp;
    while (1) {
        getline(cin, ss);
        if (cin.eof())
            break;
        out.clear();
        out.str(ss);
        char cc;
        int x, y;
        out >> cc;
        if (cc == 'p') {
            out >> x >> y;
            if (storage.size() < num)
                storage.insert(dd(x,y));
            else {
                auto it = storage.begin();
                auto it1 = it;
                int min = it->tc;
                for (;it != storage.end();it++) {
                    if (it->tc < min) {
                        min = it->tc;
                        it1 = it;
                    }
                }
                storage.erase(it1);
                storage.insert(dd(x, y));
            }
        }
        else if (cc == 'g') {
            out >> x;
            bool itt = false;
            for (auto it = storage.begin();it != storage.end();it++) {
                if (it->gg(x))
                {
                    itt = true;
                    sp.push_back(it->y);
                    break;
                }
            }
            if (!itt) {
                sp.push_back(-1);
            }
            itt = false;
        }
        else
            break;
        ss.clear();
    }
    for (int i = 0;i < sp.size();i++)
        cout << sp[i] << "\n";
    while (1);
}

#携程#
全部评论
a了吗
点赞 回复 分享
发布于 2018-09-04 20:53
学习学习
点赞 回复 分享
发布于 2018-09-04 20:56

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务