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个数 然后你需要 ...
随机推荐
- window下boost库
1.下载boost开发库源码. 2.使用vs2008的命令行工具,进入到源码目录xxx/boost_1_58_0,命令行中运行bootstrap.bat,生成文件b2.exe,在命令行中执行b2.ex ...
- 短URL链接系统
定义: 短网址(Short URL),顾名思义就是在形式上比较短的网址.但不知道有多少人像我一样,由于面试问道才知道有这种系统而对短连接原理好奇,从而进行进一步的研究.在Web 2.0的今天,不得不说 ...
- leetcode914
public class Solution { public bool HasGroupsSizeX(int[] deck) { var len = deck.Length; ; i <= le ...
- Java安全框架 Apache Shiro学习-1-ini 配置
简单登录流程: 1. SecurityManager 2. SecurityUtils.setSecurityManager 3. SecurityUtils.getSubject ...
- spring-boot 外部jar 打包 配置
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactI ...
- iss 默认上传大小为30 M
在 Windows7(iis7.5).Win2008(iis 7.0)和Win2003(iis 6.0) 中,默认设置是特别严格和安全的,这样可以最大限度地减少因以前太宽松的超时和限制而造成的攻击.指 ...
- linux安装mysql服务分两种安装方法:
linux安装mysql服务分两种安装方法: ①源码安装,优点是安装包比较小,只有十多M,缺点是安装依赖的库多,安装编译时间长,安装步骤复杂容易出错: ②使用官方编译好的二进制文件安装,优点是安装速度 ...
- 消息队列—ActiveMQ
1. 学习计划 1.什么是MQ 2.MQ的应用场景 3.ActiveMQ的使用方法. 4.使用消息队列实现商品同步. 2. 同步索引库分析 方案一:在manager(后台)中,添加商品的业务逻 ...
- 解决SharePoint下载文件时自动修改扩展名的问题
今天,有人告诉了我一个有趣的问题.当用户将一个扩展名为.ai的文件(Adobe illustrator格式的文件)上载到SharePoint 2013文档库中之后,点击它下载时,下载提示栏所显示的文件 ...
- 解剖Nginx·模块开发篇(2)ngx_http_hello_world_module 模块基本结构定义
elloWorld 是一个典型的 location 模块.什么是 location 模块?在 Nginx 中,根据作用域,有 main 模块.server 模块.location 模块. 1 模块定义 ...