codeforces754d Fedor and Coupons

Source: Internet
Author: User

This article is copyright ljh2000 and Blog Park is shared, welcome reprint, but must retain this statement, and give the original link, thank you for your cooperation.

This article ljh2000
Author Blog: http://www.cnblogs.com/ljh2000-jump/
Reprint please specify the source, infringement must investigate, retain the final interpretation right!

Description

All of our characters has hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket.

the goods in the supermarket has a unique integer IDs. Also, for every an integer there is a product with the ID equal to the This integer. Fedor Has  n discount coupons, the  i -th of them can used with products with IDs ranging From  l i  to  R i , inclusive. Today Fedor wants to take Exactly  k coupons with him.

Fedor wants to choose, the k coupons in such a, the the number of the such products x , all Co Upons can used with this product x are as large as possible (for better understanding, see examples). Fedor wants to save the he time as well, the so he asks the Choose coupons for him. Help fedor!

Input

The first line contains the integers n and k (1?≤? K? ≤? n? ≤?3 105)-the number of coupons Fedor have, and the number of coupons he wants to choose.

Each of the next n lines contains II integers li and Ri (?-? ten9?≤? 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, the chosen coupons can be used. The which at least one coupon cannot is 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 is multiple answers, print any of them.

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

In the first example if we take the first and coupons then all the products with IDs in range [40,?70] can be bought With both coupons. There is to products in total.

In the second example, no product can be bought with a coupons, that's why the answer is 0. Fedor can choose any and coupons in this example.

Positive solution: Heap + greedy

Problem Solving Report:

The model outlined in this question is very concise and classic: Remove the exact k bar from the N line to make the intersection length as long as possible, and output the optimal value and scheme.

I've been thinking about the monotony for a long time, but I can't make a monotonous decision, and I can't restore the historical version. So I thought about it, and it seemed like a log would be a good thing to do?

Consider sorting by the left endpoint first, maintaining a small Gan of the right endpoint coordinates, it is easy to see that I just need to ensure that the heap size is always less than or equal to K. When I sweep to a left endpoint, the right end point is compared to the heap top, and if it is smaller than the heap top, it is not considered, otherwise, the top of the heap is deleted and the new right endpoint coordinates are added to the heap. Update the answer at a time with the top of the heap minus the left end of the currently processed segment (if and only if there are exactly k elements in the heap). Output scheme, you can do it again with the same method.

It is made by Ljh2000#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio > #include <cmath> #include <algorithm> #include <ctime> #include <vector> #include <queue > #include <map> #include <set> #include <string>using namespace std;typedef long long ll;const int MAXN = 300011;int n,k,ans,dui[maxn];struct node{int pos,id; inline bool operator < (const node &a) const{return A.P os<pos; }}tmp;priority_queue<node>q;struct seq{int L,r,id;} a[maxn];inline BOOL CMP (SEQ q,seq QQ) {return q.l<qq.l;} inline int getint () {int w=0,q=0; char C=getchar (); while ((c< ' 0 ' | |    C> ' 9 ') && c!= '-') C=getchar (); if (c== '-') Q=1,c=getchar (); while (c>= ' 0 ' &&c<= ' 9 ') w=w*10+c-' 0 ', C=getchar (); return q?-w:w;} inline void work () {n=getint (); K=getint (); for (int i=1;i<=n;i++) a[i].l=getint (), A[i].r=getint (), A[i].id=i;sort (a +1,A+N+1,CMP); ans=-1;//!!! for (int i=1;i<=n;i++) {if (! Q.empTy ()) tmp=q.top (); if (int) q.size () <k) {tmp.pos=a[i].r;tmp.id=i; Q.push (TMP);} else {if (A[i].r>tmp.pos) {q.pop (); tmp.pos=a[i].r;tmp.id=i; Q.push (TMP);}} if (int) q.size () >=k) Ans=max (Q.top (). Pos-a[i].l,ans);} printf ("%d\n", ans+1), if (Ans==-1) {for (int i=1;i<=k;i++) printf ("%d", I); return; }int Lans=ans; Ans=-1;while (! Q.empty ()) Q.pop (); for (int i=1;i<=n;i++) {if (! Q.empty ()) tmp=q.top (); if (int) q.size () <k) {tmp.pos=a[i].r;tmp.id=a[i].id;//!!! Q.push (TMP);} else {if (A[i].r>tmp.pos) {q.pop (); tmp.pos=a[i].r;tmp.id=a[i].id;//!!! Q.push (TMP);}} if (int) q.size () >=k) {Ans=max (Q.top (). Pos-a[i].l,ans); if (ans==lans) {int Cnt=0;while (! Q.empty ()) {tmp=q.top ();d ui[++cnt]=tmp.id; Q.pop ();} Sort (dui+1,dui+k+1); for (int i=1;i<=k;i++) printf ("%d", Dui[i]); return;}}}    int main () {work (); return 0;}

  

codeforces754d Fedor and Coupons

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.