CF 754D---- Fedor and coupons
2018-03-28 19:44
309 查看
题目链接
题意:
给定N个区间,选择任意K个区间,求K个区间中的公共最长区间为多少
思路:
贪心+优先队列
如果选,那么选的区间都是 L尽量小,R尽量大
这样,区间长度最长
当L确定的时候,那么尽量取R最大的
将区间的L从小到大进行排列
然后维护一个R从小到大的优先队列(队列中只有K个元素,多余的弹出)
因为每次拿一个新的区间,L是最大的,即此时L确定,那么尽量的让R最大
即将队列里R最小的取出来
运用了动态数组存储
运用了优先队列(greater是从小到大,less是从大到小)priority_queue<int, vector<int>, greater<int>> q;
代码来源: https://blog.csdn.net/max_kibble/article/details/54232012
题意:
给定N个区间,选择任意K个区间,求K个区间中的公共最长区间为多少
思路:
贪心+优先队列
如果选,那么选的区间都是 L尽量小,R尽量大
这样,区间长度最长
当L确定的时候,那么尽量取R最大的
将区间的L从小到大进行排列
然后维护一个R从小到大的优先队列(队列中只有K个元素,多余的弹出)
因为每次拿一个新的区间,L是最大的,即此时L确定,那么尽量的让R最大
即将队列里R最小的取出来
运用了动态数组存储
运用了优先队列(greater是从小到大,less是从大到小)priority_queue<int, vector<int>, greater<int>> q;
#include<cstdio> #include<vector> #include<queue> #include<algorithm> using namespace std; int n, m; int ans, L, R; struct Seg { int l, r; int idx; bool operator < (const Seg &obj) const { return l < obj.l; } }; vector<Seg> segs; priority_queue<int, vector<int>, greater<int>> q; Seg seg(int _l, int _r, int _idx) { Seg ret; ret.l = _l; ret.r = _r; ret.idx = _idx; return ret; } int main() { scanf("%d%d", &n, &m); int _l, _r; for(int i = 0; i < n; i++) { scanf("%d%d", &_l, &_r); segs.push_back(seg(_l, _r, i + 1));//动态数组存储,i+1表示第几个 } sort(segs.begin(), segs.end());//按照l大小排序 for(int i = 0; i < n; i++) { q.push(segs[i].r); if(q.size() > m) q.pop(); if(q.size() == m) { int candidate = q.top() - segs[i].l + 1; if(candidate > ans) { ans = candidate; L = segs[i].l; R = q.top(); } } } printf("%d\n", ans); if(ans == 0) { for(int i = 1; i <= m; i++) printf("%d ", i); } else { for(int i = 0; i < n && m > 0; i++) { if(segs[i].l <= L && segs[i].r >= R) { printf("%d ", segs[i].idx); m--; } } } printf("\n"); return 0; }
代码来源: https://blog.csdn.net/max_kibble/article/details/54232012
相关文章推荐
- 【codeforces 754D】Fedor and coupons
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- codeforces D. Fedor and coupons 贪心+优先队列
- D. Fedor and coupons(思路)
- Codeforces 754 D Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D - Fedor and coupons (贪心)
- D. Fedor and coupons(贪心+优先队列)
- Codeforces 754 D Fedor and coupons【优先队列】
- Codeforces 754 D Fedor and coupons
- D. Fedor and coupons Codeforces Round #390 (Div. 2)(优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
- cf 267 div.2 D Fedor and Essay
- Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
- codeforces round390 div2 C Fedor and coupons
- Codeforces Round #390 (Div. 2)D Fedor and coupons