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$段,考虑最少能够减少多少划分 ...
随机推荐
- 如何建立批处理文件(.bat或.cmd)
如何建立批处理文件(.bat或.cmd) 建立批处理文件 批处理文件就是把多个dos命令放在一起. 批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令 ...
- Unity3D模型的细致纹理问题解决办法
http://hunterwang.diandian.com/post/2012-09-28/40039798509 也许有人也遇到过同样的问题,也许解决方式不同,我来介绍一下偶尔尝试发现的解决办法. ...
- rails命令避免直接操作数据库
文章是从我的个人主页上粘贴过来的, 大家也可以访问我的主页 www.iwangzheng.com 今天在数据库里新增人员的时候出了点小问题,有人员重复了,以至于要删除一些,大师和pp指导完成的. $r ...
- Hibernate与 MyBatis的比较
希望大家指出不对之处. 第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架,它出身于sf.net,现在已经成为Jboss的一部分. Myb ...
- net发送邮件
对于.NET而言,从2.0开始,发邮件已经是一件非常easy 的事了.下面我给出一个用C#群发邮件的实例,做了比较详细的注解,希望对有需要的朋友有所help.看了这篇BLOG,如果你还不会用.NET发 ...
- AES 推荐文章
链接如下,写得很好!http://blog.csdn.net/a00553344/article/details/4002507
- iOS 获得指定文件夹下的指定格式文件
这个容易忘记,然后只能用些自己写的长代码代替了....这里做个备忘 主要用到NSFileManager的 contentsOfDirectoryAtPath:error: 和 NSArray的 pat ...
- 关于php中数据访问的几点补充
前几篇文章说了,parent.self.static关键字的使用,parent可以访问父类的静态方法和静态变量,self和static可以访问本类的静态成员等等,但实际上他们还有其他作用,来看一下: ...
- php中重写和final关键字的使用
为什么把重写和final放在一起,原因就是一条:final的意思是不可更改的,也就是说final定义的东西是不可改变的,下面具体来说一下. 来看一段简单的代码: class BaseClass { f ...
- DP:Ant Counting(POJ 3046)
数蚂蚁 题目大意:一只牛想数蚂蚁,蚂蚁分成很多组,每个组里面有很多只蚂蚁,现在问你有多少种组合方式 (说白了就是问1,1,1,...,2...,3...,4...)这些东西有多少种排列组合方式 这一道 ...