题意:给定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;
}

最新文章

  1. Java 中的集合接口——List、Set、Map
  2. HAproxy的安装与配置讲解
  3. Android课程---远程服务器存储
  4. Make something people want
  5. jQuery的常用函数扩展
  6. centos 安装haproxy 1.6.3
  7. Python yield 使用浅析(转)
  8. windows设备驱动安装接口(自己仿写)
  9. c++学籍管理系统
  10. php连接sql server 2008数据库
  11. 【工具】idea工具 java代码 gbk转utf8
  12. vc6.0问题
  13. 【转】javaUDP套接字通信
  14. 数据库事务的隔离以及spring的事务传播机制
  15. 01++ Bookshelf 2
  16. ASP.NET Core 请求/查询/响应参数格式转换(下划线命名)
  17. 【iCore4 双核心板_ARM】例程三:EXTI中断输入实验——读取ARM按键状态
  18. 机器学习理论之SVM
  19. Hive任务优化--控制hive任务中的map数和reduce数
  20. 移动直播app怎么做

热门文章

  1. mysql 货币字段类型的存储
  2. openwrt+ndp+ndppd+radvd+dhcpv6,ipv6穿透配置指南
  3. PHP DES加密
  4. mapreduce 依赖组合
  5. Mysql limit性能优化(小offset与大offset)
  6. CentOS安装VirtualBox增强工具
  7. Android Stduio的使用(七)--Structure窗口
  8. spring,hibernate配置事务 beans.xml
  9. linux下libreoffice安装测试
  10. java的property