Codeforces Gym 100513G G. FacePalm Accounting
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长度区间,需满足sum(k)小于0,问你最小修改差是多少,且修改的值不得小于原序列中最小值,
题解:
贪心,每次从K区间中右到左来修改
代码
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#define inf 1000000007
#define mod 1000000007
using namespace std;
typedef __int64 ll;
inline ll read()
{
ll x=,f=;
char ch=getchar();
while(ch<''||ch>'')
{
if(ch=='-')f=-;
ch=getchar();
}
while(ch>=''&&ch<='')
{
x=x*+ch-'';
ch=getchar();
}
return x*f;
}
//*******************************************************************
ll a[]; int main()
{
ll n,k;
ll minn=inf; scanf("%I64d%I64d",&n,&k);
for(int i=; i<=n; i++)
scanf("%I64d",&a[i]),minn=min(minn,a[i]);
ll sum=; for(int i=; i<k; i++)
{
sum+=a[i];
}
ll ans=;
for(int i=k; i<=n; i++)
{
sum+=(a[i]-a[i-k]);
if(sum>=)
{
ll tmp=sum+; //printf("1111");
ans+=sum+;
int j=i;
while(tmp>)
{ ll gg=min(a[j]-minn,tmp);
a[j]-=gg;
tmp-=gg;
j--;
}
sum=-;
}
}
printf("%I64d\n",ans);
for(int i=; i<=n; i++)
{
printf("%I64d ",a[i]);
}
return ;
}
Codeforces Gym 100513G G. FacePalm Accounting的更多相关文章
- Codeforces Gym 100513G G. FacePalm Accounting 暴力
G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...
- Codeforces Gym 100637G G. #TheDress 暴力
G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...
- Codeforces Gym 100203G G - Good elements 标记暴力
G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- Codeforces Gym 100203G G - Good elements 暴力
G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...
- Codeforces Gym 101142 G Gangsters in Central City (lca+dfs序+树状数组+set)
题意: 树的根节点为水源,编号为 1 .给定编号为 2, 3, 4, …, n 的点的父节点.已知只有叶子节点都是房子. 有 q 个操作,每个操作可以是下列两者之一: + v ,表示编号为 v 的房子 ...
- Codeforces gym 101061 G【递推公式+逆元】
题意: 就是n复制m次,然后数mod1e9+7; 思路: 案例:31*10^6 + 31*10^4 + 31*10^2 + 31*10^0 所以就是一个等比数列,然后整理一下就是n*(10^(m*le ...
- Codeforces Gym 101138 G. LCM-er
Description 在 \([a,b]\) 之间选择 \(n\) 个数 (可以重复) ,使这 \(n\) 个数的最小公倍数能被 \(x\) 整除,对 \(10^9+7\) 取膜. \(1\leqs ...
- Codeforces Gym 101190M Mole Tunnels - 费用流
题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...
- Codeforces Gym 101623A - 动态规划
题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...
随机推荐
- WPF捕捉Windows关机事件
private const int SC_SCREENSAVE = 0xF140; private const int WM_QUERYENDSESSION = 0x0011; private boo ...
- Ubuntu如何安装secureCRT
以前在ubuntu上安装过secureCRT,是自己按照网上的教程安装的. 电脑重装了系统之后,想在电脑上安装一个,又得去网上搜,安装完后,自己总结了一下. 1,下载secureCRT包 根据自己电脑 ...
- js获取单选框里面的值
rt,如果想获取单选框里面的值,该如何获取呢. <script> window.onload = function(){ //通过名字获取 getElementsByName //var ...
- bootshrap会改变IE浏览器滚动条样式
在某个小网站的开发中 客户一直抱怨在IE11中网页右边滚动条不一样 后来发现在IE11中,有2个页面滚动条会自动隐藏,一开始以为是浏览器默认行为,改了overflow:scroll后也没有用.仔细观察 ...
- 繁华模拟赛 Vicent坐电梯
/*n<=5000这样就不能用O(n)的转移了,而是要用O(1)的转移.注意我们每次的转移都来自一个连续的区间,而且我们是求和区间求和?前缀和!令sum[step][i]表示f[ste ...
- tyvj1213 嵌套矩形
描述 有n个矩形,每个矩形可以用a,b来描述,表示长和宽.矩形X(a,b)可以嵌套在矩形Y(c,d)中当且仅当a<c,b<d或者b<c,a<d(相当于旋转X90度).例如 ...
- Android/iOS微信6.3.5同时发布更新 支持群视频聊天、群公告
下午微信6.3.5发布更新,新版最大变化就是支持群视频聊天,又一次向手机QQ靠拢.在群管理方面,支持发布群公告,支持群主转让给其他群成员,同样都是QQ玩剩下的功能.另外,新版支持微信运动查看步数图表. ...
- E. Tetrahedron(数学推导)
E. Tetrahedron 分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报 time limit per test 2 seconds memory lim ...
- NGUI的部分控件无法更改layer?
http://momowing.diandian.com/post/2012-09-17/40038835795 今天狗日的遇到这样的问题,这是一个imagebutton:,它的层定义为:,NGUI里 ...
- load url from future 解释
利用url 标签之后,不管urlpatterns里的某个地址叫法怎么改变,Templates里的地址都不用修改了.在模版中调用url标签的时候,需要:{% load url from future % ...