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(还债)的更多相关文章

  1. 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 ...

  2. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  3. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  4. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  5. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  6. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  7. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

  8. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

  9. USACO翻译:USACO 2012 JAN三题(1)

    USACO 2012 JAN(题目一) 一.题目概览 中文题目名称 礼物 配送路线 游戏组合技 英文题目名称 gifts delivery combos 可执行文件名 gifts delivery c ...

随机推荐

  1. flask blueprint出现的坑

    from flask import Blueprint admin = Blueprint('admin',__name__) def init_bule(app): app.register_blu ...

  2. vue基础----自定义组件directive ,bind,update,insert

    <div id="app"> <input type="text" v-limit.3="msg" v-focus> ...

  3. C# Bitmap 转 Bytes数组

    首先是Bitmap 转 MemoryStream MemoryStream ms = new MemoryStream(); bitmap.save(ms, ImageFormat.Jpeg); ms ...

  4. C 2015年真题

    1.写出程序输出结果 void main() { char p[10]="abc"; char q[]="xyz"; int i,j; i=0; while(* ...

  5. [剑指offer]3.数组中的重复数字

    3.数组中的重复数字 题目 找出数组中重复的数字. 在一个长度为 n 的数组 nums 里的所有数字都在 0-n-1 的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了 ...

  6. 手动实现AJAX

    AJAX 每日更新前端基础,如果觉得不错,点个star吧

  7. Django ajax的简单使用、自定义分页器

    一. ajax初识 1. 前后端传输数据编码格式contentType 使用form表单向后端提交数据时,必须将form表单的method由默认的get改为post,如果提交的数据中包含文件,还要将f ...

  8. masql数据库的表查询

    昨日回顾 表与表之间建关系 一对多 换位思考 图书与出版社 先站在左表: 考虑左表的多条数据能否对应右表的一条数据 翻译:多本书能否被一个出版社出版 可以! 注意:单站在一张得出的表关系并不能明确两张 ...

  9. 带有路径压缩和rank优化的并查集实现

    public class unionfind2 implements UF { int[] parent; int[] rank; public unionfind2(int n) { parent= ...

  10. ASP.NET Core 奇淫技巧之伪属性注入

    一.前言 开局先唠嗑一下,许久未曾更新博客,一直在调整自己的状态,去年是我的本命年,或许是应验了本命年的多灾多难,过得十分不顺,不论是生活上还是工作上.还好当我度过了所谓的本命年后,许多事情都在慢慢变 ...