CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)
You are a given a list of integers a 1 ,a 2 ,…,a n a1,a2,…,an and s s of its segments [l j ;r j ] [lj;rj] (where 1≤l j ≤r j ≤n 1≤lj≤rj≤n ).
You need to select exactly m m segments in such a way that the k k -th order statistic of the multiset of a i ai , where i i is contained in at least one segment, is the smallest possible. If it's impossible to select a set of m m segments in such a way that the multiset contains at least k k elements, print -1.
The k k -th order statistic of a multiset is the value of the k k -th element after sorting the multiset in non-descending order.
Input
The first line contains four integers n n , s s , m m and k k (1≤m≤s≤1500 1≤m≤s≤1500 , 1≤k≤n≤1500 1≤k≤n≤1500 ) — the size of the list, the number of segments, the number of segments to choose and the statistic number.
The second line contains n n integers a i ai (1≤a i ≤10 9 1≤ai≤109 ) — the values of the numbers in the list.
Each of the next s s lines contains two integers l i li and r i ri (1≤l i ≤r i ≤n 1≤li≤ri≤n ) — the endpoints of the segments.
It is possible that some segments coincide.
Output
Print exactly one integer — the smallest possible k k -th order statistic, or -1 if it's impossible to choose segments in a way that the multiset contains at least k k elements.
Examples
4 3 2 2
3 1 3 2
1 2
2 3
4 4
2
5 2 1 1
1 2 3 4 5
2 4
1 5
1
5 3 3 5
5 5 2 1 1
1 2
2 3
3 4
-1
题意:给定给N个点,以及M个线段,让你选择S个线段,使得至少被一个线段覆盖的点排序后,第K大最小,没有则输出-1。
思路:求第K大最小,显然需要二分,每次验证看当前的mid是否有大于等于K个数小于mid。验证我们用dp来验证,复杂度是O(NMS*lgN);
需要优化掉一个。这里用背包把M优化掉了,我们找到每个点的Next,Next代表包含这个点的最右端。就不难得到dp方程,这个时候M已经没用了。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
struct in{ int L,R;}s[maxn];
int a[maxn],b[maxn],N,S,M,K,sum[maxn];
int dp[maxn][maxn],Next[maxn];
bool check(int Mid) //M个选最多S个的第K大
{
rep(i,,N) sum[i]=sum[i-]+(a[i]<=Mid);
rep(i,,S) rep(j,,N) dp[i][j]=;
rep(i,,S){
rep(j,,N) dp[i][j]=max(dp[i][j],dp[i-][j]); //不选j位置。
rep(j,,N) if(Next[j]) dp[i][Next[j]]=max(dp[i][Next[j]],dp[i-][j-]+sum[Next[j]]-sum[j-]); //选j
rep(j,,N) dp[i][j]=max(dp[i][j],dp[i][j-]);
}
return dp[S][N]>=K;
}
int main()
{
scanf("%d%d%d%d",&N,&M,&S,&K);
rep(i,,N) scanf("%d",&a[i]),b[i]=a[i];
rep(i,,M) scanf("%d%d",&s[i].L,&s[i].R);
rep(i,,M) rep(j,s[i].L,s[i].R) Next[j]=max(Next[j],s[i].R);
sort(b+,b+N+); int L=,R=N,Mid,ans=-;
while(L<=R){
Mid=(L+R)>>;
if(check(b[Mid])) ans=b[Mid],R=Mid-;
else L=Mid+;
}
printf("%d\n",ans);
return ;
}
CF-1055E:Segments on the Line (二分&背包&DP优化)(nice problem)的更多相关文章
- BZOJ 1044 木棍分割(二分答案 + DP优化)
题目链接 木棍分割 1044: [HAOI2008]木棍分割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3830 Solved: 1453[S ...
- HDU 1171 Big Event in HDU 多重背包二进制优化
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1171 Big Event in HDU Time Limit: 10000/5000 MS (Jav ...
- hdu 5534 Partial Tree 背包DP
Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- Codeforces Codeforces Round #319 (Div. 2) B. Modulo Sum 背包dp
B. Modulo Sum Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/577/problem/ ...
- noj [1479] How many (01背包||DP||DFS)
http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...
- luogu 4377 Talent show 01分数规划+背包dp
01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...
- bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)
就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...
- POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...
- HDU 3591 (完全背包+二进制优化的多重背包)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...
随机推荐
- ASP.NET 无法生成临时类(result=1)图解
说明: 执行当前 Web 请求期间,出现未处理的异常.请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息. 异常详细信息: System.InvalidOperationExcep ...
- Java实现动态规划法求解0/1背包问题
摘要: 使用动态规划法求解0/1背包问题. 难度: 初级 0/1背包问题的动态规划法求解,前人之述备矣,这里所做的工作,不过是自己根据理解实现了一遍,主要目的还是锻炼思维和编程能力,同时,也是为了增进 ...
- docker 删除所有的 docker ps -a 记录
docker rm `docker ps -a -q` 删除的是镜像运行的实例,要删除镜像用docker rmi + 镜像名或者那串字符(image id)
- 正在尝试解析依赖项“MvvmLightLibs (≥ 5.2.0.0)”。 “MvvmLightLibs”已拥有为“CommonServiceLocator”定义的依赖项
正在尝试解析依赖项"MvvmLightLibs (≥ 5.2.0.0)". "MvvmLightLibs"已拥有为"CommonServiceLoca ...
- 20145322何志威《网络对抗》Exp2 后门原理与实践
基础问题回答 1 例举你能想到的一个后门进入到你系统中的可能方式? 在网上下载盗版软件时捆绑的后门程序. 不小心进入钓鱼网站. 2 例举你知道的后门如何启动起来(win及linux)的方式? Wind ...
- 【安装】Nginx安装
系统平台:CentOS release 6.5 (Final) 64位. 安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool ...
- Anaconda中常用的用法
Anaconda中常用的用法 conda 是开源包(packages)和虚拟环境(environment)的管理系统. packages 管理: 可以使用 conda 来安装.更新 .卸载工具包 ,并 ...
- Tinkoff Challenge - Elimination Round B. Igor and his way to work(dfs+优化)
http://codeforces.com/contest/793/problem/B 题意:一个地图,有起点和终点还有障碍点,求从起点出发到达终点,经过的路径上转弯次数是否能不超过2. 思路: 直接 ...
- HDU 3605 Escape(状态压缩+最大流)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意: 有n个人和m个星球,每个人可以去某些星球和不可以去某些星球,并且每个星球有最大居住人数,判断是否所 ...
- 2016"百度之星" - 资格赛(Astar Round1) A 逆元
Problem A Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem ...