二,RoundC

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Main {
  public static void main(String[] args) throws FileNotFoundException {
    FileInputStream fis = new FileInputStream("A-large.in");
    PrintStream out = new PrintStream(new FileOutputStream("A-large.out"));
    System.setIn(fis);
    System.setOut(out);
    Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
//    System.out.println((char)(65));
    int T = in.nextInt();
    for (int t = 1; t <= T; ++t) {
        int n = in.nextInt();
        HashMap<Character, Long> map = new HashMap();
        int start = 65;
        for (int i = 0; i < n; ++i) {
            map.put((char)(start++), in.nextLong());
        }
        // 用贪心每次都找最多的。
//        System.out.println(hash);
        ArrayList<String> res = new ArrayList();
        // 特判三人的时候
        String now = new String();
        while(!map.isEmpty()) {
            Long max = (long) 0;
            char cur = ' ';
            for (char c : map.keySet()) {
                if (map.get(c) > max) {
                    cur = c;
                    max = map.get(c);
                }
            }
            if (map.get(cur) == 1) {
                map.remove(cur);
            } else {
                map.put(cur, map.get(cur) - 1);
            }
            
            now += cur;
            if (now.length() == 2 || map.isEmpty()) {
                res.add(now);
                now = "";
            }
        }
        if (res.size() != 1 && res.get(res.size() - 1).length() == 1) {
            // swap
            String a = res.get(res.size() - 1);
            String b = res.get(res.size() - 2);
            res.set(res.size() - 1, b);
            res.set(res.size() - 2, a);
        }
        System.out.print("Case #" + t + ":" );
        for (String s : res) {
            System.out.print(" " + s);
        }
        System.out.println();
        
    }
  }
}
View Code

相关文章:

  • 2022-12-23
  • 2022-01-05
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-01
  • 2021-08-27
  • 2022-02-17
猜你喜欢
  • 2021-08-11
  • 2021-09-13
  • 2022-02-04
  • 2021-09-01
  • 2021-07-11
  • 2021-07-12
  • 2021-11-04
相关资源
相似解决方案