Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons
4 seconds
256 megabytes
standard input
standard output
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.
The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him.
Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
The first line contains two integers n and k (1 ≤ k ≤ n ≤ 3·105) — the number of coupons Fedor has, and the number of coupons he wants to choose.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li ≤ ri ≤ 109) — the description of the i-th coupon. The coupons can be equal.
In the first line print single integer — the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.
In the second line print k distinct integers p1, p2, ..., pk (1 ≤ pi ≤ n) — the ids of the coupons which Fedor should choose.
If there are multiple answers, print any of them.
4 2
1 100
40 70
120 130
125 180
31
1 2
3 2
1 12
15 20
25 30
0
1 2
5 2
1 10
5 15
14 50
30 70
99 100
21
3 4
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.
In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
题意:N个区间中取K个使其交集最大。
贪心策略自己看代码吧,应该是比较清晰的。(我不会告诉你是因为我不会语言表述才没写题解的,语文太差了2333)不过贪心策略较难想到啊,想了一整天(还是太弱了!QAQ)。个人认为想到了贪心策略证明还是Too Simple的,so在此就不解释了。
本想写个随机化装个B的,没想到随机会TLE,然而只好老老实实写了,装逼不成,成傻逼了...
#include<iostream>
#include<fstream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<deque>
#include<utility>
#include<map>
#include<set>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<functional>
#include<sstream>
#include<cstring>
#include<bitset>
#include<stack>
#include<ctime>
#include<cstdlib>
using namespace std; int n,k,ans,ansl,ansr;
struct sdt
{
int lef,rig,num;
}a[300005];
priority_queue<int,vector<int>,greater<int> >q; int read()
{
int x=0;char c=getchar();
while(c<48||c>57)c=getchar();
while(c>47&&c<58)x*=10,x+=c-48,c=getchar();
return x;
} bool cmp(sdt x,sdt y)
{
return x.lef<y.lef;
} double random(double start,double end)
{
return start+(end-start)*rand()/(RAND_MAX+1.0);
} int main()
{
//srand(unsigned(time(0)));
n=read();k=read();
for(int i=1;i<=n;i++)
{
cin>>a[i].lef>>a[i].rig;
a[i].num=i;
}
sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++)
{
q.push(a[i].rig);
if(i<k)continue;
else if(q.size()>k)q.pop();
int len=q.top()-a[i].lef+1;
if(len>ans)
{
ans=len;
ansl=a[i].lef;
ansr=q.top();
}
} if(ans==0)
{
printf("%d\n",0);
//bool vis[300005]={0};
while(1)
{
//int p=int(random(1,n+1));
//if(vis[p]==1)continue;
//vis[p]=1;
printf("%d ",k);
k--;
if(k==0)return 0;
}
}
else
{
printf("%d\n",ans);
for(int i=1;i<=n;i++)
{
if(a[i].lef<=ansl && a[i].rig>=ansr)
{
printf("%d ",a[i].num);
k--;
if(k==0)return 0;
}
}
}
return 23333333;
}
Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)的更多相关文章
- CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 754D Fedor and coupons (优先队列)
题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...
- CodeForces 754D Fedor and coupons ——(k段线段最大交集)
还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...
- cf754 754D - Fedor and coupons
2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...
- HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解
思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...
- C. Playlist Educational Codeforces Round 62 (Rated for Div. 2) 贪心+优先队列
C. Playlist time limit per test 2 seconds memory limit per test 256 megabytes input standard input o ...
- hihoCoder 1309:任务分配 贪心 优先队列
#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, ...
- UVA 11134 - Fabled Rooks(贪心+优先队列)
We would like to place n rooks, 1 ≤ n ≤ 5000, on a n×n board subject to the following restrict ...
随机推荐
- 前阿里CEO卫哲谈阿里创业经验:如何找人、找钱、找方向?(不同的阶段分别有:时间优先、金额优先、比例优先,不要做平台,太难)
新浪科技李根 整理报道 卫哲现在是御嘉基金的创始合伙人,他另一个更加知名的身份是阿里巴巴(B2B)前CEO,在2006年到2011年的时间里,卫哲见证了阿里巴巴如何利用人才.资本和方向选择一路壮大. ...
- 【HDOJ】4328 Cut the cake
将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. /* 4328 */ #include <iostream> #include <sstream&g ...
- Factorial
Factorial 计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...
- poj 2676 Sudoku ( dfs )
dfs 用的还是不行啊,做题还是得看别人的博客!!! 题目:http://poj.org/problem?id=2676 题意:把一个9行9列的网格,再细分为9个3*3的子网格,要求每行.每列.每个子 ...
- 函数buf_LRU_get_free_only
/******************************************************************//** Returns a free block from th ...
- bzoj1821: [JSOI2010]Group 部落划分 Group
kruskal算法. #include<cstdio> #include<algorithm> #include<cstring> #include<cmat ...
- [原]Unity3D深入浅出 - 雾效(Fog)
在Unity中开启雾效的方式:依次选中菜单栏中的 Edit - Render Settings 项,勾选Fog 选框即可开启雾效.雾效的参数如下: Fog Color:雾的颜色. Fog Mode:雾 ...
- BZOJ_1022_[SHOI2008]_小约翰的游戏John_(博弈论_反Nim游戏)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1022 反Nim游戏裸题.详见论文<组合游戏略述——浅谈SG游戏的若干拓展及变形>. ...
- [转] web.xml文件详解
转自:http://www.cnblogs.com/hellojava/archive/2012/12/28/2835730.html 前言:一般的web工程中都会用到web.xml,web.xml主 ...
- textview的上下滑动效果
1.xml文件中 <TextView … android:scrollbars="vertical" ../> 2.java文件中 textview.se ...