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个数 然后你需要 ...
随机推荐
- mysql清理连接
关闭指定ip的连接: for i in $(mysql -uusername -ppassword -Bse "select * from information_schema.proces ...
- Webstorm2018破解笔记
参考链接: (1)https://www.jb51.net/softs/598714.html 过程步骤 (2)http://idea.lanyus.com/ IntelliJ IDEA 注册 ...
- 比XGBOOST更快--LightGBM介绍
xgboost的出现,让数据民工们告别了传统的机器学习算法们:RF.GBM.SVM.LASSO.........现在,微软推出了一个新的boosting框架,想要挑战xgboost的江湖地位.笔者尝试 ...
- C#如何消除绘制图形缩放时抖动,总结
一.手动双缓冲 首先定义一个BitmapBitmap backBuffer = new Bitmap(画布宽度, 画布高度);然后获取这个Bitmap的GraphicsGraphics graphic ...
- JSTL之C标签的用法
转自:https://my.oschina.net/zimingforever/blog/78980 最近开始整理以前的onenote,居然有200多篇,大致翻了下,很多内容都是在大学的时候学习的时候 ...
- UGUI BUG
UNITY UGUI问题:父类使用 GroupLayout,子类使用contentsize filter时,会出现运行时布局重叠,但隐藏后再显示就会好了.
- ubuntu linux常用指令(1)
序号 命令 说明 1 sudo su 从普通用户切换到root用户 2 su user 从root用户切换到普通用户 3 ls 列出当前目录的文件和目录,但是不包括隐藏文件和目录 4 ls -a 列出 ...
- C# 文本文件的读写
// *********************************************************************** // Assembly : XXX // Auth ...
- 01 lucene基础 北风网项目培训 Lucene实践课程 系统架构
Lucene在搜索的时候数据源可以是文件系统,数据库,web等等. Lucene的搜索是基于索引,Lucene是基于前面建立的索引之上进行搜索的. 使用Lucene就像使用普通的数据库一样. Luce ...
- MonoBehaviour.print和Debug.Log是同样的作用
MonoBehaviour.print("identical------------------------");