TOJ 3203.Pay Back
来源:互联网 发布:idea中导入hadoop源码 编辑:程序博客网 时间:2024/05/30 22:50
题目链接:http://acm.tju.edu.cn/toj/showp3203.html
Time Limit: 2.0 Seconds Memory Limit: 65536K
Total Runs: 595 Accepted Runs: 260 Multiple test files
"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.
Input
* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer: D_i
Output
* Line 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.
Sample Input
5100-200250-200200
Sample Output
9
Source: USACO 2009 March Competition
水题,按照题意,一直模拟着进行就可:
#include <stdio.h>int debt[100001];int main(){int n;while(~scanf("%d",&n)){for(int i=0;i<n;i++)scanf("%d",&debt[i]);int sum=0,step=0,position=0;for(int i=0;i<n;i++){sum+=debt[i];if(sum<0&&sum>debt[i])position=i;else if(sum>=0&&sum<debt[i])step+=2*(i-position);}printf("%d\n",step+n);}}
- TOJ 3203.Pay Back
- Sicily 2013. Pay Back
- 2013. Pay Back
- Sicily 2013. Pay Back
- Sicily 2013 Pay Back
- Sicily 2013. Pay Back
- pay
- Back...
- Back
- back
- back
- Back
- BACK
- Apple Pay
- Apple Pay
- Apple Pay
- apple Pay
- Apple pay
- 【框架】[Spring]纯Java方式实现AOP拦截-详解ThrowsAdvice异常通知
- pgsql提示启动成功但查不到进程
- seo点击积累一:缺少keywords和description的meta标签
- Hibernate将HQL语句转换成SQL语句
- 自定义控件-下拉刷新
- TOJ 3203.Pay Back
- 南阳理工OJ 题目115 城市平乱
- 流体力学及其工程应用
- 直方图中最大矩形面积
- Unity Shaders 基础篇(二) 基本操作
- 多重json字符串解析
- http问题
- 启动Mac mysql服务器步骤
- Unity UI(七):交互式控件之Scroll Rect和ScrollView