题意:给定n个优惠券,每张都有一定的优惠区间,然后要选k张,保证k张共同的优惠区间最大。
析:先把所有的优惠券按左端点排序,然后维护一个容量为k的优先队列,每次更新优先队列中的最小值,和当前的右端点,
之间的距离。优先队列只要存储右端点就好。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <unordered_map> #include <unordered_set> #define debug() puts("++++"); #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const LL LNF = 1LL << 60; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 3e5 + 5; const int mod = 2000; const int dr[] = {-1, 1, 0, 0}; const int dc[] = {0, 0, 1, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } struct Node{ int l, r, id; bool operator < (const Node &p) const{ return l < p.l; } }; Node a[maxn]; int main(){ while(scanf("%d %d", &n, &m) == 2){ for(int i = 0; i < n; ++i){ scanf("%d %d", &a[i].l, &a[i].r); a[i].id = i + 1; } sort(a, a + n); priority_queue<int, vector<int>, greater<int> >pq; int ans = 0; int t = 0; for(int i = 0; i < n; ++i){ pq.push(a[i].r); if(pq.size() > m) pq.pop(); int tmp = pq.top() - a[i].l + 1; if(pq.size() == m && ans < tmp){ ans = tmp; t = a[i].l; } } printf("%d\n", ans); if(!ans){ printf("1"); for(int i = 2; i <= m; ++i) printf(" %d", i); continue; } else{ int cnt = 0; for(int i = 0; i < n && m; ++i) if(a[i].l <= t && a[i].r >= t + ans - 1){ if(cnt) putchar(' '); printf("%d", a[i].id); --m; ++cnt; } } printf("\n"); } return 0; }