Codeforces Global Round 15 ABCD
A. Subsequence Permutation
題意:
給定一個字符串,選擇任意數量的字符調整位置,使得調整位置之后整個序列是從小到大的,要求調整的位置數量最小
輸入
4
3
lol
10
codeforces
5
aaaaa
4
dcba
輸出
2
6
0
4
思路
這個沒什么好說的,定義一個字符串,從小到大排序之后判斷是不是和原位置的字符相等就可,另外注意不要用之前的位置和當前的位置比較
代碼
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map>using namespace std;typedef long long ll; #define SSS \ios_base::sync_with_stdio(0); \cin.tie(0); \cout.tie(0);#define rep(i, a, b) for (int i = (a), i <= (b); i++) #define per(i, a, b) for (int i = (a), i >= (b); i--) #define pii pair<int, int> #define pdd pair<double, double> const int N = 1e5 + 10; const int mod = 1e9 + 7;int main() {SSS;int t;cin >> t;while (t--){int n;string s;cin >> n;cin >> s;string s1 = s;int cnt = 0;sort(s1.begin(), s1.end());for (int i = 0; i < s.size(); i++){if (s[i] != s1[i]){cnt++;}}cout << cnt << endl;}return 0; }B - Running for Gold
題意:
有n個選手要參加馬拉松比賽,并且每個馬拉松選手以前都參加過5場比賽,如果存在一個選手,在任意三場比賽中都比另一個選手的成績好,那么這個選手就強,如果有選手比其他所有的選手都強,那么就認為這個選手可以在本次馬拉松比賽中拿第一,求是否存在這樣的選手,存在輸出這個選手的下標,否則輸出-1
輸入
4
1
50000 1 50000 50000 50000
3
10 10 20 30 30
20 20 30 10 10
30 30 10 20 20
3
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
6
9 5 3 7 1
7 4 1 6 8
5 6 7 3 2
6 7 8 8 6
4 2 2 4 5
8 3 6 9 4
輸出
1
-1
1
5
思路
這題n<=50000, t<=1000,所以得想一個O(n)的做法,可以轉換成打擂臺的題,假設有選手A和B,A如果在任意三場比賽中強于B,那么B就必不可能是第一,那么我們可以按照這樣模擬一個擂臺,首先A上場,和B打,A或者B贏了的留下,作為當前的最強者繼續和C打,那么到最后必定有一個人留下,這個人前面的必定都是被打下去的,這個人之后的都是被他打過的,然后對于這樣的一個最強者,我們對他之前的選手進行遍歷,尋找是否有比他強的,因為前面的人是被別人打下的,他自己不一定可以打下,如果存在這樣的選手,就說明不存在可以拿第一的,否則輸出當前選手下標
代碼
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map>using namespace std;typedef long long ll; #define SSS \ios_base::sync_with_stdio(0); \cin.tie(0); \cout.tie(0); #define rep(i, a, b) for (int i = (a), i <= (b); i++) #define per(i, a, b) for (int i = (a), i >= (b); i--) #define pii pair<int, int> #define pdd pair<double, double> const int N = 5e4 + 10; const int mod = 1e9 + 7;int r[10][N]; int main() {SSS;int t;cin >> t;while (t--){int n;cin >> n;for (int i = 1; i <= n; i++){for (int j = 1; j <= 5; j++){cin >> r[j][i];}}int ans = 1; // 當前的最強者for (int i = 2; i <= n; i++){int cnt = 0;for (int j = 1; j <= 5; j++){if (r[j][ans] > r[j][i]){cnt++;}} // 如果有選手三項比賽的成績大于當前的最強者就更新if (cnt >= 3){ans = i;}}int flag = 1; // 之后在遍歷一遍,強者鑒定for (int i = 1; i <= n; i++){int cnt = 0;for (int j = 1; j <= 5; j++){if (r[j][ans] > r[j][i]){cnt++;}if (cnt >= 3){flag = 0;break;}}}if (flag){cout << ans << endl;}else{cout << -1 << endl;}}return 0; }C - Maximize the Intersections
題意:
大概就是有2n個點,給你k條已經連起來的邊,求如果把剩下沒連起來的邊連起來,那么最終所有相交線的交點最多有多少個(沒太看懂,但是按我理解是這么個意思)
輸入
4
4 2
8 2
1 5
1 1
2 1
2 0
10 6
14 6
2 20
9 10
13 18
15 12
11 7
輸出
4
0
1
14
思路:
如果我們想要交點最多,那么就要讓每隊點的小點盡可能的小,另一個大點要大于盡可能多的小點,然后看下圖(其實自己畫一畫就出來了)
從cf題解里面扣的圖,可以看出ad和bc連是最優的,得出一個結論,假設有2n個點要連,那么最優解是1 – n+1, 2 – n+2 , … , n-1 – 2n
代碼
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map>using namespace std;typedef long long ll; #define SSS \ios_base::sync_with_stdio(0); \cin.tie(0); \cout.tie(0);#define rb(i, a, b) for (int i = (a), i <= (b); i++) #define re(i, a, b) for (int i = (a), i >= (b); i--) #define pii pair<int, int> const int N = 110; const int mod = 1e9 + 7;pii a[N]; int used[500]; int main() {SSS;int t;cin >> t;while (t--){int n, k;cin >> n >> k;n = n * 2;for (int i = 1; i <= n; i++){used[i] = i;a[i].first = 0;a[i].second = 0;}for (int i = 1; i <= k; i++){cin >> a[i].first >> a[i].second;if (a[i].first > a[i].second){int temp = a[i].first;a[i].first = a[i].second;a[i].second = temp;}used[a[i].first] = 1000;used[a[i].second] = 1000;}sort(used + 1, used + n + 1);// for (int i = 1; i <= n; i++)// {// cout << used[i] << " ";// }int mid = (n - k * 2) / 2;// cout << mid << endl;for (int i = k + 1; i <= k + mid; i++){a[i].first = used[i - k];a[i].second = used[mid + i - k];}sort(a + 1, a + n / 2 + 1);// cout << endl;// for (int i = 1; i <= n / 2; i++)// {// cout << a[i].first << " " << a[i].second << endl;// }// cout << endl;int cnt = 0;for (int i = 1; i <= n/2-1; i++){for (int j = i + 1; a[j].first < a[i].second && j <=n/2; j++){if (a[j].second > a[i].second){cnt++;// cout << endl;// cout << i << " " << j << endl;// cout << a[i].first << " " << a[i].second << endl;// cout << a[j].first << " " << a[j].second << endl;// cout << endl;}}}cout << cnt << endl;}return 0; }D. Array Differentiation
題意
給定一個序列a,是否存在一個序列b,對于任意一個a,都有ai = bj + bk
輸入
5
5
4 -7 -1 5 10
1
0
3
1 10 100
4
-3 2 10 2
9
25 -171 250 174 152 242 100 -205 -258
輸出
YES
YES
NO
YES
YES
思路
對于序列a和b,我們可以首先求出序列a的所有子集,若序列a的所有子集存在重復的,那么就勢必存在一種序列b使得序列a成立
假設序列a的子集有c[i],c[j],c[k],c[l],若c[j] = c[k],則c[j]和c[k]對應的集合可視作一個集合,即序列a至少會少一個數,而一個數量為n的序列是可以被n+1個對應序列所表示的,比如a序列1 2 3 4 5對應的b序列可以為0 1 2 3 4 5,而少一個數則說明對應的序列也可以-1,即至多可以用n個數來表示,所以條件成立
#include <iostream> #include <algorithm> #include <cstring> #include <vector> #include <cmath> #include <stack> #include <queue> #include <set> #include <map>using namespace std;typedef long long ll; #define SSS \ios_base::sync_with_stdio(0); \cin.tie(0); \cout.tie(0); #define caseT \int t; \cin >> t; \while (t--) #define rep(i, a, b) for (int i = (a), i <= (b); i++) #define per(i, a, b) for (int i = (a), i >= (b); i--) #define pii pair<int, int> #define pdd pair<double, double> const int N = 100 + 10; const int mod = 1e9 + 7;int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b); }int lcm(int a, int b) {return a * b / gcd(a, b); }int lowbit(int x) {return x & -x; } int a[N], b[N];int main() {SSS;int t;cin >> t;while (t--){int n;map<int, int> mp;cin >> n;for (int i = 0; i < n; i++){cin >> a[i];}for (int i = 0; i < (1 << n); i++){int sum = 0;for (int j = 0; j < n; j++){if (i & (1 << j))sum += a[j];}mp[sum]++;}// for (auto i = mp.begin(); i != mp.end(); i++)// {// cout << i->first << " ";// }// cout << endl;// cout << mp.size() << " " << (1 << n) << endl;if (mp.size() != (1 << n)){puts("YES");}else{puts("NO");}}return 0; }總結
以上是生活随笔為你收集整理的Codeforces Global Round 15 ABCD的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深度剖析数据在内存中的存储(修炼内功~吊
- 下一篇: 微信公众号和微信群怎么推广?