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
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.

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<string.h>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<cmath>
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const double PI=acos(-1.0);
17 const double eps=0.0000000001;
18 const int N=500000+100;
19 const int INF=0x3f3f3f3f;
20 struct node{
21     int l,r;
22     int val;
23 }a[N];
24 bool cmp(node aa,node bb){
25     if(aa.l==bb.l)return aa.r<bb.r;
26     return aa.l<bb.l;
27 }
28 int main(){
29     int n,m;
30     while(scanf("%d%d",&n,&m)!=EOF){
31         for(int i=0;i<n;i++){
32             scanf("%d%d",&a[i].l,&a[i].r);
33             a[i].val=i+1;
34         }
35         sort(a,a+n,cmp);
36         priority_queue<int,vector<int>,greater<int> >q;
37         while(q.size())q.pop();
38         int maxx=-INF;
39         int ans=0;
40         int l,r;
41         for(int i=0;i<n;i++){
42             q.push(a[i].r);
43             if(q.size()>m)q.pop();
44             if(q.size()==m){
45                 maxx=q.top()-a[i].l+1;
46                 //cout<<maxx<<endl;
47             }
48             if(ans<maxx){
49                 ans=maxx;
50                 l=a[i].l;
51                 r=q.top();
52             }
53         }
54         cout<<ans<<endl;
55        // cout<<l<<" "<<r<<endl;
56         //continue;
57         if(ans==0){
58             cout<<1;
59             for(int i=1;i<m;i++)
60                 cout<<" "<<i+1;
61         }
62         else{
63             for(int i=0;i<n&&m;i++){
64                 if(a[i].r>=r&&a[i].l<=l){cout<<a[i].val<<" ";
65                 m--;
66                 }
67             }
68         }
69         cout<<endl;
70     }
71 }

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.

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

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #include<cstdlib>
 6 #include<string.h>
 7 #include<set>
 8 #include<vector>
 9 #include<queue>
10 #include<stack>
11 #include<map>
12 #include<cmath>
13 typedef long long ll;
14 typedef unsigned long long LL;
15 using namespace std;
16 const double PI=acos(-1.0);
17 const double eps=0.0000000001;
18 const int INF=0x3f3f3f3f;
19 const int N=200000+100;
20 struct node{
21     int l,r;
22     int time;
23     int val;
24     friend bool operator<(node aa,node bb){
25         return aa.r>bb.r;
26     }
27 }a[N];
28 bool cmp(node aa,node bb){
29     if(aa.l==bb.l)return aa.r<bb.r;
30     return aa.l<bb.l;
31 }
32 int vis[N];
33 int main(){
34     int n,x;
35     while(scanf("%d%d",&n,&x)!=EOF){
36         for(int i=1;i<=n;i++){
37             scanf("%d%d%d",&a[i].l,&a[i].r,&a[i].val);
38             a[i].time=a[i].r-a[i].l+1;
39         }
40         sort(a+1,a+n+1,cmp);
41         memset(vis,-1,sizeof(vis));
42         int ans=2e9+100;
43        // for(int i=1;i<=n;i++)cout<<a[i].l<<" "<<a[i].r<<endl;
44         priority_queue<node>q;
45         for(int i=1;i<=n;i++){
46             if(a[i].time>x)continue;
47             while(!q.empty()){
48                 node tt=q.top();
49                 if(tt.r>=a[i].l)break;
50                 if(vis[tt.time]==-1)vis[tt.time]=tt.val;
51                 vis[tt.time]=min(vis[tt.time],tt.val);
52                 q.pop();
53 
54             }
55             q.push(a[i]);
56             if(vis[x-a[i].time]==-1)continue;
57             ans=min(ans,a[i].val+vis[x-a[i].time]);
58         }
59         if(ans==2e9+100)cout<<-1<<endl;
60         else
61         cout<<ans<<endl;
62     }
63 }
原文地址:https://www.cnblogs.com/Aa1039510121/p/7188620.html