http://codeforces.com/contest/749/problem/D
现在发现做题要把样例抄下来,然后画一画,这样才容易发现新大陆。嗯,以后做题就这样。
如果排除了被删除了的人,那么,剩下的人中,胜出的,就是剩下出价最高的那个,但是它有可能不需要出价那么高,所以只需要比现在剩下的人中,出价第二高的那个人的出价最大值大就可以了。
所以每次只需要找出两个人,即可。
这题学到了,对一个数组排序,还可以依赖其他数组来排。
比如我用per[i]表示排名第i的那个人的id。如果要按它出价最高来排序,只需要在cmp函数上修改下就好。

#include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <algorithm> #include <assert.h> #define IOS ios::sync_with_stdio(false) using namespace std; #define inf (0x3f3f3f3f) typedef long long int LL; #include <iostream> #include <sstream> #include <vector> #include <set> #include <map> #include <queue> #include <string> const int maxn = 200000 + 20; int mx[maxn]; vector<int>each[maxn]; int per[maxn]; int lef[maxn]; set<int>ss; bool cmp(int a, int b) { return mx[a] < mx[b]; //学到了,可以根据这样来排序 } void work() { int n; cin >> n; for (int i = 1; i <= n; ++i) { int id, val; cin >> id >> val; per[i] = i; mx[id] = val; each[id].push_back(val); ss.insert(id); } sort(per + 1, per + 1 + n, cmp); // for (int i = 1; i <= n; ++i) { // cout << per[i] << " "; // } // cout << endl; int q; int DFN = 0; cin >> q; while (q--) { ++DFN; int gg; cin >> gg; for (int i = 1; i <= gg; ++i) { int x; cin >> x; lef[x] = DFN; } int temp[5] = {0}; int lentemp = 0; for (int i = n; i >= n - ss.size() + 1; --i) { if (lef[per[i]] == DFN) continue; temp[++lentemp] = per[i]; //存id if (lentemp == 3) break; } // cout << lentemp << endl; if (lentemp == 0) { cout << 0 << " " << 0 << endl; } else if (lentemp == 1) { cout << temp[1] << " " << each[temp[1]][0] << endl; } else { int ans = upper_bound(each[temp[1]].begin(), each[temp[1]].end(), mx[temp[2]]) - each[temp[1]].begin(); cout << temp[1] << " " << each[temp[1]][ans] << endl; } } } int main() { #ifdef local freopen("data.txt", "r", stdin); // freopen("data.txt", "w", stdout); #endif IOS; work(); return 0; }