2 seconds
256 megabytes
standard input
standard output
The new "Die Hard" movie has just been released! There are n people at the cinema box office standing in a huge line. Each of them has a single 100, 50 or 25 ruble
bill. A "Die Hard" ticket costs 25 rubles. Can the booking clerk sell a ticket to each person and give the change if he initially has no money and sells the tickets
strictly in the order people follow in the line?
The first line contains integer n (1 ≤ n ≤ 105) —
the number of people in the line. The next line contains n integers, each of them equals25, 50 or 100 —
the values of the bills the people have. The numbers are given in the order from the beginning of the line (at the box office) to the end of the line.
Print "YES" (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print "NO".
4 25 25 50 50
YES
2 25 100
NO
4 50 50 25 25
NO
/* * Author: ***** * Created Time: 2013/9/27 23:04:50 * File Name: F.cpp * solve: F.cpp */ #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> #include<string> #include<map> #include<stack> #include<set> #include<iostream> #include<vector> #include<queue> //ios_base::sync_with_stdio(false); //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define sz(v) ((int)(v).size()) #define rep(i, a, b) for (int i = (a); i < (b); ++i) #define repf(i, a, b) for (int i = (a); i <= (b); ++i) #define repd(i, a, b) for (int i = (a); i >= (b); --i) #define clr(x) memset(x,0,sizeof(x)) #define clrs( x , y ) memset(x,y,sizeof(x)) #define out(x) printf(#x" %d\n", x) #define sqr(x) ((x) * (x)) typedef long long LL; const int INF = 1000000000; const double eps = 1e-8; const int maxn = 30000; int sgn(const double &x) { return (x > eps) - (x < -eps); } int used[200]; int main() { //freopen("in.txt","r",stdin); int n; scanf("%d",&n); clr(used); int flag = 1; rep(i,0,n) { int a; scanf("%d",&a); if(a == 25) { used[25]++; continue; } if(a == 50 ) { used[50]++; if(used[25]) { used[25]--; continue; } else { flag = 0; break; } } if(a == 100) { used[100]++; if(used[25] && used[50]) { used[25]--; used[50]--; continue; } if(used[25] == 0) { flag = 0; break; } if(used[25]<3 && used[50] == 0) { flag = 0; break; } if(used[25] >= 3) used[25] -= 3; } } if(flag) cout<<"YES\n"; else cout<<"NO\n"; return 0; }