1642: 【USACO】Payback(还债)
1642: 【USACO】Payback(还债)
时间限制: 1 Sec 内存限制: 64 MB
提交: 190 解决: 95
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述
"Never a borrower nor a lender be." O how Bessie wishes she had taken that advice! She has borrowed from or lent money to each of N (1 <= N <= 100,000) friends, conveniently labeled 1..N. Payback day has finally come. She knows she is owed more money than she owes to the other cows. They have all lined up in a straight line, cow i standing i meters from the barn. Bessie is going to traverse the line collecting money from those who owe her and reimbursing money to those she owes. As she moves down the line, she can request any cow who owes her money to give her the money. When she has enough money to pay off any or all of her debts, she can pay the (recently collected) money to those she owes. Cow i owes Bessie D_i money (-1,000 <= D_i <= 1,000; D_i != 0). A negative debt means that Bessie owes money to the cow instead of vice-versa. Bessie starts at the barn, location 0. What is the minimum distance she must travel to collect her money and pay all those she owes? She must end her travels at the end of the line.
/*
"决不借别人钱也不借钱给别人。" 贝茜希望自己能遵守这个忠告! 她从N(1 <= N <= 100,000)个朋友那借了钱或借钱给他们,这些朋友编号为1..N。 还清债务的日子到了。她知道她借给别人的钱比她欠别人的钱多。他们全部排成了一条直线,奶牛i站在距离谷仓i米的地方。贝茜要穿过这条直线来索要她的钱并还清她的债务。
当她走过某一头牛时,她可以请求欠她钱的牛还她钱。当她有足够多的钱还清任一头牛或所有牛的债务时,她可以去还钱。奶牛i与贝茜的债务为D_i(-1,000 <= D_i <= 1,000; D_i != 0)。正整数表示奶牛i欠贝茜钱,反之则是贝茜欠他们钱。 贝茜一开始呆在谷仓,位置号为0。编一个程序计算出她收还所有债务必须移动的最小距离。她最后必须站在这条直线上的最后一头牛那。
*/
输入
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer: D_i
/*
* 第1行: 一个单精度整数: N
* 第2..N+1行: 行i+1表示一个单精度整数: D_i
*/
输出
* Line 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.
/*
* 第1行: 一个单精度整数表示贝茜为了收还债务而必须移动的最小米数。
*/
样例输入
5
100
-200
250
-200
200
样例输出
9
提示
INPUT DETAILS: Three cows owe Bessie; she owes two. When she's done, Bessie will have 150 money.
/*
输入说明: 三头牛欠贝茜钱; 她欠两头牛钱。当她收还完债务时,有150元钱。
*/
OUTPUT DETAILS:(输出说明:)
来源/分类
USACO 2009 March Bronze
题解如下
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int ar[n+1];
ar[0]=0;
for(int i=1;i<n+1;i++)
{
scanf("%d",&ar[i]);
}
int sum_money=0;
int owe_money=0;
int sum_distance=0;
int flag_pos=99999999;
for(int i=1;i<=n;i++)
{
sum_distance++;
if(ar[i]>0)
{
sum_money+=ar[i];
if(sum_money>=-owe_money&&owe_money!=0)
{
sum_money+=owe_money;
sum_distance+=2*(i-flag_pos);
flag_pos=9999999;
owe_money=0;
}
}
else if(ar[i]<0)
{
if(sum_money>=-ar[i])
{
sum_money+=ar[i];
ar[i]=0;
}
else
{
if(flag_pos>i)
flag_pos=i;
owe_money+=ar[i];
}
}
}
printf("%d",sum_distance);
return 0;
}
1642: 【USACO】Payback(还债)的更多相关文章
- USACO . Your Ride Is Here
Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
- USACO翻译:USACO 2012 JAN三题(2)
USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...
- USACO翻译:USACO 2012 JAN三题(1)
USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...
随机推荐
- SpringBoot1.5.10.RELEASE整合druid时,在druid monitor界面出现(*) property for user to setup
接上篇随笔,配置druid之后出现的错误,解决方法如下: @Bean(destroyMethod = "close",initMethod = "init") ...
- Aircrack-ng无线审计工具破解无线密码
Aircrack-ng工具 Aircrack-ng是一个与802.11标准的无线网络分析的安全软件,主要功能有网络探测.数据包嗅探捕获.WEP和WPA/WPA2-PSK破解.Aircrack可以工作在 ...
- 解决 unable to lock the administration directory (/var/lib/dpkg/) 问题
阿里开源镜像站提供了raspbian的软件包镜像,国内的用户可以选择改用阿里镜像站作为更新源. 但是在更换源之后,执行 sudo apt-get update && apt-get u ...
- postman小工具
进入lmm后,做接口测试使用的是postman,以前稍微接触过,但是不是很会用,这里就自学顺便总结一下,以便以后或者能帮助到别人,如果有什么不同的意见或者有错误,请毫不客气的指出,感谢! 推荐一篇博客 ...
- <字符串匹配>KMP算法为何比暴力求解的时间复杂度更低?
str表示文本串,m表示模式串; str[i+j] 和 m[j] 是正在进行匹配的字符; KMP的时间复杂度是O(m+n) , 暴力求解的时间复杂度是O(m*n) KMP利用了B[0:j]和A[i ...
- 记一次华为eNSP设备网络项目基本配置过程
下图为综合项目示例图,详细命令见图下: 屏蔽垃圾信息undo terminal monitorundo terminal trappingundo terminal loggingundo termi ...
- vue练手项目——桌面时钟
用vue实现一个简单的网页桌面时钟,主要包括时钟显示.计时.暂停.重置等几个功能. 效果图如下,页面刚进来的时候是一个时钟,时钟上显示的时.分.秒为当前实际时间,点击计时器按钮后,页面变成一个计时器, ...
- word文档常用的格式操作(分页、分节、每一节如何设置页码、以及生成目录)
分页: 分节: 自动生成目录:
- 通过js自动判断移动终端设备(ios\android等)
当用户用移动设备扫描一个二维码是,将扫描后的链接链接到一个页面,该页面只包含判断移动终端设备的js,判断好后自动跳转到对应的链接 或下载对应的内容. html代码如下: <script> ...
- 264. 丑数 II
编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n = 10输出: 12解释: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 是前 ...