链接:https://ac.nowcoder.com/acm/contest/1086/C

题目描述

"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 Di money (-1,000 <= Di <= 1,000; Di != 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.

输入描述:

* Line 1: A single integer: N
* Lines 2..N+1: Line i+1 contains a single integer: Di

输出描述:

* Line 1: A single integer that is the total metric distance Bessie must travel in order to collect or pay each cow.

示例1

输入

  1.  
  2. -
  3.  
  4. -

输出

 

题目概述:
Bessie欠一些人钱,也有一些人欠Bessie的钱。Bessie在原点,也就是x=0,而其他共n个人恰好住在x=1,2,3,…,n处。Bessie现在从家出发,要到把别人欠的债收回来,并用收回来的钱还清欠别人的债,并到达x=n处。
问Bessie最少花费的时间

方法:
当且仅当手中的可以还清当前的所有欠款时,立即回头去还清!
将每一步到达的钱累加:
(1)如果刚好从不欠款到欠款,说明目前这点是欠款最少的点,等到一有钱马上回来该点还钱。
(2)如果刚好从欠款过渡到不欠款,说明目前有能力去还钱,马上回头,所走的步数是当前点位置到起点来回减去还钱点位置到起点来回步数,即(i-flag)*2;(需要还钱的不仅仅是前面记录下来的那点,沿途可能还有其他需要还钱的点,但不影响结果)
(3)其他情况只需要直接还钱或者直接收钱,因为前面有累加,所以不用再做处理。

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <string>
  5. #include <math.h>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. #include <set>
  10. #include <map>
  11. #include <math.h>
  12. const int INF=0x3f3f3f3f;
  13. typedef long long LL;
  14. const int mod=1e9+;
  15. const int maxn=1e5+;
  16. using namespace std;
  17.  
  18. int A[maxn];
  19.  
  20. int main()
  21. {
  22. int n;
  23. scanf("%d",&n);
  24. for(int i=;i<=n;i++)
  25. {
  26. scanf("%d",&A[i]);
  27. }
  28. int money=;
  29. int pace=;
  30. int flag;
  31. for(int i=;i<=n;i++)
  32. {
  33. pace++;
  34. if(money>=&&money+A[i]<)//从不欠钱到欠钱,记录下来这个要还钱的最远点
  35. {
  36. flag=i;
  37. }
  38. else if(money<&&money+A[i]>=)//从欠钱到不欠钱,说明已有能力还清前面所有点的钱,马上回去还钱
  39. {
  40. pace+=(i-flag)*;//一来一回
  41. }
  42. money+=A[i];
  43. }
  44. printf("%d\n",pace);
  45. return ;
  46. }
 
 
 

Pay Back(模拟)的更多相关文章

  1. python接口测试,mock模块基本使用介绍

    mock简介 py3已将mock集成到unittest库中 为的就是更好的进行单元测试 简单理解,模拟接口返回参数 通俗易懂,直接修改接口返回参数的值 mock作用 解决依赖问题,达到解耦作用 当我们 ...

  2. python_Mock基本使用

    ## 1.mock简介 1. py3已将mock集成到unittest库中 2. 为的就是更好的进行单元测试 3. 简单理解,模拟接口返回参数 4. 通俗易懂,直接修改接口返回参数的值 5. 官方文档 ...

  3. Codeforces 740A. Alyona and copybooks 模拟

    A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...

  4. iOS Apple Pay

    iOS 苹果支付 需要证书支持支付功能 targets 打开支付功能按钮 //ApplePay#import <PassKit/PassKit.h>                     ...

  5. Apple Pay

    Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上.目前还不支持企业证书添加. 环境搭建好后可以在模拟器上面运行,xcode7.2.1+ ...

  6. HDU 4884 TIANKENG’s rice shop (模拟)

    TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...

  7. JS5模拟类

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  8. 2017.07.11【NOIP提高组】模拟赛B组

    Summary 今天的比赛打得还不错,第一题被同桌灌输的贪心,纯模拟洗脑了,然后steal的看了一下,发现怎么也对不了,一直在检查.最后10分钟才找出反例,推出动态规划方程,没有想到怎么转移,比赛就结 ...

  9. 用DDD模拟案例分析

    之前我写了几篇关于DDD的介绍和一些小例子说明,我想这对于介绍DDD还是有些模糊,甚至还不知道怎么用DDD来分析设计.昨天和园友讨论也发现没有例子很难说明,所以今天我模拟了一个案例,同时这个案例也是真 ...

随机推荐

  1. JS的BOM对象

    BOM对象 (一)简介:BOM对象,即浏览器对象模型: 通过javascript的对象,操作和浏览器相关的操作 B:  Browser,浏览器 O: Object,对象 M: Model,模型 (1) ...

  2. salt教程1-理解saltstack

    https://docs.saltstack.com/en/getstarted/system/index.html 1 基本介绍 通过观察它的实际运行,你可以大致理解salt如何工作.这意味着,在控 ...

  3. h5页面乱码-设置编码

    1.h5页面正常,重定向以后出现乱码,如图所示. 解决办法:重定向的时候 需要设置编码. 2.文件charset已经是utf-8,页面还是乱码,文件保存的格式也要是utf-8的哦

  4. HDU - 1068 Girls and Boys(二分匹配---最大独立集)

    题意:给出每个学生的标号及与其有缘分成为情侣的人的标号,求一个最大集合,集合中任意两个人都没有缘分成为情侣. 分析: 1.若两人有缘分,则可以连一条边,本题是求一个最大集合,集合中任意两点都不相连,即 ...

  5. jdk 的安装教程

    1. 配置的位置 鼠标右击计算机----属性-----高级系统设置---环境变量----在系统变量中配置(推荐) 2.增加环境变量 (1)新建 -----  变量名:   JAVA_HOME 变量值 ...

  6. dmidecode 硬件

    dmidecode -t Memory   #查内存 Maximum Capacity: 4 GB  #最大4G         Number Of Devices: 2   #2个插槽 服务器到底能 ...

  7. BZOJ 3170 [Tjoi2013]松鼠聚会

    题解:切比雪夫距离转化为曼哈顿距离 枚举源点,横纵坐标互不影响,分开考虑,前缀和优化 横纵分开考虑是一种解题思路 #include<iostream> #include<cstdio ...

  8. 【每日Scrum】第三天冲刺

    一.计划会议内容 登录和个人主界面开发布局实现. 二.任务看板 三.scrum讨论照片 四.产品的状态 登录与个人界面布局实现 五.任务燃尽图  

  9. UML-GoF设计模式-总结

    1.GRASP 2.设计模式

  10. 洛谷P1257(暴力超时)

    1.先输入再求勾股定理会超时 2.需要一边输入一边求. #include<iostream> #include<cmath>#include<cstdio> usi ...