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

Input
4 3 2 2
3 1 3 2
1 2
2 3
4 4
Output
2
Input
5 2 1 1
1 2 3 4 5
2 4
1 5
Output
1
Input
5 3 3 5
5 5 2 1 1
1 2
2 3
3 4
Output
-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)的更多相关文章

  1. BZOJ 1044 木棍分割(二分答案 + DP优化)

    题目链接  木棍分割 1044: [HAOI2008]木棍分割 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3830  Solved: 1453[S ...

  2. 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 ...

  3. hdu 5534 Partial Tree 背包DP

    Partial Tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...

  4. 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/ ...

  5. noj [1479] How many (01背包||DP||DFS)

    http://ac.nbutoj.com/Problem/view.xhtml?id=1479 [1479] How many 时间限制: 1000 ms 内存限制: 65535 K 问题描述 The ...

  6. luogu 4377 Talent show 01分数规划+背包dp

    01分数规划+背包dp 将分式下面的部分向右边挪过去,通过二分答案验证, 注意二分答案中如果验证的mid是int那么l=mid+1,r=mid-1,double类型中r=mid,l=mid; 背包dp ...

  7. bzoj5281/luogu4377 Talent Show (01分数规划+背包dp)

    就是01分数规划的思路,只不过当把w[i]-r*t[i]>0的选完以后如果w值还没达到要求,那就再01背包dp一下就好了(dp时w值>W的时候就存在W里就不会爆内存了). (跑得很慢..大 ...

  8. POJ-2018 Best Cow Fences(二分加DP)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...

  9. HDU 3591 (完全背包+二进制优化的多重背包)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3591 The trouble of Xiaoqian Time Limit: 2000/1000 M ...

随机推荐

  1. cocos-lua基础学习(九)spite类学习笔记

    创建精灵 ,) ) layer:addChild( sprite ) ) layer:addChild(BatchNode, , kTagSpriteBatchNode) ,) ) layer:add ...

  2. mysql索引之聚簇索引与非聚簇索引

    1 数据结构及算法基础 1.1 索引的本质 官方定义:索引(Index)是帮助MySQL高效获取数据的数据结构 本质:索引是数据结构 查询是数据库的最主要功能之一.我们都希望查询速度能尽可能快,因此数 ...

  3. ubuntu16.04(64位)安装 Drcom

    一 : 下载DrClient(DrcomAndPPOE) 解压   下载地址 https://www.baidu.com/s?wd=DrClient(DrcomAndPPOE)&rsv_spt ...

  4. DB开发之oracle存储过程

    1. 存储过程格式 /* Formatted on 2011/1/17 13:20:44 (QP5 v5.115.810.9015) */ CREATE OR REPLACE procedure pr ...

  5. python 字典(dict)get方法应用

    如果我们需要获取字典值的话,我们有两种方法,一个是通过dict['key'],另外一个就是dict.get()方法. 今天给大家分享的就是字典的get()方法. 这里我们可以用字典做一个小游戏,假设用 ...

  6. CF337C - Quiz

    /*题目大意,给出n道题,假设答对了m道题,求最小的分数,有一个规则,就是连续答对num==k道题那么分数就翻倍,然后num清零,从新开始计数,到大连续k道的时候 要先加上这道题的分数1,再乘以2, ...

  7. RocEDU.阅读.写作《苏菲的世界》书摘(六)

    * 身为一个经验主义者,休姆期许自己要整理前人所提出的一些混淆不清的思想与观念,包括中世纪到十七世纪这段期间,理性主义哲学家留传下来的许多言论和著作.休姆建议,人应回到对世界有自发性感觉的状态.他说, ...

  8. HDU 6438 网络赛 Buy and Resell(贪心 + 优先队列)题解

    思路:维护一个递增队列,如果当天的w比队首大,那么我们给收益增加 w - q.top(),这里的意思可以理解为w对总收益的贡献而不是真正获利的具体数额,这样我们就能求出最大收益.注意一下,如果w对收益 ...

  9. MSBI

    https://blog.csdn.net/fanyingnedu/article/details/78597207 Familiarity with Microsoft BI Stack - SSI ...

  10. C# 版本和.NET 版本以及VS版本的对应关系

    https://en.wikipedia.org/wiki/C_Sharp_(programming_language)#Versions http://stackoverflow.com/quest ...