G. FacePalm Accounting
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/gym/100513/problem/G
Description
An owner of a small company FacePalm has recently learned that the city authorities plan to offer to small businesses to participate in improving parks and garden squares. However, credible sources informed the FacePalm owner that the loss-making companies will not get such an offer. Moreover, the sources have also told how loss-making companies will be determined.
A company will be considered loss-making if for every k contiguous days the total income of the company is negative.
The FacePalm owner discussed the situation with his chief accountant, and they decided to change the report so that the company would look loss-making.
The company report for n days can be represented as a sequence of integers a1, a2, ..., an, where ai is the company income in the dayi (negative values correspond to losses).
The accountant can change any values in this sequence, but no updated value can become less than the smallest value in the original report — otherwise the updated report will look absolutely implausible. Besides, the accountant wants the total change of the values to be as small as possible.
We will assume that the total change of the values is , where is the i-th value in the updated report.
Your task is to calculate the minimum required total change of the values and provide the updated report.
Input
The first line contains integers n and k (1 ≤ k ≤ n ≤ 2·105) — the number of days in the report and the number of days in the definition of loss-making company.
The second line contains n space-separated integers a1, a2, ..., an ( - 10000 ≤ ai ≤ 10000), where ai is the company income in dayi.
It is guaranteed that at least one value of ai is negative.
Output
In the first line print the required minimum total change. In the second line print the corresponding updated report. No value in the updated report can be less than the minimum value of the original report.
If there are multiple solutions, print any of them.
Sample Input
5 4
3 -3 -1 1 2
Sample Output
1
2 -3 -1 1 2
HINT
题意
要维护每一个连续长度为k的区间,使得区间和小于0,并且修改的数,不能小于原数组最小的数
问你最少修改多少
题解:
暴力就好了,修改肯定修改右边比修改左边好,然后暴力就好了……
代码
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) const int maxn=202501; #define mod 1000000007 #define eps 1e-9 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** ll a[maxn]; ll sum=0; int main() { ll n=read(),k=read(); ll minn=inf; for(int i=1;i<=n;i++) a[i]=read(),minn=min(minn,a[i]); for(int i=1;i<k;i++) sum+=a[i]; ll ans=0; for(int i=k;i<=n;i++) { sum+=a[i]-a[i-k]; if(sum>=0) { ans+=(sum+1); ll tmp=sum+1; int now=i; while(tmp) { ll kiss=min(tmp,a[now]-minn); a[now]-=kiss; tmp-=kiss; now--; } sum=-1; } } cout<<ans<<endl; for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; }