Codeforces 754 D Fedor and coupons【优先队列】
2017-03-02 11:53
369 查看
题目:http://codeforces.com/contest/754/problem/D
题意:
从n条线段中取出恰好k条使得交集长度尽可能长,输出最优值和方案。分析:
感觉这种题做过好多次了,贪心+优先队列就可做。根据左端点排序,然后依次插入右端点,直到队列中元素有k个,然后当前插入的左端点是最大的,队首的就是右端点最小的。这样维护一下即可。
set和优先队列都可以,set似乎更方便些~~
代码:
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; struct Coupon { int l; int r; int id; Coupon(int l, int r, int id): l(l), r(r), id(id) {} bool operator<(const Coupon& c) { return l < c.l; } }; int main() { ios_base::sync_with_stdio(0); cin.tie(nullptr); int n, k; cin >> n >> k; vector<Coupon> e; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; e.push_back(Coupon(l, r, i)); } sort(e.begin(), e.end()); set<pii> q; int ans = 0; int when = -1; for (int i = 0; i < e.size(); i++) { q.insert(pii(e[i].r, e[i].id)); while (q.size() > k) q.erase(q.begin()); if (q.size() == k) { int cur = q.begin()->first - e[i].l + 1; if (cur > ans) { ans = cur; when = i; } } } cout << ans << endl; if (ans == 0) { for (int i = 1; i <= k; i++) cout << i << " "; cout << endl; return 0; } q.clear(); for (int i = 0; i < e.size(); i++) { q.insert(pii(e[i].r, e[i].id)); while (q.size() > k) q.erase(q.begin()); if (when == i) { for (auto kv : q) cout << kv.second + 1 << " "; cout << endl; return 0; } } }
相关文章推荐
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- Codeforces 754 D Fedor and coupons
- Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
- Codeforces 754 D Fedor and coupons
- #Codeforces 381 [div2] D. Alyona and a tree 【树链剖分 + 优先队列】
- 【codeforces 754D】Fedor and coupons
- Codeforces 426C - Sereja and Swaps 【优先队列||DP】
- #Codeforces 343 [div2] D. Babaei and Birthday Cake 【线段树优先队列查询优化】
- hdoj 1026 Ignatius and the Princess I 【BFS + 优先队列 + stack路径记录】 【经典题目】
- Codeforces 467D - Fedor and Essay (反向建图 + DFS)
- CodeForces 731B-Coupons and Discounts(贪心 模拟)
- hdu 1026 Ignatius and the Princess I 优先队列 + bfs
- hdu 1026 Ignatius and the Princess I bfs 优先队列 路径记录
- codeforces 15D . Map 优先队列
- codeforces 835B The number on the board (优先队列,贪心)
- hdu1026 Ignatius and the Princess I (优先队列 BFS)
- Codeforces Round #243 (Div. 2) C. Sereja and Swaps(优先队列 暴力)
- HDU1026 Ignatius and the Princess I 优先队列
- hdu 1026 Ignatius and the Princess I (bfs记录路径 两种记录路径方法模拟优先队列和优先队列两种做法)
- Codeforces 845 C Two TVs(贪心之优先队列维护)