Codeforces Round #388 (Div. 2)D Leaving Auction

題目大意:

       有很多人在競價,有n次出價,由出價人編號和出價金額組成。現在有k次詢問,問每次如果這些人不競價,那麼最後會是誰獲勝,競價金額是多少。輸出保證了不會有同一個人兩次出價,每次出價的金額也是嚴格遞增的。

題目解法:

      先用向量保存每個人的出價記錄。然後將每個人最後一次的出家情況放入set,每次刪除去掉的人,觀察如果set爲空,就輸出0 0;如果只剩一個人,那就是那個人並且出價金額爲他第一次的出價金額;如果超過這麼一個人,那麼獲勝者應該是最後一人,但是他的出價金額一定是比倒數第二人的出價金額要高,這裏就是用下lowerbound就好了。最後每一次詢問後記得恢復set裏面的內容。本質其實就是一道利用STL的模擬題。

代碼:

#include "iostream"
#include "cstdio"
#include "math.h"
#include "algorithm"
#include "string"
#include "string.h"
#include "vector"
#include "map"
#include "queue"
#include "bitset"
#include "set"
using namespace std;
struct Node {
	int id, price;
	friend bool operator<(Node a, Node b) {
		return a.price < b.price;
	}
	Node(){}
	Node(int a, int b) {
		id = a;
		price = b;
	}
};
vector<int>vec[200005];
set<Node>s;
int x[200005];
int main() {
	int n, m, k;
	scanf("%d", &n);
	for (int i = 1;i <= n;i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		vec[a].push_back(b);
	}
	for (int i = 1;i <= n;i++) {
		if (vec[i].size()) {
			s.insert(Node(i, vec[i][vec[i].size() - 1]));
		}
	}
	scanf("%d", &k);
	while (k--) {
		scanf("%d", &m);
		for (int i = 1;i <= m;i++) {
			scanf("%d", &x[i]);
			if (vec[x[i]].size()) {
				s.erase(Node(x[i], vec[x[i]][vec[x[i]].size() - 1]));
			}
		}
		set<Node>::iterator it;
		if (s.size() == 0) {
			puts("0 0");
		}
		else if (s.size() == 1) {
			it = s.begin();
			printf("%d %d\n", it->id, vec[it->id][0]);
		}
		else {
			it = s.end();
			it--;
			int a = it->id;
			it--;
			int b = it->price;
			int ans = *lower_bound(vec[a].begin(), vec[a].end(), b);
			printf("%d %d\n", a, ans);
		}

		for (int i = 1;i <= m;i++) {
			if (vec[x[i]].size()) {
				s.insert(Node(x[i], vec[x[i]][vec[x[i]].size() - 1]));
			}
		}
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章