F2. Pictures with Kittens (hard version)
time limit per test
2.5 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
The only difference between easy and hard versions is the constraints.
Vova likes pictures with kittens. The news feed in the social network he uses can be represented as an array of nn consecutive pictures (with kittens, of course). Vova likes all these pictures, but some are more beautiful than the others: the ii-th picture has beauty aiai.
Vova wants to repost exactly xx pictures in such a way that:
For example, if k=1k=1 then Vova has to repost all the pictures in the news feed. If k=2k=2 then Vova can skip some pictures, but between every pair of consecutive pictures Vova has to repost at least one of them.
Your task is to calculate the maximum possible sum of values of reposted pictures if Vova follows conditions described above, or say that there is no way to satisfy all conditions.
Input
The first line of the input contains three integers n,kn,k and xx (1≤k,x≤n≤50001≤k,x≤n≤5000) — the number of pictures in the news feed, the minimum length of segment with at least one repost in it and the number of pictures Vova is ready to repost.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the beauty of the ii-th picture.
Output
Print -1 if there is no way to repost some pictures to satisfy all the conditions in the problem statement.
Otherwise print one integer — the maximum sum of values of reposted pictures if Vova follows conditions described in the problem statement.
Examples
input
Copy
5 2 3 5 1 3 10 1
output
Copy
18
input
Copy
6 1 5 10 30 30 70 10 10
output
Copy
-1
input
Copy
4 3 1 1 100 1 1
output
Copy
100
点我传送
n个物体,每隔m个物体必须取一个拿走,需要取走k个。
询问最大能得到的价值是多少。
很容易想到n^3的方法,
(t=max(i-m,0);t
但是在增强的数据里面显然不行。
观察到dp[t][j-1]的更新实际上是可以用优先队列来完成的,因为dp[ i ][ j ]显然是只需要取到dp[ t ][ j-1 ]中最大的一个就可以了,那么我们就先把取走了 j 的信息先用优先队列储存好就可以了。随着 i 的更新,每次取出队首元素时来考察他们的位置是否符合。
最后由于元素太多了,第一次交T掉了。改了个continue上去,跑了2400ms过了非常神奇。
#include
#include
#include
#include
#include
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
struct Node
{
LL val;
int pos,id;
bool operator < (const Node b)const
{
if(val!=b.val)return valq[5005];
int main()
{
int tot=0;
scanf("%d %d %d",&n,&m,&k);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
memset(dp,-inf,sizeof(dp));
dp[0][0]=0;
Node st;
st.id=tot++;st.val=0;st.pos=0;
q[0].push(st);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=k;j++)
{
if(j>i)break;
while(!q[j-1].empty()&&q[j-1].top().pos=n-m+1;i--) ans=max(ans,dp[i][k]);
printf("%lld\n",ans);
}