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个数 然后你需要 ...
随机推荐
- JNI(java Native Interface)
参看: http://blog.csdn.net/xw13106209/article/details/6989415
- bash中常用的快捷键
常用快捷键 ctrl+c 强制终止当前命令 ctrl+l 清屏(clear) ctrl+a 光标移动到命令行行首 ctrl+e 光标移动到命令行行尾 ctrl+u 从光标所在位置删除到行首 ctrl+ ...
- mac 使用svn记录
checkout project : svn checkout svn://127.0.0.1/repository --username=username --password=password ...
- iframe callback方式文件上传
1.前端default.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "h ...
- python开发_python中的list操作
对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...
- jquery 三元运算
三元运算: 条件 ? 条件为真取此值 : 条件为假取此值; var v = $(:check).prop('checked')?faule:true; $(:check).prop('checked ...
- django网页的分页功能,大家如果有疑问请留言
url文件 from django.contrib import admin from django.conf.urls import url from app01 import views urlp ...
- jq循环方法
jq在前台使用很广泛,其中不可避免会用到循环,for循环可以,但有时不方便,下边举两个jq循环: 第一个: $.each(数组或对象,function(index,item){ //循环的逻辑代码 } ...
- WOW研究资料收集
1,模拟服务器:trinity core, sunwell core等 参考:逍遥魔兽 2,大芒果:通用网游框架,带了WOW的模拟模块 参考资料: 大芒果论坛http://www.mangoscn.c ...
- windows下Mysql5.7.10免安装版配置
免安装配置: 在环境变量 Path 中追加 %mysql_home%\bin; 配置mysql目录下的 my-default.ini 文件,在mysql 根目录下新建 data 文件夹 使用管理员权限 ...