4385: [POI2015]Wilcze doły

来源:互联网 发布:淘宝化妆品店名 编辑:程序博客网 时间:2024/04/29 15:05

4385: [POI2015]Wilcze doły

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 682  Solved: 282
[Submit][Status][Discuss]

Description

给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0。
请找到最长的一段连续区间,使得该区间内所有数字之和不超过p。

Input

第一行包含三个整数n,p,d(1<=d<=n<=2000000,0<=p<=10^16)。
第二行包含n个正整数,依次表示序列中每个数w[i](1<=w[i]<=10^9)。

Output

包含一行一个正整数,即修改后能找到的最长的符合条件的区间的长度。

Sample Input

9 7 2
3 4 1 9 4 1 7 1 3

Sample Output

5

HINT

将第4个和第5个数修改为0,然后可以选出区间[2,6],总和为4+1+0+0+1=6。

Source

鸣谢Claris

[Submit][Status][Discuss]



从左往右枚举区间的左端点,假设在最优决策下,左端点为L的时候最多能向右扩展到R

那么,当左端点为L+1的时候,向右扩展也至少能达到R,也就是具有决策单调性

因为只要把[L,R]的最优方案照搬过来,肯定也适用

对于[L,R]的最优决策,肯定是把其中一段长度为d,权值最大的子串变成0

右端点右移的时候用一个单调队列维护这样的子串就行了

#include<iostream>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<algorithm>#include<cmath>using namespace std;const int maxn = 2E6 + 20;typedef long long LL;int n,d,head,tail,Ans,q[maxn];LL p,sum[maxn],s[maxn];bool Judge(int L,int R){LL Max = head > tail?s[R]:max(s[R],s[q[head]]);return sum[R] - sum[L-1] - Max <= p;}void Insert(int R){while (head <= tail && s[q[tail]] <= s[R]) --tail; q[++tail] = R;}int getint(){char ch = getchar(); int ret = 0;while (ch < '0' || '9' < ch) ch = getchar();while ('0' <= ch && ch <= '9')ret = ret*10 + ch - '0',ch = getchar();return ret;}int main(){#ifdef DMCfreopen("DMC.txt","r",stdin);#endifcin >> n >> p >> d;for (int i = 1; i <= n; i++){int w = getint();sum[i] = sum[i-1] + 1LL*w;}for (int i = 1; i < d; i++) s[i] = sum[i];for (int i = d; i <= n; i++) s[i] = sum[i] - sum[i-d];int R = 0; head = 1;for (int i = 1; i <= n; i++){while (head <= tail && q[head] <= i + d - 1) ++head;while (R < n && Judge(i,R + 1)) Insert(++R);Ans = max(Ans,R - i + 1);}cout << Ans;return 0;}

0 0
原创粉丝点击