Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
来源:互联网 发布:舆情指数算法 编辑:程序博客网 时间:2024/05/23 00:01
记录一个菜逼的成长。。
题目链接
题目大意:
给你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;}
0 0
- Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
- Codeforces Round #390 (Div. 2)DFedor 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 Round #390 (Div. 2)(优先队列)
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
- Codeforces Round #390 (Div. 2//754D. Fedor and coupons
- Codeforces Round #390 (Div. 2) D - Fedor and coupons (贪心)
- Codeforces Round #390 (Div. 2)D Fedor and coupons
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts
- Codeforces Round #376 (Div. 2) B. Coupons and Discounts
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) A. Bear and Elections(优先队列)
- codeforces 754D Fedor and coupons(优先队列)
- codeforces 754D Fedor and coupons (优先队列)
- Codeforces 754 D Fedor and coupons【优先队列】
- codeforces D. Fedor and coupons 贪心+优先队列
- 【Codeforces Round #376 (Div. 2)】 Codeforces 731B Coupons and Discounts
- 音视频格式大全:stream type
- C++外观模式
- 16-Oracle入门之数据库事务
- Android——NDK开发(一)
- linux磁盘浅析
- Codeforces Round #390 (Div. 2)-DFedor and coupons(优先队列)
- 自定义控件一
- Reactor模式详解
- oracle学习之:DG中物理备库、快照备库的相互转换
- 练习
- javaWEB总结(14):请求的转发和重定向
- tolua 学习笔记 UI监听事件
- 关于交换机trunk的疑问 : 引用别人的
- 漫反射