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, KF1F2F3F4, 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

Input

34 0 1 0 0 09 4 1 0 0 08 0 0 1 0 0

Output

no

Input

39 4 1 0 0 04 0 1 0 0 08 0 0 1 0 0

Output

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[100005],one[100005],five[100005],ten[100005],twel[100005],fifth[100005];

int main()
{
    ll n;
    ll num1,num5,num10,num20,num50;
    while(cin>>n)
    {

        for(int i=0; i<n; ++i)
            scanf("%lld%lld%lld%lld%lld%lld",&money[i],&one[i],&five[i],&ten[i],&twel[i],&fifth[i]);

        if(one[0] + five[0]*5 + ten[0]*10 + twel[0]*20 + fifth[0]*50 > money[0]) //如果一开始就需要找钱,那直接GG
            cout<<"no"<<endl;

        else
        {
            num1=0,num5=0,num10=0,num20=0,num50=0;
            num1 += one[0];
            num5 += five[0];
            num10 += ten[0];
            num20 += twel[0];
            num50 += fifth[0];
            ll give,flag;
            for(int i=1; i<n; ++i)
            {
                flag = 0;
                give = (one[i] + five[i]*5 + ten[i]*10 + twel[i]*20 + fifth[i]*50) - money[i]; //计算需要找多少钱 

                if(give == 0) //如果不用找钱,那直接把收的钱加到总钱当中
                {
                    num1 += one[i];
                    num5 += five[i];
                    num10 += ten[i];
                    num20 += twel[i];
                    num50 += fifth[i];
                    continue;
                }
                else//如果要找钱
                {
                    while(num50 > 0 && give >= 50) //要找的钱比50多,且50的钱不为0,就用50的给,直到给不了为止
                    {
                        num50--;
                        give -= 50;
                    }
                    while(num20 > 0 && give >= 20) //剩下要找的钱比20多,且20的钱不为0,就用20的给,直到给不了为止
                    {
                        num20--;
                        give -= 20;
                    }
                    while(num10 > 0 && give >= 10) //剩下要找的钱比10多,且10的钱不为0,就用10的给,直到给不了为止
                    {
                        num10--;
                        give -= 10;
                    }
                    while(num5 > 0 && give >= 5) //剩下要找的钱比5多,且5的钱不为0,就用5的给,直到给不了为止
                    {
                        num5--;
                        give -= 5;
                    }
                    while(num1 > 0 && give >= 1) //剩下要找的钱比1多,且1的钱不为0,就用1的给,直到给不了为止
                    {
                        num1--;
                        give--;
                    }

                    if(give > 0) //如过还没找完,表示无法满足要求
                    {
                        flag = 1;
                        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 0;
}


原文地址:https://www.cnblogs.com/tuyang1129/p/9272953.html

时间: 2024-10-15 03:46:07

Gym - 100989H (贪心)的相关文章

hdu-5695 Gym Class(贪心+拓扑排序)

题目链接: Gym Class Time Limit: 6000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) Problem Description 众所周知,度度熊喜欢各类体育活动. 今天,它终于当上了梦寐以求的体育课老师.第一次课上,它发现一个有趣的事情.在上课之前,所有同学要排成一列, 假设最开始每个人有一个唯一的ID,从1到N,在排好队之后,每个同学会找出包括自己在内的前方所有同学的最小ID,作为

hdu 5695 Gym Class 贪心+拓扑

题目链接:hdu 5695 Gym Class 题目意思:每个同学会找出包括自己在内的前方所有同学的最小ID,作为自己评价这堂课的分,最后的排队结果可以使得所有同学的评价分数和最大 思路:贪心的策略是尽量把id大的放在前面,拓扑排序是为了挑出没有排队限制的且数目尽可能大的id放在前面 /************************************************************** Problem:hdu 5695 User: youmi Language: C++

UVaLive 6588 &amp;&amp; Gym 100299I (贪心+构造)

题意:给定一个序列,让你经过不超过9的6次方次操作,变成一个有序的,操作只有在一个连续区间,交换前一半和后一半. 析:这是一个构造题,我们可以对第 i 个位置找 i 在哪,假设 i  在pos 位置,那么如果 (pos-i)*2+i-1 <= n,那么可以操作一次换过来, 如果不行再换一种,如果他们之间元素是偶数,那么交换 i - pos,如果是奇数,交换 i - pos+1,然后再经过一次就可以换到指定位置. 代码如下: #pragma comment(linker, "/STACK:1

UESTC 2016 Summer Training #1 Div.2 H - Queue (A) 贪心

H - Queue (A) Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice Gym 100989H Description standard input/output After the data structures exam, students lined up in the cafeteria to have a drink and ch

2016-5-21 letwetell Round3 (百度之星初赛,dfs序)

halfapri(- o -)Y { 1.2016百度之星Round2A 题目链接 题解链接 1001 All X 循环节 1002 Sitting in Line 状压dp 1003 Snacks 1004 D Game 1005 BD String 找规律 1006 Gym Class 贪心+topo 2.2012多校第7场 hdu4366 Successor 线段树 + dfs序 }

codeforces Gym 100187F F - Doomsday 区间覆盖贪心

F. Doomsday Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/problem/F Description Doomsday comes in t units of time. In anticipation of such a significant event n people prepared m vaults in which, as they think, it will

Gym - 100338E Numbers 贪心

Gym - 100338E 题意:给你n,k问在1-n中能整出k的字典序最小的数.范围1018 思路:比较简单的贪心了,枚举10的幂m,然后加上k-m%k, 更新答案就可以了,数据一定要用unsigned long long,我就在这里挂了几次,查了半天. 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <c

Codeforces Gym 101174 A Within Arm&#39;s Reach 贪心 手臂

#include<iostream> #include<stdio.h> #include <string.h> #include <algorithm> #include <vector> #include <math.h> using namespace std; #define LL long long const int maxn=25; double a[maxn],l[maxn],r[maxn]; double ex,ey

GYM 101173 F.Free Figurines(贪心||并查集)

原题链接 题意:俄罗斯套娃,给出一个初始状态和终止状态,问至少需要多少步操作才能实现状态转化 贪心做法如果完全拆掉再重装,答案是p[i]和q[i]中不为0的值的个数.现在要求寻找最小步数,显然要减去一些多余的步数.如果初始的一些链的前端是终止的某一条链的连续的一部分,那么这条链就不用被拆开再连上,这样每一个长度为x的链对答案的贡献就是-2*(x-1),对每条链进行同样的操作之后就是答案 #include <iostream> #include<cstdio> #include<