CodeForces754D【贪心】
题意:
有n个区间,每个区间覆盖区间里一段数,求最大连续区间长度被覆盖k次,并输出选取的区间。
思路:
贪心;
感觉一开始肯定是要把区间按left从小到大排序的。
然后肯定是连续k个区间能够达到的重叠最长?
因为left已经足够小了?
3 2
1 100
30 70
31 78
这个案例就说明这样贪心还不是准确的。
其实不管k多大,只有一个left,一个right,left大,right小这样是最差的,也是最长的最小。
现在就是进来的时候,left就是很大啊!
然后只要考虑right是不是又特别小。ok!!!!就是这样啊!!
用优先队列维护right,队列头的right最小就好了
//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<queue>
#include<math.h>
#include<string.h>
#include<algorithm>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const int N=3e5+10;
struct asd{
int left,right,id;
friend bool operator< (asd n1,asd n2)
{
return n1.right>n2.right;
}
};
bool cmp(asd n1,asd n2)
{
if(n1.left==n2.left) return n1.right<n2.right;
return n1.left<n2.left;
}
asd now[N];
priority_queue<asd>q;
int main()
{
int n,k,s,t;
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d%d",&s,&t);
now[i].left=s;
now[i].right=t;
now[i].id=i+1;
}
int ans,rightmin,leftmax;
sort(now,now+n,cmp);
ans=0;
for(int i=0;i<n;i++)
{
q.push(now[i]);
if(q.size()>k)
q.pop();
if(q.size()==k)
{
leftmax=now[i].left;
if((q.top().right-leftmax+1)>ans)
{
ans=q.top().right-leftmax+1;
rightmin=q.top().right;
}
}
}
printf("%d\n",ans);
if(ans==0)
for(int i=1;i<=k;i++)
printf("%d ",i);
else
for(int i=0;i<n;i++)
if(k&&now[i].left<=leftmax && now[i].right>=rightmin){
printf("%d ",now[i].id);
k--;
}
return 0;
}
CodeForces754D【贪心】的更多相关文章
- BZOJ 1692: [Usaco2007 Dec]队列变换 [后缀数组 贪心]
1692: [Usaco2007 Dec]队列变换 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1383 Solved: 582[Submit][St ...
- HDOJ 1051. Wooden Sticks 贪心 结构体排序
Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序
FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- BZOJ 1691: [Usaco2007 Dec]挑剔的美食家 [treap 贪心]
1691: [Usaco2007 Dec]挑剔的美食家 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 786 Solved: 391[Submit][S ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【BZOJ-4245】OR-XOR 按位贪心
4245: [ONTAK2015]OR-XOR Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 486 Solved: 266[Submit][Sta ...
- codeforces754D Fedor and coupons
本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...
- code vs 1098 均分纸牌(贪心)
1098 均分纸牌 2002年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 有 N 堆纸牌 ...
- 【BZOJ1623】 [Usaco2008 Open]Cow Cars 奶牛飞车 贪心
SB贪心,一开始还想着用二分,看了眼黄学长的blog,发现自己SB了... 最小道路=已选取的奶牛/道路总数. #include <iostream> #include <cstdi ...
随机推荐
- Android 四大组件学习之BroadcastReceiver一
本节课学习四大组件最后一个, 广播接受者. 顾名思义广播接受者就是接受广播呗.比方在现实社会中,曾经每一个人家都有一台收音机,这可就能够去接受广播发出来的消息.大家都知道.程序世界也是參照的显示生活设 ...
- iOS8 PUSH解决方法
本文转载至 http://blog.csdn.net/pjk1129/article/details/39548523 - (void)registerForRemoteNotificatio ...
- Package template (html/template) ... Types HTML, JS, URL, and others from content.go can carry safe content that is exempted from escaping. ... (*Template) Funcs ..
https://godoc.org/text/template GoDoc Home About Go: text/templateIndex | Examples | Files | Directo ...
- csslint
http://csslint.net/ line column title description browserwarning 1 1 Disallow @import @import preven ...
- SVD分解的理解
对称阵A 相应的,其对应的映射也分解为三个映射.现在假设有x向量,用A将其变换到A的列空间中,那么首先由U'先对x做变换: 由于正交阵“ U的逆=U‘ ”,对于两个空间来讲,新空间下的“ 基E' 坐标 ...
- 利用AutoLayout适配滚动视图和表视图
1.新增一个contentView,设置为与滑动视图的父视图等高等宽. 2.利用代码 if(_MyTestTableView.frame.size.height != _MyTestTableView ...
- 【 spring配置文件详解】
转自: http://book.51cto.com/art/201004/193743.htm Spring配置文件是用于指导Spring工厂进行Bean生产.依赖关系注入(装配)及Bean实例分发的 ...
- hdu 1361.Parencodings 解题报告
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence. P-se ...
- python学习笔记:第四天( 字符串)
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 在Python2中,普通字符串是以8位ASCII码进行存储的,而Unicode字 ...
- hdu 1358 Period(kmp求一个串的重复子串)
题意:统计单串中从某个位置以前有多少重复的串 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string. ...