Pay Back(模拟)
链接:https://ac.nowcoder.com/acm/contest/1086/C
题目描述
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(模拟)的更多相关文章
- python接口测试,mock模块基本使用介绍
mock简介 py3已将mock集成到unittest库中 为的就是更好的进行单元测试 简单理解,模拟接口返回参数 通俗易懂,直接修改接口返回参数的值 mock作用 解决依赖问题,达到解耦作用 当我们 ...
- python_Mock基本使用
## 1.mock简介 1. py3已将mock集成到unittest库中 2. 为的就是更好的进行单元测试 3. 简单理解,模拟接口返回参数 4. 通俗易懂,直接修改接口返回参数的值 5. 官方文档 ...
- Codeforces 740A. Alyona and copybooks 模拟
A. Alyona and copybooks time limit per test: 1 second memory limit per test: 256 megabytes input: st ...
- iOS Apple Pay
iOS 苹果支付 需要证书支持支付功能 targets 打开支付功能按钮 //ApplePay#import <PassKit/PassKit.h> ...
- Apple Pay
Apple Pay运行环境:iPhone6以上设备,操作系统最低iOS9.0以上,部分信息设置需要iOS9.2以上.目前还不支持企业证书添加. 环境搭建好后可以在模拟器上面运行,xcode7.2.1+ ...
- HDU 4884 TIANKENG’s rice shop (模拟)
TIANKENG's rice shop 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/J Description TIANKE ...
- JS5模拟类
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 2017.07.11【NOIP提高组】模拟赛B组
Summary 今天的比赛打得还不错,第一题被同桌灌输的贪心,纯模拟洗脑了,然后steal的看了一下,发现怎么也对不了,一直在检查.最后10分钟才找出反例,推出动态规划方程,没有想到怎么转移,比赛就结 ...
- 用DDD模拟案例分析
之前我写了几篇关于DDD的介绍和一些小例子说明,我想这对于介绍DDD还是有些模糊,甚至还不知道怎么用DDD来分析设计.昨天和园友讨论也发现没有例子很难说明,所以今天我模拟了一个案例,同时这个案例也是真 ...
随机推荐
- SpringBoot学习(四)——配置文件占位符
RandomValuePropertySource:配置文件中可以使用随机数 ${Random.value} ${random.int}, ${random.long}, ${random.int( ...
- LIS是什么?【标本分拣】
接之前[LIS是什么?]中,提到几点需要补充描述的部分. Ⅰ.标本分管处理 标本的分管处理,在医院和第三方实验室有多种叫法,例如:分拣.合管等等.这里我称之为分拣,分拣实际上分为两个部分:系统中标本分 ...
- CentOS 7设置开机启动服务,添加自定义系统服务
由于版本的迭代,最近刚刚接触 CentOS 7,各种蛋疼 发现跟以前用的CentOS 6有着一些本质上的差别,连启动服务都不会启动了,一怒之下自己找资料,于是有了这篇文章... 1.建立服务文件 ...
- vue中的axios请求
1.get请求 // get this.$axios.get('请求路径') .then(function (response) { console.log(response); // 成功 }) . ...
- POJ 1651:Multiplication Puzzle 矩阵相乘式DP
Multiplication Puzzle Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7118 Accepted: ...
- opencv vs2013提示缺少Qedit.h问题
#pragma include_alias( "dxtrans.h", "qedit.h" ) #define __IDxtCompositor_INTERFA ...
- 福州大学2020年春软工实践W班第一次作业
作业描述 这个作业属于哪个课程 福州大学2020年春软工实践W班 这个作业要求在哪里 寒假作业(1/2) 这个作业的目标 建立博客.回顾,我的初心.当下和未来.学习路线 作业正文 福州大学2020年春 ...
- 经典线段树 UVALive 3938/UVA 1400
题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...
- KL散度与JS散度
1.KL散度 KL散度( Kullback–Leibler divergence)是描述两个概率分布P和Q差异的一种测度.对于两个概率分布P.Q,二者越相似,KL散度越小. KL散度的性质:P表示真实 ...
- update-help : 无法更新带有 UI 区域性 {zh-CN} 的模块“WindowsUpdateProvider”帮助: 在 HelpInfo XML 文件中检索不到 UI 区域性 zh-CN
环境 OS: Windows10 企业版 LTSC x64 CPU: Intel i5-7500 CPU 3.4GHz PowerShell:5.1.17763.503 描述 更新powershell ...