CF1415D XOR-gun

思路:

需要根据数据范围的不同设计不同的算法,若数组长度大于2(log2109+1)=60,可以构造出只需做一次XOR操作的解;否则暴力枚举求解即可。要善于发现隐含条件,有的时候数据范围可能没有看起来的那么大。

实现:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 100005;
 4 int a[N];
 5 
 6 int main()
 7 {
 8     int n;
 9     while (cin >> n)
10     {
11         for (int i = 0; i < n; i++) cin >> a[i];
12         if (n > 100) cout << 1 << endl;
13         else
14         {
15             int res = n;
16             for (int i = 0; i < n; i++)
17             {
18                 int l = 0;
19                 for (int j = i; j < n; j++)
20                 {
21                     l ^= a[j];
22                     int r = 0;
23                     for (int k = j + 1; k < n; k++)
24                     {
25                         r ^= a[k];
26                         if (l > r) res = min(res, k - i - 1);
27                     }
28                 }
29             }
30             cout << (res == n ? -1: res) << endl;
31         }
32     }
33     return 0;
34 }
原文地址:https://www.cnblogs.com/wangyiming/p/14758968.html