链接: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

输入


-

-

输出

 
 

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

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

 #include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#include <math.h>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <math.h>
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+;
const int maxn=1e5+;
using namespace std; int A[maxn]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&A[i]);
}
int money=;
int pace=;
int flag;
for(int i=;i<=n;i++)
{
pace++;
if(money>=&&money+A[i]<)//从不欠钱到欠钱,记录下来这个要还钱的最远点
{
flag=i;
}
else if(money<&&money+A[i]>=)//从欠钱到不欠钱,说明已有能力还清前面所有点的钱,马上回去还钱
{
pace+=(i-flag)*;//一来一回
}
money+=A[i];
}
printf("%d\n",pace);
return ;
}
 
 
 

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. bugku-Web 求getshell

    求getshell 上传一个png文件发现提示My name is margin,give me a image file not a php 然后也尝试上传php文件,但是依然不行,这里显然对文件类 ...

  2. 字符串专题之KMP算法

    写点自己对KMP的理解,我们有两个字符串A和B,求A中B出现了多少次. 这种问题就可以用KMP来求解. 朴素的匹配最坏情况是O(n^2)的.KMP是个高效的算法,效率是O(n)的. KMP算法的思想是 ...

  3. axios请求接口的时候带一个参数

    getHomeInfo () { this.axios.get('/api/index.json?city=' + this.city) .then(this.getHomeInfoSucc) } 尽 ...

  4. JAVA基础——使用配置文件

    一. 前言         日常我们做项目中,我们经常会遇到这样的情况:由于开发环境和生产环境的不同,项目部署在生产环境之前,有些参数我们并不知道如何取值.例如:数据库链接设定,我们在部署生产环境之前 ...

  5. VS2013 配置Mysql

    1.添加mysql.h 对着项目右键,选择properties 双击C/C++,选择General,看到Additional Include Directories 编辑 点击文件夹的按钮 点击... ...

  6. Jlink线序问题

  7. 18 12 25 css 基本语法以及页面使用

    css的定义方法是: 选择器 { 属性:值; 属性:值; 属性:值;} 选择器是将样式和页面元素关联起来的名称,属性是希望设置的样式属性每个属性有一个或多个值 css页面引入方法: 1.外联式:通过l ...

  8. 吴裕雄--天生自然MySQL学习笔记:MySQL DELETE 语句

    可以使用 SQL 的 DELETE FROM 命令来删除 MySQL 数据表中的记录. 可以在 mysql> 命令提示符或 PHP 脚本中执行该命令. 语法 以下是 SQL DELETE 语句从 ...

  9. protobuf 的enum与string转换

    c/c++ enum 介绍 说起c/c++ 的enum,比起python 真的是方便简洁 enum type{ type1 = 0, type2 } enum的元素对应的int 默认从0 开始依次增加 ...

  10. mysql not in 或 in 优化

    在MySQL 中,not in 或in 优化思路, 利用left join 来优化,类似如下的查询方式: select id from a where id in (select id from b ...