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. 测试后的iview的表格

    <template>   <GPage bg>     <div>       <div class="table">        ...

  2. 必备技能二、es6

    一.ES6模块 ES6 引入了模块化,其设计思想是在编译时就能确定模块的依赖关系,以及输入和输出的变量. ES6 的模块化分为导出(export) @与导入(import)两个模块. 特点 ES6 的 ...

  3. 一致性hash算法之php实现

    源码地址:https://github.com/killallspree/myFrame/blob/master/framework/components/Flexihash.php

  4. Simulink仿真入门到精通(十四) Simulink自定义环境

    14.1 Simulink环境自定义功能 sl_sustomization.m函数是Simulink提供给用户使用MATLAB语言自定义Simulink标准人机界面的函数机制.若sl_sustomiz ...

  5. 工具之scroolToIndex

    需求定位:导航中实现子元素滚动到父元素的最左侧 解决方案:查找该子元素的offsetLeft值,然后让父元素滚动offsetLeft,parenDom.scrollLeft = childDom.of ...

  6. vscode使用cnpm报错

     1.在wind10搜索框里输入 Windows PowerShell 进入这个界面 2.打开Windows PowerShell 之后 输入命令:set-ExecutjionPolicy Remot ...

  7. video 在iphone手机的ios系统和微信端无法自动播放

    描述:video 在iphone手机,微信端无法自动播放,ios系统下不能自动播放视频.而且如果没有autoplay属性,在微信端点击一次,弹不出视频,要一直触着两秒后才可以打开视频.如果点击播放的话 ...

  8. node 微信支付

    基于node 实现微信支付功能 需要了解的网站:微信支付 流程图: 1. 1.我的路由: const Koa = require('koa') const app = new Koa() const ...

  9. [CSP初赛] 组合数学的三个技巧以及从另一方面思考组合类问题

    也不知道老师讲不讲 话说好久没有水博客了,看了一点\(python\)然后就去搞文化课了 正好网课讲到组合数学,然后觉得还蛮难的(其实是我变菜了),就想到了以前的\(csp\)的组合数学基础 果然被我 ...

  10. S3C2440A特殊寄存器

    S3C2440A特殊寄存器 特殊寄存器有: 输入输出端口 存储器控制器 NANDFLASH 看门狗定时器 时钟和电源管理 PWM定时器 UART USB设备 中断控制器 DMA LCD控制器 RTC ...