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;
}
}

最新文章

  1. 国内外三个不同领域巨头分享的Redis实战经验及使用场景
  2. lvs简单配置
  3. iOS开发--动画(Animation)总结
  4. HTTP常见错误 400/401/403/404/500及更多
  5. [unity菜鸟] controller.SimpleMove(transform .forward); 无法移动
  6. WordPress Events Manager插件多个跨站脚本漏洞
  7. 40个DBA日常维护的SQL脚本--1113
  8. 细说OC中的load和initialize方法
  9. An internal error occurred during: &quot;Launching New_configuration&quot;
  10. linux命令读取文件中特定行
  11. Struts存取数据
  12. php-fpm重启操作
  13. 怎样用git上传代码到github以及如何更新代码
  14. Linux shell基础知识(上)
  15. php输出年份
  16. SSM框架的搭建与测试
  17. scanf_s,scanf安全版本
  18. JavaScript引用类型和值类型
  19. C#学习笔记-中英文切换(XML)
  20. Servlet------&gt;jsp输出JavaBean

热门文章

  1. Ajax——异步基础知识(二)
  2. HDU_1087_Super Jumping! Jumping! Jumping!_dp
  3. mybatis 简单的入门实例
  4. 洛谷——P3906 Geodetic集合
  5. socket 网络编程笔记 一
  6. 【模板】Tarjan缩点
  7. 《hello-world》第八次团队作业:Alpha冲刺-Scrum Meeting 2
  8. Huawei-R&amp;S-网络工程师实验笔记20190530-FTP上传下载、STelnet登录、SFTP登录
  9. 【[Offer收割]编程练习赛12 C】矩形分割
  10. bx值