D. Fedor and coupons
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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!

Input

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.

Output

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.

Examples
Input
4 2
1 100
40 70
120 130
125 180
Output
31
1 2
Input
3 2
1 12
15 20
25 30
Output
0
1 2
Input
5 2
1 10
5 15
14 50
30 70
99 100
Output
21
3 4
Note

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.

按左区间从小到大排序一下 用优先队列维护一下右区间

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int N=+;
const int INF=0x3f3f3f3f;
struct node{
int l,r;
int val;
}a[N];
bool cmp(node aa,node bb){
if(aa.l==bb.l)return aa.r<bb.r;
return aa.l<bb.l;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<n;i++){
scanf("%d%d",&a[i].l,&a[i].r);
a[i].val=i+;
}
sort(a,a+n,cmp);
priority_queue<int,vector<int>,greater<int> >q;
while(q.size())q.pop();
int maxx=-INF;
int ans=;
int l,r;
for(int i=;i<n;i++){
q.push(a[i].r);
if(q.size()>m)q.pop();
if(q.size()==m){
maxx=q.top()-a[i].l+;
//cout<<maxx<<endl;
}
if(ans<maxx){
ans=maxx;
l=a[i].l;
r=q.top();
}
}
cout<<ans<<endl;
// cout<<l<<" "<<r<<endl;
//continue;
if(ans==){
cout<<;
for(int i=;i<m;i++)
cout<<" "<<i+;
}
else{
for(int i=;i<n&&m;i++){
if(a[i].r>=r&&a[i].l<=l){cout<<a[i].val<<" ";
m--;
}
}
}
cout<<endl;
}
}
C. Hacker, pack your bags!
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

It's well known that the best way to distract from something is to do one's favourite thing. Job is such a thing for Leha.

So the hacker began to work hard in order to get rid of boredom. It means that Leha began to hack computers all over the world. For such zeal boss gave the hacker a vacation of exactly x days. You know the majority of people prefer to go somewhere for a vacation, so Leha immediately went to the travel agency. There he found out that n vouchers left. i-th voucher is characterized by three integers li, ri, costi — day of departure from Vičkopolis, day of arriving back in Vičkopolis and cost of the voucher correspondingly. The duration of the i-th voucher is a value ri - li + 1.

At the same time Leha wants to split his own vocation into two parts. Besides he wants to spend as little money as possible. Formally Leha wants to choose exactly two vouchers i and j (i ≠ j) so that they don't intersect, sum of their durations is exactly x and their total cost is as minimal as possible. Two vouchers i and j don't intersect if only at least one of the following conditions is fulfilled: ri < lj or rj < li.

Help Leha to choose the necessary vouchers!

Input

The first line contains two integers n and x (2 ≤ n, x ≤ 2·105) — the number of vouchers in the travel agency and the duration of Leha's vacation correspondingly.

Each of the next n lines contains three integers li, ri and costi (1 ≤ li ≤ ri ≤ 2·105, 1 ≤ costi ≤ 109) — description of the voucher.

Output

Print a single integer — a minimal amount of money that Leha will spend, or print  - 1 if it's impossible to choose two disjoint vouchers with the total duration exactly x.

Examples
Input
4 5
1 3 4
1 2 5
5 6 1
1 2 4
Output
5
Input
3 2
4 6 3
2 4 1
3 5 4
Output
-1
Note

In the first sample Leha should choose first and third vouchers. Hereupon the total duration will be equal to (3 - 1 + 1) + (6 - 5 + 1) = 5 and the total cost will be 4 + 1 = 5.

In the second sample the duration of each voucher is 3 therefore it's impossible to choose two vouchers with the total duration equal to 2.

感觉是套路  也是搞个队列维护下

 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<string.h>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<map>
#include<cmath>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const double PI=acos(-1.0);
const double eps=0.0000000001;
const int INF=0x3f3f3f3f;
const int N=+;
struct node{
int l,r;
int time;
int val;
friend bool operator<(node aa,node bb){
return aa.r>bb.r;
}
}a[N];
bool cmp(node aa,node bb){
if(aa.l==bb.l)return aa.r<bb.r;
return aa.l<bb.l;
}
int vis[N];
int main(){
int n,x;
while(scanf("%d%d",&n,&x)!=EOF){
for(int i=;i<=n;i++){
scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
a[i].time=a[i].r-a[i].l+;
}
sort(a+,a+n+,cmp);
memset(vis,-,sizeof(vis));
int ans=2e9+;
// for(int i=1;i<=n;i++)cout<<a[i].l<<" "<<a[i].r<<endl;
priority_queue<node>q;
for(int i=;i<=n;i++){
if(a[i].time>x)continue;
while(!q.empty()){
node tt=q.top();
if(tt.r>=a[i].l)break;
if(vis[tt.time]==-)vis[tt.time]=tt.val;
vis[tt.time]=min(vis[tt.time],tt.val);
q.pop(); }
q.push(a[i]);
if(vis[x-a[i].time]==-)continue;
ans=min(ans,a[i].val+vis[x-a[i].time]);
}
if(ans==2e9+)cout<<-<<endl;
else
cout<<ans<<endl;
}
}

CodeForces 754D Fedor and coupons&&CodeForces 822C Hacker, pack your bags!的更多相关文章

  1. Codefroces 822C Hacker, pack your bags!

    C. Hacker, pack your bags! time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  2. codeforces 754D. Fedor and coupons

    D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. CodeForces 754D Fedor and coupons (优先队列)

    题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大. 析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点, ...

  4. CodeForces 754D Fedor and coupons ——(k段线段最大交集)

    还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...

  5. Codeforces 822C Hacker, pack your bags! - 贪心

    It's well known that the best way to distract from something is to do one's favourite thing. Job is ...

  6. Codeforces 822C Hacker, pack your bags!(思维)

    题目大意:给你n个旅券,上面有开始时间l,结束时间r,和花费cost,要求选择两张时间不相交的旅券时间长度相加为x,且要求花费最少. 解题思路:看了大佬的才会写!其实和之前Codeforces 776 ...

  7. CodeForces 822C Hacker, pack your bags!

    题意 给出一些闭区间(始末+代价),选取两段不重合区间使长度之和恰为x且代价最低 思路 相同持续时间的放在一个vector中,内部再对起始时间排序,从后向前扫获取对应起始时间的最优代价,存在minn中 ...

  8. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(二分写法)

    [题目链接]:http://codeforces.com/contest/822/problem/C [题意] 有n个旅行计划, 每个旅行计划以开始日期li,结束日期ri,以及花费金钱costi描述; ...

  9. 【Codeforces Round #422 (Div. 2) C】Hacker, pack your bags!(hash写法)

    接上一篇文章; 这里直接把左端点和右端点映射到vector数组上; 映射一个open和close数组; 枚举1..2e5 如果open[i]内有安排; 则用那个安排和dp数组来更新答案; 更新答案完之 ...

随机推荐

  1. [Windows Server 2008] Apache+PHP安全设置

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:Win2008 ...

  2. #1003 Max Sum

    http://acm.hdu.edu.cn/showproblem.php?pid=1003 给你一串数列,让你求出其中 一段连续的子数列 并且 该子数列所有数字之和最大,输出该子数列的和.起点与终点 ...

  3. C# Winform 获得下拉框 选中的值

    string PrintName = cmbPrinter.SelectedIndex.ToString(); PrintName = cmbPrinter.SelectedItem.ToString ...

  4. c#符号含义

    属性:(带手型图标)方法:(紫红色菱形)事件:(闪电)字段:(蓝色菱形) 还有很多,具体图标不好描述命名空间,类,接口,值类,枚举,清单或类信息项等

  5. 写一个 sum方法,在使用下面任一语法调用时,都可以正常工作

    console.log(sum(2,3)); // Outputs 5 console.log(sum(2)(3)); // Outputs 5 (至少)有两种方法可以做到: 方法1: functio ...

  6. ThinkPHP框架表单验证AJAX

    验证有两种方式:静态验证与动态验证. 一.静态验证 在模型类里面预先定义好该模型的自动验证规则,我们称为静态定义. 验证时要在test表的Model里面加验证条件:新建testModel.class. ...

  7. js 简单模板引擎

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  8. Web 常用

    System.Web.Hosting.HostingEnvironment.MapPath(); HttpUtility.UrlEncode();

  9. python 单元测试中处理用例失败的情况

    今天有一个需求, 在单元测试失败的时候打印一些日志, 我们管他叫 dosomething 吧 ,反正就是做一些操作 查了下并没有查到相关的方法, 于是研究了一波unittest 的源码 发现了这个东西 ...

  10. oracle的递归运算(树运算) 无限树形

    oracle的递归运算(树运算)start with org_id ='1'connect by prior parent_id=son_id 1.前言   oracle的递归运算,在我们web页面的 ...