2013. Pay Back
2013-08-18 12:06
441 查看
2013. Pay BackDescription"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.
[align=left] [/align]InputLine 1: A single integer: N Lines 2..N+1: Line i+1 contains a single integer: D_i
OutputLine 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.
Sample Input
Sample Output
题目概述:
Bessie欠一些人钱,也有一些人欠Bessie的钱。Bessie在原点,也就是x=0,而其他共n个人恰好住在x=1,2,3,…,n处。Bessie现在从家出发,要到把别人欠的债收回来,并用收回来的钱还清欠别人的债,并到达x=n处。
◦问Bessie最少花费的时间
方法:
当且仅当手中的可以还清当前的所有欠款时,立即回头去还清!
将每一步到达的钱累加:
(1)如果刚好从不欠款到欠款,说明目前这点是欠款最少的点,等到一有钱马上回来该点还钱。
(2)如果刚好从欠款过渡到不欠款,说明目前有能力去还钱,马上回头,所走的步数是当前点位置到起点来回减去还钱点位置到起点来回步数,即i+i-p-p;(需要还钱的不仅仅是前面记录下来的那点,沿途可能还有其他需要还钱的点,但不影响结果)
(3)其他情况只需要直接还钱或者直接收钱,因为前面有累加,所以不用再做处理。
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.
[align=left] [/align]InputLine 1: A single integer: N Lines 2..N+1: Line i+1 contains a single integer: D_i
OutputLine 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.
Sample Input
5 100 -200 250 -200 200
Sample Output
9
题目概述:
Bessie欠一些人钱,也有一些人欠Bessie的钱。Bessie在原点,也就是x=0,而其他共n个人恰好住在x=1,2,3,…,n处。Bessie现在从家出发,要到把别人欠的债收回来,并用收回来的钱还清欠别人的债,并到达x=n处。
◦问Bessie最少花费的时间
方法:
当且仅当手中的可以还清当前的所有欠款时,立即回头去还清!
将每一步到达的钱累加:
(1)如果刚好从不欠款到欠款,说明目前这点是欠款最少的点,等到一有钱马上回来该点还钱。
(2)如果刚好从欠款过渡到不欠款,说明目前有能力去还钱,马上回头,所走的步数是当前点位置到起点来回减去还钱点位置到起点来回步数,即i+i-p-p;(需要还钱的不仅仅是前面记录下来的那点,沿途可能还有其他需要还钱的点,但不影响结果)
(3)其他情况只需要直接还钱或者直接收钱,因为前面有累加,所以不用再做处理。
#include<iostream> using namespace std; int main() { int n; cin>>n; int arr[100001]; int sum=0; int p,pace; for(int i=0;i<n;i++) { cin>>arr[i]; } for(int j=0;j<n;j++) { sum+=arr[j]; if(sum<0&&sum-arr[j]>=0)//从不欠钱到欠钱的过渡点,记录下来 { p=j; } else if(sum>=0&&sum-arr[j]<0)//从欠钱到不欠钱的过渡点,马上回去还钱 { pace+=j+j-p-p; //pace记录回头还钱走过的步数 } } pace+=n; //所走过的步数还要加上一路走来的 cout<<pace<<endl; //system("pause"); return 0; }
相关文章推荐
- Sicily 2013. Pay Back
- Sicily 2013 Pay Back
- Sicily 2013. Pay Back
- sicily 2013. Pay Back
- Sicily 2013. Pay Back
- 网友提供Slides:CVPR2013显著性方面文章速读
- 总结:我的2013,我的C++
- Exchange 2013 PowerShell控制语句
- Windows VS2013 Intro - Output Debug Info
- 2013-3 阿里性能稳定性沙龙
- <2013 06 14> 无线局域网能否取代有线以太网?
- 【北京站】详解Visual Studio 2013:开发iOS及android应用!现场图集
- MyEclipse2013工具与破解工具
- [bzoj3202][SDOI2013]项链
- Exchange 2013/2016最新更新补丁发布
- HDU_2013——蟠桃记,反向推理
- VS2013遇到“未能找到元数据文件”问题解决办法
- Exchange 2010迁移Exchange 2013(一)共存部署
- Bzoj3193:[JLOI2013]地形生成:dp+组合数学
- BZOJ 3144 [Hnoi2013]切糕