Lonlife-ACM 1014 - Absolute Defeat [差分]

来源:互联网 发布:网络教育学校名单 编辑:程序博客网 时间:2024/05/15 23:46

DESCRIPTION
Eric has an array of integers a1,a2,...,an
. Every time, he can choose a contiguous subsequence of length k and increase every integer in the contiguous subsequence by 1.He wants the minimum value of the array is at least m
. Help him find the minimum number of operations needed.
INPUT
There are multiple test cases. The first line of input contains an integerT
, indicating the number of test cases. For each test case:The first line contains three integersn,m andk(1n105,1kn,1m104).The second line contains n integersa1,a2,...,an(1ai104)
.
OUTPUT
For each test case, output an integer denoting the minimum number of operations needed.
SAMPLE INPUT
32 2 21 15 1 41 2 3 4 54 10 31 2 3 4
SAMPLE OUTPUT
1015
SOLUTION

#include<cstdio>#include<iostream>#include<algorithm>#include<cstring>#include<cmath>using namespace std;int T;#define maxn 100010int a[maxn],d[maxn];int n,m,k;int main(){scanf("%d",&T);while(T--){scanf("%d%d%d",&n,&m,&k);int tot = 0,sum = 0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);d[i]=a[i]-a[i-1];}for(int i=1;i<=n;i++){sum+=d[i];if(sum<m){tot += m-sum;if(i+k<=n)d[i+k] -= m-sum;sum += m-sum;}}printf("%d\n",tot);}return 0;}



0 0
原创粉丝点击