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. js 面向对象中,定义一个函数的过程

    定义一个函数做的两件事:1: 实例化一个Function对象:2: 实例化一个Object对象,并给该函数扩展prototype属性指向这个构造函数 大致过程如图所示: 每一种引用类型(函数,对象,数 ...

  2. tfgan折腾笔记(三):核心函数详述——gan_loss族

    gan_loss族的函数有: 1.gan_loss: 函数原型: def gan_loss( # GANModel. model, # Loss functions. generator_loss_f ...

  3. echarts-liquidfill 水球显示小数点

    使用echarts-liquidfill.js的水球,水球上显示的文字可以使用chartOption.series[0].label.normal.formatter设置,想显示什么显示什么. var ...

  4. java网络编程——多线程数据收发并行

    基本介绍与思路 收发并行 前一篇博客中,完成了客户端与服务端的简单TCP交互,但这种交互是触发式的:客户端发送一条消息,服务端收到后再回送一条.没有做到收发并行.收发并行的字面意思很容易理解,即数据的 ...

  5. iframe的父子层跨域 用了百度的postMessage()方法

    父层:第一个是方法申明 第二个是接收子层过来的数据 <script type="text/javascript"> $("#main").load( ...

  6. (转)windows宿主机,ubuntu虚拟机下的上网设置(有线网络和无线网络)

    转自:http://hi.baidu.com/puppywst/item/d9f73734856e2af32684f4e3 虚拟机下ubuntu共享方式上网: 有线网络 在有线网络的条件下,vmwar ...

  7. Win2012+Nginx+IIS+xxfpm(服务版)

    这次做了一个项目部署在环境为win2012+nginx1.13.5+mysql5.6+php7的环境下,服务器是阿里云的 由于之前没有这种经验,遇到了点坑(据参考文章里说的这坑还有些年份了),最开始自 ...

  8. postman使用简介

    postman进行Http类型的接口测试的功能测试(手工测试): 1.postman下载,解压,打开Chrome浏览器-->设置-->扩展程序-->勾选开发者模式-->加载已解 ...

  9. SpringBoot源码分析(一)@SpringBootApplication解析

    @SpringBootApplication解析 三层注解 @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(exclu ...

  10. 遍历集合的常见方式,排序,用lambda表示是怎样的

       Collection集合的功能:            Object[] toArray() 将集合转成数组            Iterator iterator() 通过方法的调用 获取I ...