cf-754 D Fedor and coupons(优先队列+区间贪心)
作者:互联网
https://codeforces.com/problemset/problem/754/D
题意:
给定n组区间,要求选出k组区间保证其交集最大,并输出选出的区间
思路:
区间贪心,优先队列算一下天数即可。
代码:
#include<iostream> #include<stdio.h> #include<stdlib.h> #include<string> #include<iomanip> #include<algorithm> #include<string.h> #include<queue> #include<cmath> #include<stack> using namespace std; const int maxn=3e5+10; const int inf=0x7f7f7f7f; typedef long long ll; int n,k; struct node { int l,r; int idx; bool operator<(const node &n)const{ return l<n.l; } }a[maxn]; priority_queue<int, vector<int>, greater<int>> qq; int st,ed; int main() { ios::sync_with_stdio(false); cin>>n>>k; for(int i=0; i<n; i++) { cin>>a[i].l>>a[i].r; a[i].idx=i+1; } sort(a,a+n); for(int i=0; i<n; i++) { if(qq.size()==k-1){ int len=a[i].r-a[i].l+1; if(!qq.empty()) len=min(len,qq.top()-a[i].l+1); if(len>ed-st){ st=a[i].l; ed=a[i].l+len; } } qq.push(a[i].r); while(qq.size()>=k) qq.pop(); } cout<<ed-st<<endl; int res=0; for(int i=0; i<n; i++) { if(res==k) break; if((a[i].l<=st&&a[i].r+1>=ed)||st==ed){ cout<<a[i].idx<<" "; res++; } } cout<<endl; //system("pause"); return 0; }
标签:qq,754,int,ed,cf,st,Fedor,区间,include 来源: https://www.cnblogs.com/sweetlittlebaby/p/14355705.html