CodeForces 754D Fedor and coupons (优先队列)
题意:给定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;
}
CodeForces 754D Fedor and coupons (优先队列)的更多相关文章
- 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 standar ...
- codeforces 754D. Fedor and coupons
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 754D Fedor and coupons ——(k段线段最大交集)
还记得lyf说过k=2的方法,但是推广到k是其他的话有点麻烦.现在这里采取另外一种方法. 先将所有线段按照L进行排序,然后优先队列保存R的值,然后每次用最小的R值,和当前的L来维护答案即可.同时,如果 ...
- Codeforces 390Div2-754D. Fedor and coupons(贪心+优先队列)
D. Fedor and coupons time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- cf754 754D - Fedor and coupons
2个多小时,弱智了..(连A都做不对,就不要做D了(迷)) #include<bits/stdc++.h> #define lowbit(x) x&(-x) #define LL ...
- Codeforces I. Producing Snow(优先队列)
题目描述: C. Producing Snow time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【codeforces 754D】Fedor and coupons
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- Codeforces Round #390 (Div. 2) D. Fedor and coupons(区间最大交集+优先队列)
http://codeforces.com/contest/754/problem/D 题意: 给定几组区间,找k组区间,使得它们的公共交集最大. 思路: 在k组区间中,它们的公共交集=k组区间中右端 ...
- Codeforces Round #390 (Div. 2) D. Fedor and coupons
题意:题目简化了就是要给你n个区间,然后让你选出k个区间 使得这k个区间有公共交集:问这个公共交集最大能是多少,并且输出所选的k个区间.如果有多组答案,则输出任意一种. 这题是用优先队列来处理区 ...
随机推荐
- SSH整合中为获取表单对象Action类实现的接口及拦截器配置
保存员工或者用户信息时,以员工为例,是通过表单收集信息的,需要把这些信息赋给一个对象,然后保存到数据库中.对应的Action类须实现Preparable接口及ModelDriven接口,且在Actio ...
- [转]程序开发基础学习二(C++ Google Style 命名规则)
无规矩不成方圆,新的岗位就需要服从团队的编码规则.很开心团队用的是Google的C++编码规则,大概看了下Google 的编码规则,正如九天翔雁说的:“Google的 C++ Style Guide远 ...
- java.lang.ClassNotFoundException: org.apache.lucene.store.Directory
看下你的lucene-core.jar有没有在WEB-INF\lib下.
- andorid 自定义view属性declare-styleable
1. reference:参考某一资源ID. (1)属性定义: <declare-styleable name = "名称"> <attr name = &quo ...
- ref和out,以及一般方法的引用参数和值参数寻解
对与ref和out的区别,我相信很多人都知道,这里我简单罗列下: 1.首先ref和out两种类型的参数都是可以将方法内对参数的修改传递到方法外面 2.ref参数需要在传递之前初始化,out不需要,ou ...
- iptables配置vsftp访问
一.FTP服务简介 FTP服务器有两种工作模式:主动模式和被动模式.这两种方式的特点如下: (1)主动模式下: tcp, 20(20号端口用于数据传输),21(21号端口用于控制连接) (2 ...
- 关于jdk环境变量配置成了1.6.0_39 32位jdk 的路径 cmd中java -version却还是显示 64位或者其他jdk 路径的解决方法
其实是c盘或者其他盘的 jdk 安装的太多了,把其他的都卸载掉就行了
- CodeForces 158DIce Sculptures(枚举)
一个暴力的枚举,枚举组成正多边形需要对应覆盖原先的几条边,范围为(1,n/3),然后维护最大值就可以了,注意初始化为-inf. #include<stdio.h> #include< ...
- 转:创建WebTest插件
•Web测试插件为隔离Web测试中各个主声明语句外部的代码提供了一种手段.自定义的Web测试插件为在运行Web测试时调用某些代码提供了途径.在每个测试迭代中,Web测试插件都要运行一次. •通过从We ...
- android开发中应该注意的问题
1. Activity可继承自BaseActivity,便于统一风格与处理公共事件,构建对话框统一构建器的建立,万一需要整体变动,一处修改到处有效. 2. 数据库表段字段常量和SQL逻辑分离,更清 ...