Codeforces Round #310 (Div. 2) B. Case of Fake Numbers

原题:http://codeforces.com/contest/556/problem/B


题目大意: n个数字组成的数组, 如果是已经排序的0.1.2.3…n 返回yes, 如果不是, 偶数的数变大,奇数的数变小, 不管大小, 都绕着n转,问能否转出来0.1.2.3..n


分析: n才1000, hash一下放hashset里. 转的时候, 多一步+n再取模, 防止负数

 public void solve(int testNumber, InputReader in, OutputWriter out) {
        int n = in.readInt();
        int[] nums = IOUtils.readIntArray(in, n);
        HashSet<Long> hashSet = new HashSet<>();
        int[] ary = new int[n];
        for (int i = 0; i < n; i++) {
            ary[i] = i;
        }
        long res = hash(ary);
        for (int k = 0; k < 1005; k++) {
            long h = hash(nums);
            for (int i = 0; i < nums.length; i++) {
                if (i % 2 == 0) {
                    nums[i]++;
                } else {
                    nums[i]--;
                }
                nums[i] += n;
                nums[i] %= n;
            }
            long c = hash(nums);
            if (res == c) {
                out.print("Yes");
                return;
            }
            if (hashSet.contains(c)){
                out.print("No");
                return;
            }
        }

        out.print("No");

    }