Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
2017-01-08 13:30
423 查看
记录一个菜逼的成长。。
题目链接
题目大意:
给你n个区间,问k个区间相交的最大区间。
PS:这类题型貌似挺经典。用优先队列去维护很方便。
把区间按左端点从小到大排序。
创建一个优先队列,默认是大顶堆,但我们要维护区间右端点的最小值
所以只需把区间右端点取负数即可。
判断区间相交大小只需用到队头与当前的区间。
题目链接
题目大意:
给你n个区间,问k个区间相交的最大区间。
PS:这类题型貌似挺经典。用优先队列去维护很方便。
把区间按左端点从小到大排序。
创建一个优先队列,默认是大顶堆,但我们要维护区间右端点的最小值
所以只需把区间右端点取负数即可。
判断区间相交大小只需用到队头与当前的区间。
#include <bits/stdc++.h> using namespace std; #define cl(a,b) memset(a,b,sizeof(a)) #define pb push_back #define mp make_pair #define lowbit(x) (x)&(-x) typedef long long LL; typedef pair<int,int> PII; const int INF = 0x3f3f3f3f; const int maxn = 300000 + 10; struct Seg{ int x,y,id; }a[maxn]; bool cmp(Seg a,Seg b) { return a.x < b.x; } priority_queue<int>q; int main() { int n,k; while(~scanf("%d%d",&n,&k)){ for( int i = 0; i < n; i++ ){ scanf("%d%d",&a[i].x,&a[i].y); a[i].id = i + 1; } int ans = 0,l = 0,r = 0; sort(a,a+n,cmp); for( int i = 0; i < n; i++ ){ if(q.size() < k)q.push(-a[i].y); else if(-q.top() < a[i].y)q.pop(),q.push(-a[i].y); //其实下面这个判断严格上来说是当前区间放入队列里才成立 //但是之前已经排序了,所以即使没有放入队列,右端点不变,左端点不递减, //一定不会满足判断里的第一个条件,不会影响 if(ans < -q.top() - a[i].x + 1 && q.size() == k){ ans = -q.top() - a[i].x + 1; l = a[i].x; r = -q.top(); } } printf("%d\n",ans); if(ans == 0){ for( int i = 0; i < k; i++ ){ printf("%d%c",i+1,i != k-1 ? ' ': '\n'); } continue; } int flag = 1; for( int i = 0; i < n && k; i++ ){ if(a[i].x <= l && a[i].y >= r){ if(flag)printf("%d",a[i].id),flag = 0; else printf(" %d",a[i].id); k--; } } puts(""); } return 0; }
相关文章推荐
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
- Codeforces Round #390 (Div. 2)DFedor and coupons (排序+优先队列)
- D. Fedor and coupons Codeforces Round #390 (Div. 2)(优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons 贪心+优先队列
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- codeforces 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) D. Fedor and coupons
- codeforces round390 div2 C Fedor and coupons
- Codeforces Round #390(Div. 2)D. Fedor and coupons【优先队列】
- D. Fedor and coupons(贪心+优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #267 (Div. 2) D. Fedor and Essay
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts
- Codeforces Round #267 (Div. 2) D. Fedor and Essay
- Fedor and coupons CodeForces - 754D (优先队列,贪心)
- 【codeforces 754D】Fedor and coupons
- Codeforces 754 D Fedor and coupons
- CF 754D---- Fedor and coupons