1. 程式人生 > >Codeforces Round #500 (Div. 2) BC

Codeforces Round #500 (Div. 2) BC

wid gif int and footer str false 時間復雜度 with

B

可以發現只有一次與操作是有意義的,所以答案只有-1,0,1,2四種情況

技術分享圖片
 1 #include <bits/stdc++.h>
 2 #define show(a) cout << #a << " = " << a << endl;
 3 const int MOD = 1e9+7;
 4 const int MAXN = 100005;
 5 const int INF = 500005;
 6 typedef long
long ll; 7 8 using namespace std; 9 10 int main() 11 { 12 std::ios::sync_with_stdio(false); 13 int n, x, ans = -1; 14 cin >> n >> x; 15 int a[MAXN], point[MAXN]; 16 for (int i = 1; i <= n; i++) 17 { 18 cin >> a[i]; 19 } 20 sort(a+1, a+1
+n); 21 for (int i = 2; i <= n; i++) 22 { 23 if (a[i] == a[i-1]) 24 { 25 ans = 0; 26 break; 27 } 28 } 29 if (ans == -1) 30 { 31 int flg = 0; 32 for (int i = 1; i <= n; i++) 33 point[i] = a[i] & x;
34 for (int i = 1; i <= n; i++) 35 { 36 int s = lower_bound(a+1, a+1+n, point[i]) - a; 37 if (point[i] == a[s] && s != i) 38 { 39 ans = 1, flg = 1; 40 break; 41 } 42 } 43 if (!flg) 44 { 45 sort(point+1, point+1+n); 46 for (int i = 2; i <= n; i++) 47 { 48 if (point[i] == point[i-1]) 49 { 50 ans = 2; 51 break; 52 } 53 } 54 } 55 } 56 cout << ans << endl; 57 return 0; 58 }
View Code

C

可以看成是紅藍染色,要求(紅最大-紅最小)*(藍最大*藍最小),排序後考慮兩端顏色相同或不同,時間復雜度O(n)

技術分享圖片
 1 #include <bits/stdc++.h>
 2 #define show(a) cout << #a << " = " << a << endl;
 3 typedef long long ll;
 4 const int MOD = 1e9+7;
 5 const int MAXN = 200005;
 6 const ll INF = 1e18;
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     std::ios::sync_with_stdio(false);
13     int n;
14     cin >> n;
15     ll num[n*2+5];
16     for (int i = 1; i <= 2*n; i++)
17         cin >> num[i];
18     sort(num+1, num+1+2*n);
19     ll ans = INF;
20     for (int i = 2; i <= n; i++)
21         ans = min(ans, (num[i+n-1] - num[i]) * (num[2*n] - num[1]));
22     ans = min(ans, (num[n*2] - num[n+1]) * (num[n] - num[1]));
23     cout << ans << endl;
24     return 0;
25 }
View Code

Codeforces Round #500 (Div. 2) BC