NC213956. RikkawithRCPC
描述
输入描述
The first line contains three integers and .
The second line contains n integers .
输出描述
Output a single integer, the answer.
示例1
输入:
4 1 5 3 1 4 2
输出:
3
说明:
For the sample, one optimal plan is to answer questions on Day 1 and Day 3:示例2
输入:
10 2 7 2 7 4 4 1 5 6 7 3 1
输出:
36
C++(clang++11) 解法, 执行用时: 49ms, 内存消耗: 5140K, 提交时间: 2021-01-21 22:48:20
#include<cstdio> #include<iostream> #define rep(i,s,t) for(int i=s;i<=t;++i) #define db double using namespace std; #define ll long long const int N=1e6+11; int n,k,T; int a[N],q[N]; ll f[N],s[N],ans; int main(){ int l,r; scanf("%d%d%d",&n,&k,&T); ++k; rep(i,1,n)scanf("%d",a+i),s[i]=s[i-1]+a[i]; l=r=0; rep(i,1,n){ f[i]=f[i-1]; if(i>=k){ while(l!=r&&f[q[r-1]]-s[q[r-1]]<f[i-k]-s[i-k])--r; q[r++]=i-k; } while(l!=r&&s[q[l]]+T<s[i])++l; if(l!=r)f[i]=max(f[i],f[q[l]]-s[q[l]]+s[i]); } ans=s[n]; rep(i,0,n) if(s[n]-s[i]<=T) ans=min(ans,s[i]-f[i]); cout<<ans<<endl; return 0; }