Gym - 100989H (贪心)
After the data structures exam, students lined up in the cafeteria to have a drink and chat about how much they have enjoyed the exam and how good their professors are. Since it was late in the evening, the cashier has already closed the cash register and does not have any change with him.
The students are going to pay using Jordanian money notes, which are of the following types: 1, 5, 10, 20, 50.
Given how much each student has to pay, the set of notes he’s going to pay with, and the order in which the students arrive at the cashier, your task is to find out if the cashier will have enough change to return to each of the student when they arrive at the cashier.
Input
The first line of input contains a single integer N (1 ≤ N ≤ 105), the number of students in the queue.
Each of the following N lines describes a student and contains 6 integers, K, F1, F2, F3, F4, and F5, where K represents the amount of money the student has to pay, and Fi (0 ≤ Fi ≤ 100) represents the amount of the ith type of money this student is going to give to the cashier.
The students are given in order; the first student is in front of the cashier.
It is guaranteed that no student will pay any extra notes. In other words, after removing any note from the set the student is going to give to the cashier, the amount of money will be less than what is required to buy the drink.
Output
Print yes if the cashier will have enough change to return to each of the students when they arrive in the given order, otherwise print no.
Examples
3
4 0 1 0 0 0
9 4 1 0 0 0
8 0 0 1 0 0
no
3
9 4 1 0 0 0
4 0 1 0 0 0
8 0 0 1 0 0
yes 题意:第一行一个n,表示有n个人排队买东西付钱,接下来n行,每行6个数,第一个表示要付多少钱,接下来的5个数表示他付的1块,5块,10块,20,50的数量,每个人在付钱的时候可能要找钱,一开始你一分钱都没有,问你能不能实现每个人都能找钱。
题解:运用贪心的思想,越小的钱越好操作,所以在找钱的时候尽量选择较大的金额,留下较小的金额。然后判断每个人的时候,应该在没有加上他的钱的基础上判断,因为如果你在买东西的时候对方没钱找,是不能交易的,他也得不到你得钱,就不能用你的钱找给你。
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std; ll money[],one[],five[],ten[],twel[],fifth[]; int main()
{
ll n;
ll num1,num5,num10,num20,num50;
while(cin>>n)
{ for(int i=; i<n; ++i)
scanf("%lld%lld%lld%lld%lld%lld",&money[i],&one[i],&five[i],&ten[i],&twel[i],&fifth[i]); if(one[] + five[]* + ten[]* + twel[]* + fifth[]* > money[]) //如果一开始就需要找钱,那直接GG
cout<<"no"<<endl; else
{
num1=,num5=,num10=,num20=,num50=;
num1 += one[];
num5 += five[];
num10 += ten[];
num20 += twel[];
num50 += fifth[];
ll give,flag;
for(int i=; i<n; ++i)
{
flag = ;
give = (one[i] + five[i]* + ten[i]* + twel[i]* + fifth[i]*) - money[i]; //计算需要找多少钱 if(give == ) //如果不用找钱,那直接把收的钱加到总钱当中
{
num1 += one[i];
num5 += five[i];
num10 += ten[i];
num20 += twel[i];
num50 += fifth[i];
continue;
}
else//如果要找钱
{
while(num50 > && give >= ) //要找的钱比50多,且50的钱不为0,就用50的给,直到给不了为止
{
num50--;
give -= ;
}
while(num20 > && give >= ) //剩下要找的钱比20多,且20的钱不为0,就用20的给,直到给不了为止
{
num20--;
give -= ;
}
while(num10 > && give >= ) //剩下要找的钱比10多,且10的钱不为0,就用10的给,直到给不了为止
{
num10--;
give -= ;
}
while(num5 > && give >= ) //剩下要找的钱比5多,且5的钱不为0,就用5的给,直到给不了为止
{
num5--;
give -= ;
}
while(num1 > && give >= ) //剩下要找的钱比1多,且1的钱不为0,就用1的给,直到给不了为止
{
num1--;
give--;
} if(give > ) //如过还没找完,表示无法满足要求
{
flag = ;
cout<<"no"<<endl;
break;
}
else //否则,表示可以找钱
{
num1 += one[i];
num5 += five[i];
num10 += ten[i];
num20 += twel[i];
num50 += fifth[i];
}
}
}
if(!flag) cout<<"yes"<<endl;
}
}
return ;
}
Gym - 100989H (贪心)的更多相关文章
- hdu-5695 Gym Class(贪心+拓扑排序)
题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- HDU 5965 Gym Class 贪心+toposort
分析:就是给一些拓补关系,然后求最大分数,所以贪心,大的越靠前越好,小的越靠后越好 剩下的就是toposort,当然由于贪心,所以使用优先队列 #include <iostream> #i ...
- Ice Cream Tower Gym - 101194D (贪心 + 二分 )
题目链接 : https://cn.vjudge.net/problem/Gym-101194D 题目大意 : 给你n个冰激凌球,让你用这些冰激凌球去垒冰激凌,要求是下面的这一个必须是他上面一个的两倍 ...
- Gym - 100989H
After the data structures exam, students lined up in the cafeteria to have a drink and chat about ho ...
- Pond Cascade Gym - 101670B 贪心+数学
题目:题目链接 思路:题目让求最下面池子满的时间和所有池子满的时间,首先我们考虑所有池子满的时间,我们从上到下考虑,因为某些池子满了之后溢出只能往下溢水,考虑当前池子如果注满时间最长,那么从第一个池子 ...
- UVaLive 6588 && Gym 100299I (贪心+构造)
题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i 在pos 位置, ...
- 贪心 Gym 100502E Opening Ceremony
题目传送门 /* 题意:有一堆砖块,每一次操作可以选择消去任意一行,也可以选择消去任意一列.求要消去所有的砖块需要最小的操作数 贪心:首先清楚的是消去最高列的最佳,消去第一行最佳,行列的顺序只对中间过 ...
- Codeforces Gym 100803C Shopping 贪心
Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
随机推荐
- [Python] Regular Expressions
1. regular expression Regular expression is a special sequence of characters that helps you match or ...
- neovim在win10下安装配置
安装 github安装包地址 在Windows的Pre-built archives下 点击nvim-win64.zip,下载. 解压安装包,放入适合的文件中,比如 D:\Editor中. 双击 nv ...
- Arduino UNO 键盘记录器中时钟接到2口或3口,其它接口不行。马上就要放弃了。要修改例子中时钟的引脚。
- MySQL系统时间函数NOW(),CURRENT_TIMESTAMP(),SYSDATE()的区别
CURRENT_TIMESTAMP是NOW的同义词,也就是说两者是相同的. SYSDATE函数返回的是执行到当前函数时的时间,而NOW返回的是执行SQL语句时的时间. 测试语句: SELECT NOW ...
- Thread(线程)三
今天我们继续接着线程讲讲,上一章提到一下task概念, 首先接着task继续往下讲,在前章节提到过Thread怎么实现其他线程完成后再让主线程继续执行的功能,那么如果Task也需要线程等待事件,该怎么 ...
- 在Spring中使用Redis Lua脚本批量删除缓存
背景 之前分享了一篇利用lua脚本批量删除redis的key的文章.现在项目中我打算使用spring的缓存,而Spring缓存以前我是用ehcache来做实现的.没发现什么问题..这次我换成redis ...
- Spark之 使用SparkSql操作Hive的Scala程序实现
依赖 <dependency> <groupId>org.apache.spark</groupId> <artifactId>spark-hive_2 ...
- 使用CreateJS绘制数字盒子
- ios学习杂记
commond + alt + enter Xcode分屏.拖动xib连线
- Activiti 乱码问题
新版本运行起来中文看起来正常了许多,至少生成图片过程中不会出现乱码了,但不出预料的再次遇到部署时乱码问题,除了保存时报错,还会导致流程实例的节点名称是乱码. 1. IE报错定义时错误提示: 元素类型 ...