Codeforces Round #724 (Div. 2)A. Omkar and Bad Story
给一个数组, 问是否可以组成一个数组, 里面的元素包含其中任意两个元素相减的绝对值.
这题, 我上来以为是等差数列什么的呢…结果就是暴力求解..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
static class TaskA { public void solve(int testNumber, InputReader in, OutputWriter out) { int n = in.readInt(); int[] ary = in.readIntArray(n); Set<Integer> set = new HashSet<>(); List<Integer> list = new ArrayList<Integer>(); for (int a : ary) { set.add(a); list.add(a); } boolean add = true; while (set.size() <= 300 && add) { add = false; for (int i = 0; i < list.size(); i++) { for (int j = i + 1; j < list.size(); j++) { int nn = Math.abs(list.get(i) - list.get(j)); if (!set.contains(nn)) { set.add(nn); list.add(nn); add = true; break; } } } } if (set.size() > 300) { out.printLine("NO"); } else { out.printLine("YES"); out.printLine(list.size()); for (int l : list) { out.print(l + " "); } out.printLine(); } } } |