A

来源:互联网 发布:java经典编程题 编辑:程序博客网 时间:2024/06/16 04:35

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?

Input
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 equals 25, 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.

Output
Print “YES” (without the quotes) if the booking clerk can sell a ticket to each person and give the change. Otherwise print “NO”.

Example
Input
4
25 25 50 50
Output
YES
Input
2
25 100
Output
NO
Input
4
50 50 25 25
Output
NO

import java.util.ArrayList;import java.util.HashMap;import java.util.Scanner;/** * Created by 95112 on 11/4/2017. */public class DieHard {    public static void main(String[] args)    {        Scanner scanner = new Scanner(System.in);        int n = scanner.nextInt();        int[] input = new int[n];        HashMap<Integer,Integer> map = new HashMap<>();        map.put(100,0);        map.put(50,0);        map.put(25,0);        boolean ok = true;        for (int i =0 ; i< n; i++)        {            input[i] = scanner.nextInt();            if (ok){                if (input[i] == 25){                    int tims = map.get(25)+1;                    map.put(25, tims);                }                else if (input[i] == 50){                    int time = map.get(25);                    if (time <= 0)                    {                        ok = false;                        continue;                    }                    else {                        map.put(25,time-1);                        time = map.get(50);                        time++;                        map.put(50,time);                    }                }                else {                    int time25 = map.get(25);                    int time50 = map.get(50);                    if (time50>=1 && time25 >= 1){                        map.put(25,time25-1);                        map.put(50,time50-1);                    }                    else if (time25 >= 3){                        map.put(25,time25 - 3);                    }                    else {                        ok = false;                    }                }            }        }        if (ok)            System.out.println("YES");        else            System.out.println("NO");    }}
原创粉丝点击