A:
题意:给你 a,b俩个集合 求任意一个 a[i] + b[j] 不属于俩个集合。
思路:俩集合全部丢set 然后暴力枚举每个和。
AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 int a[500], b[500];
4 set<int> st;
5 int main()
6 {
7 int n, m;
8 cin >> n;
9 for(int i = 0;i < n;i++)
10 {
11 cin >> a[i];
12 st.insert(a[i]);
13 }
14 cin >> m;
15 for(int i = 0;i < m;i++)
16 {
17 cin >> b[i];
18 st.insert(b[i]);
19 }
20 for(int i = 0;i < n;i++)
21 for(int j = 0;j < m;j++)
22 {
23 if(st.count(a[i] + b[j]) == 0)
24 {
25 cout << a[i] << " " << b[j];
26 return 0;
27 }
28 }
29 return 0;
30 }
B:
题意:让通过增减1的方式所有元素乘积等于1;
思路:贪心,正数直接到 1 ,负数到-1,如果最后乘积为 -1并且 0 的个数为0则答案加 2,否则答案 + 0的个数。
AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int maxn = 1e5 +5;
4 long long a;
5 int main()
6 {
7 long long sum = 1;
8 long long ans = 0;
9 long long zero = 0;
10 int n;
11 cin >> n;
12 for(int i = 0;i < n;i++)
13 {
14 cin >> a;
15 if(a > 0)
16 {
17 ans += abs(a - 1);
18 sum *= 1;
19 }
20 else
21 if(a == 0) zero ++;
22 else{
23 ans += abs( - 1 - a);
24 sum *= -1;
25 }
26 }
27 if(sum == 1) cout << ans + zero << endl;
28 else
29 {
30 if(zero) cout << ans + zero << endl;
31 else cout << ans + 2LL;
32 }
33 return 0;
34 }
C:
这题我不会,看着图解随便猜的,结果就过了,偶数为 NO ,奇数对称来回摆。
AC代码
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int maxn = 2e5 + 5;
4 int a[maxn],b[maxn];
5 int main()
6 {
7 int n;
8 cin >> n;
9 if(n%2 == 0) cout << "NO" << endl;
10 else {
11 cout << "YES" << endl;
12 int aa = 0;
13 int bb = 0;
14 int cnt = 0;
15 for(int i = 1;i <= 2*n;i++)
16 {
17 if(!cnt)
18 {
19 a[aa++] = i++;
20 b[bb++] = i;
21 cnt = 1;
22 }
23 else
24 {
25 b[bb++] = i++;
26 a[aa++] = i;
27 cnt = 0;
28 }
29 }
30 for(int i = 0;i < aa;i++) cout << a[i] << " ";
31 for(int i = 0;i < bb;i++) cout << b[i] << " ";
32 }
33 return 0;
34 }
D:
题意:n个点,如果 A[ i ] & A[ j ] != 0 则认为 i 和 j 之间有路,问图的最小环是多少。
思路:将A[ i ]用二进制表示,可以发现当某一列的1超过3次,则最小环为3,再由鸽巢原理,超过3*64的 n 即可直接输出3退出,剩下的跑floyd最小环自己到自己即可。(赛时不知道鸽巢原理的我看着1e5*64的二进制图发呆orz)
AC代码:
1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long ll;
4 const int maxn = 4e6 + 50;
5 const int INF = 1e9;
6 ll a[maxn];
7 int num[80];
8 ll mp[500][500];
9 ll d[500][500];
10 void getnum(ll x)
11 {
12 for(int i = 63;i >= 0;i--)
13 {
14 if(x & (1LL << i)) num[i] ++;
15 }
16 }
17 int main()
18 {
19 std::ios::sync_with_stdio(false);
20 int n;
21 cin >> n;
22 for(int i = 1;i <= n;i++)
23 {
24 cin >> a[i];
25 if(a[i] == 0)
26 {
27 n--,i--;
28 continue;
29 }
30 getnum(a[i]);
31
32 }
33 for(int i = 0;i < 80;i++)
34 {
35 if(num[i] >= 3)
36 {
37 cout << 3 << endl;
38 return 0;
39 }
40 }
41 for(int i = 1;i <= n;i++)
42 for(int j = 1;j <= n;j++)
43 mp[i][j] = INF,d[i][j] = INF;
44 for(int i = 1;i <= n;i++)
45 for(int j = i+1;j <= n;j++)
46 if(a[i]&a[j])
47 mp[i][j]=1,mp[j][i]=1,d[i][j]=1,d[j][i]=1;
48 ll ans = INF;
49 for(int k = 1;k <= n;k++){
50 for(int i = 1;i < k;i++){
51 for(int j = i+1;j < k;j++){
52 ans = min(d[i][j] + mp[j][k] + mp[k][i],ans);
53 }
54 }
55 for(int i = 1;i <= n;i++){
56 for(int j = 1;j <= n;j++){
57 if(d[i][j] > (d[i][k] + d[k][j])){
58 d[i][j] = d[i][k] + d[k][j];
59 }
60 }
61 }
62 }
63 if(ans == INF) cout << -1 << endl;
64 else cout << ans << endl;
65 return 0;
66 }