Pay Back(模拟)
时间:2019-09-09
本文章向大家介绍Pay Back(模拟),主要包括Pay Back(模拟)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
链接: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.
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
输入
5 100 -200 250 -200 200
输出
9
题目概述:
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+7; 15 const int maxn=1e5+10; 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=1;i<=n;i++) 25 { 26 scanf("%d",&A[i]); 27 } 28 int money=0; 29 int pace=0; 30 int flag; 31 for(int i=1;i<=n;i++) 32 { 33 pace++; 34 if(money>=0&&money+A[i]<0)//从不欠钱到欠钱,记录下来这个要还钱的最远点 35 { 36 flag=i; 37 } 38 else if(money<0&&money+A[i]>=0)//从欠钱到不欠钱,说明已有能力还清前面所有点的钱,马上回去还钱 39 { 40 pace+=(i-flag)*2;//一来一回 41 } 42 money+=A[i]; 43 } 44 printf("%d\n",pace); 45 return 0; 46 }
原文地址:https://www.cnblogs.com/jiamian/p/11494969.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 详解Ubuntu16.04启动器图标异常解决方法
- Linux(ubuntu)下实现增加/删除文件权限
- Vim中文件编码处理与重新打开乱码文件详解
- Linux常用命令mkdir详解
- CentOS6.5环境下使用rsync增量同步备份文件的方法
- Linux 搭建Git服务器的方法
- Linux中无法远程连接数据库问题的解决方法
- VIM实现文件快速跳转插件详解
- 详解linux usb host驱动编写入门
- CentOS服务器平台搭建mysql主从复制与读写分离的方法
- Centos7中添加、删除Swap交换分区的方法
- Bash中文件描述符的详细介绍
- Linux 块设备驱动代码编写
- ubuntu中终端命令提示符太长的修改方法汇总
- CentOS 6.5 环境实现本地局域网搭建YUM的方法【基于FTP】