(打表找循環節)CodeForces - 768C Jon Snow and his Favourite Number

C. Jon Snow and his Favourite Number
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Jon Snow now has to fight with White Walkers. He has n rangers, each of which has his own strength. Also Jon Snow has his favourite number x. Each ranger can fight with a white walker only if the strength of the white walker equals his strength. He however thinks that his rangers are weak and need to improve. Jon now thinks that if he takes the bitwise XOR of strengths of some of rangers with his favourite number x, he might get soldiers of high strength. So, he decided to do the following operation k times:

  1. Arrange all the rangers in a straight line in the order of increasing strengths.
  2. Take the bitwise XOR (is written as ) of the strength of each alternate ranger with x and update it's strength.
Suppose, Jon has 5 rangers with strengths [9, 7, 11, 15, 5] and he performs the operation 1 time with x = 2. He first arranges them in the order of their strengths, [5, 7, 9, 11, 15]. Then he does the following:
  1. The strength of first ranger is updated to , i.e. 7.
  2. The strength of second ranger remains the same, i.e. 7.
  3. The strength of third ranger is updated to , i.e. 11.
  4. The strength of fourth ranger remains the same, i.e. 11.
  5. The strength of fifth ranger is updated to , i.e. 13.
The new strengths of the 5 rangers are [7, 7, 11, 11, 13]

Now, Jon wants to know the maximum and minimum strength of the rangers after performing the above operations k times. He wants your help for this task. Can you help him?

Input

First line consists of three integers n, k, x (1 ≤ n ≤ 105, 0 ≤ k ≤ 105, 0 ≤ x ≤ 103) — number of rangers Jon has, the number of times Jon will carry out the operation and Jon's favourite number respectively.

Second line consists of n integers representing the strengths of the rangers a1, a2, ..., an (0 ≤ ai ≤ 103).

Output

Output two integers, the maximum and the minimum strength of the rangers after performing the operation k times.

Examples
Input
5 1 2
9 7 11 15 5
Output
13 7
Input
2 100000 569
605 986
Output
986 605


1、升序排序;
2、對奇數位的數字與x做異或運算。
輸出k次操作後的最大值和最小值。

思路:直接模擬的話超時,打表發現存在循環節,那麼直接找循環節就行了。

#include <cstdio>
#include <iostream>
#include<cstring>
#include <algorithm>

using namespace std;

int a[100000+50];
int ans[50][100000+50], n;

int Judge(int x) // 返回循環節點
{
    for(int i = 0 ; i < x; i++) {
        bool flag = true;
        for(int j = 0; j < n; j++) {
            if(ans[i][j] != ans[x][j]) {
                flag = false;
                break;
            }
        }
        if(flag) return i;
    }
    return -1;
}

int main()
{
    int  k, x;
    while(cin >> n >> k >> x) {
        for(int i = 0; i < n; i++) {
            cin >> a[i];
            ans[0][i] = a[i];
        }
        int node, i;
        for(i = 1; i <= k; i++) {
            sort(a, a + n);
            for(int j = 0; j < n; j++) {
                if(j%2 == 0) a[j] ^= x;
                ans[i][j] = a[j];
            }
            node = Judge(i);
            if(node != -1) break;
        }
        int t;
        if(k > i) {
            int T = i - node; // T即週期
            int fin = (k - i) % T; // 週期中第幾個
            t = fin + node; // 記得加上循環節節點
        }
        else t = k;
        int maxx = -1e9;
        int minn = 1e9;
        for(int i  = 0; i < n; i++) {
            minn = min(minn, ans[t][i]);
            maxx = max(maxx, ans[t][i]);
        }
        cout << maxx << ' ' << minn << endl;
    }
}

網上還有一種做法說最後最大值和最小值會趨於穩定,找最後最大和最小不變的做法,也能AC,但我打表找不出來,暫時也想不出這種做法的正確性。

#include <cstdio>
#include <iostream>
#include<cstring>
#include <algorithm>

using namespace std;

int a[100000+50];
int ans[50][100000+50];
int maxx[100000], minn[100000];
int main()
{
    int n, k, x;
    while(cin >> n >> k >> x) {
        for(int i = 0; i < n; i++)
            cin >> a[i];
        sort(a, a + n);
        maxx[0] = a[n-1];
        minn[0] = a[0];
        for(int i = 1; i <= k; i++) {
            sort(a, a + n);
            maxx[i] = a[n-1];
            minn[i] = a[0];
            if(i >= 3 && maxx[i]==maxx[i-1] && maxx[i]==maxx[i-2] && maxx[i]==maxx[i-3] && minn[i]==minn[i-1] && minn[i]==minn[i-2] && minn[i]==minn[i-3]) {
                break;
            }
            for(int j = 0; j < n; j += 2)
                a[j] ^= x;
        }
        sort(a, a + n);
        cout << a[n-1] << ' ' << a[0] << endl;
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章