日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Global Round 15 ABCD

發(fā)布時(shí)間:2023/12/14 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Global Round 15 ABCD 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

A. Subsequence Permutation

題意:

給定一個(gè)字符串,選擇任意數(shù)量的字符調(diào)整位置,使得調(diào)整位置之后整個(gè)序列是從小到大的,要求調(diào)整的位置數(shù)量最小

輸入

4
3
lol
10
codeforces
5
aaaaa
4
dcba

輸出

2
6
0
4

思路

這個(gè)沒什么好說(shuō)的,定義一個(gè)字符串,從小到大排序之后判斷是不是和原位置的字符相等就可,另外注意不要用之前的位置和當(dāng)前的位置比較

代碼

#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個(gè)選手要參加馬拉松比賽,并且每個(gè)馬拉松選手以前都參加過(guò)5場(chǎng)比賽,如果存在一個(gè)選手,在任意三場(chǎng)比賽中都比另一個(gè)選手的成績(jī)好,那么這個(gè)選手就強(qiáng),如果有選手比其他所有的選手都強(qiáng),那么就認(rèn)為這個(gè)選手可以在本次馬拉松比賽中拿第一,求是否存在這樣的選手,存在輸出這個(gè)選手的下標(biāo),否則輸出-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,所以得想一個(gè)O(n)的做法,可以轉(zhuǎn)換成打擂臺(tái)的題,假設(shè)有選手A和B,A如果在任意三場(chǎng)比賽中強(qiáng)于B,那么B就必不可能是第一,那么我們可以按照這樣模擬一個(gè)擂臺(tái),首先A上場(chǎng),和B打,A或者B贏了的留下,作為當(dāng)前的最強(qiáng)者繼續(xù)和C打,那么到最后必定有一個(gè)人留下,這個(gè)人前面的必定都是被打下去的,這個(gè)人之后的都是被他打過(guò)的,然后對(duì)于這樣的一個(gè)最強(qiáng)者,我們對(duì)他之前的選手進(jìn)行遍歷,尋找是否有比他強(qiáng)的,因?yàn)榍懊娴娜耸潜粍e人打下的,他自己不一定可以打下,如果存在這樣的選手,就說(shuō)明不存在可以拿第一的,否則輸出當(dāng)前選手下標(biāo)

代碼

#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; // 當(dāng)前的最強(qiáng)者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++;}} // 如果有選手三項(xiàng)比賽的成績(jī)大于當(dāng)前的最強(qiáng)者就更新if (cnt >= 3){ans = i;}}int flag = 1; // 之后在遍歷一遍,強(qiáng)者鑒定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個(gè)點(diǎn),給你k條已經(jīng)連起來(lái)的邊,求如果把剩下沒連起來(lái)的邊連起來(lái),那么最終所有相交線的交點(diǎn)最多有多少個(gè)(沒太看懂,但是按我理解是這么個(gè)意思)

輸入

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

思路:

如果我們想要交點(diǎn)最多,那么就要讓每隊(duì)點(diǎn)的小點(diǎn)盡可能的小,另一個(gè)大點(diǎn)要大于盡可能多的小點(diǎn),然后看下圖(其實(shí)自己畫一畫就出來(lái)了)


從cf題解里面扣的圖,可以看出ad和bc連是最優(yōu)的,得出一個(gè)結(jié)論,假設(shè)有2n個(gè)點(diǎn)要連,那么最優(yōu)解是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

題意

給定一個(gè)序列a,是否存在一個(gè)序列b,對(duì)于任意一個(gè)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

思路

對(duì)于序列a和b,我們可以首先求出序列a的所有子集,若序列a的所有子集存在重復(fù)的,那么就勢(shì)必存在一種序列b使得序列a成立

假設(shè)序列a的子集有c[i],c[j],c[k],c[l],若c[j] = c[k],則c[j]和c[k]對(duì)應(yīng)的集合可視作一個(gè)集合,即序列a至少會(huì)少一個(gè)數(shù),而一個(gè)數(shù)量為n的序列是可以被n+1個(gè)對(duì)應(yīng)序列所表示的,比如a序列1 2 3 4 5對(duì)應(yīng)的b序列可以為0 1 2 3 4 5,而少一個(gè)數(shù)則說(shuō)明對(duì)應(yīng)的序列也可以-1,即至多可以用n個(gè)數(shù)來(lái)表示,所以條件成立

#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; }

總結(jié)

以上是生活随笔為你收集整理的Codeforces Global Round 15 ABCD的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。