zoukankan      html  css  js  c++  java
  • soj2013.Pay Back

    2013. Pay Back

    Constraints

    Time Limit: 1 secs, Memory Limit: 256 MB

    Description

     

     

    "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

    5
    100
    -200
    250
    -200
    200

    Sample Output

    9

    #include <iostream>
    #include <vector>
    using namespace std;
    int main()
    {
    	int N;
    	cin >> N;
    	int i;
    	vector<int> road(N);
    	for(i = 0;i < N;i++)
    		cin >> road[i];
    	int d = road[0];
    	int sum = 0;
    	int p;
    	for(i = 1;i < N;i++)
    	{
    		d += road[i];
    		if(d < 0 && d - road[i] >= 0)
    			p = i;
    		else if(d >= 0 && d - road[i] < 0)
    			sum += i + i - p - p;
    	}
    	sum += N;
    	cout << sum << endl;
    	return 0;
    }
    
  • 相关阅读:
    u-swipe-action报错Error: Not Found:Page[2][-1,20-9;9] at view.umd.min.js:1
    vue ui框架
    windows下简单部署django+vue项目(打包后)
    vue+django开发配置(vue转发请求、跨域携带cookie)
    python做定时任务schedule、aspscheduler、celery
    第四章练习题
    软件开发目录规范
    常用模块学习(六)
    常用模块学习(五)
    常用模块学习(二)
  • 原文地址:https://www.cnblogs.com/sysu-blackbear/p/3261597.html
Copyright © 2011-2022 走看看