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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #587 (Div. 3)

發布時間:2025/3/16 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #587 (Div. 3) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://codeforces.com/contest/1216


A:

題意:給出一個僅有a,b組成的字符串,可執行操作把a換成b,b換成a,問最小操作次數,使得任意前偶數里a,b的數量相等。

idea:遍歷一遍就好了,不滿足條件的就換。

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 7 int n, ans; 8 string a; 9 10 int main() 11 { 12 cin >> n >> a; 13 int len = n; 14 if (n % 2 != 0) len -= 1; 15 16 for (int i = 0; i < len; i ++ ) 17 { 18 if (a[i] == 'a') 19 { 20 if (a[i + 1] == 'b') i ++ ; 21 else 22 { 23 a[i + 1] = 'b'; 24 ans ++ ; 25 i ++ ; 26 } 27 } 28 else 29 { 30 if (a[i + 1] == 'a') i ++ ; 31 else 32 { 33 a[i + 1] = 'a'; 34 ans ++ ; 35 i ++ ; 36 } 37 } 38 } 39 40 cout << ans << endl; 41 cout << a; 42 return 0; 43 44 } View Code

B:

題意:射擊n個目標,每次會有不同的消耗,問按什么順序射擊可以消耗最少。

idea:貪心,排序后,從最大的開始依次射擊。

1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 5 using namespace std; 6 const int MAXN = 1000 + 10; 7 int n, ans, a[MAXN], b[MAXN]; 8 9 bool cmp(int x, int y) 10 { 11 return x > y; 12 } 13 14 int main() 15 { 16 cin >> n; 17 for (int i = 0; i < n; i ++ ) 18 { 19 cin >> a[i]; 20 b[i] = a[i]; 21 } 22 23 sort(a, a + n, cmp); 24 25 for (int i = 0; i < n; i ++ ) 26 { 27 ans += a[i] * i + 1; 28 } 29 30 cout << ans << endl; 31 for (int i = 0; i < n; i ++ ) 32 { 33 for (int j = 0; j < n; j ++ ) 34 { 35 if (a[i] == b[j]) 36 { 37 cout << j + 1 << " "; 38 b[j] = -1; 39 } 40 } 41 } 42 return 0; 43 } View Code

D:

題意:n種劍,每種劍有m把,來了k個人,每人偷走了s把劍,且每個人偷走的都是同一類型的劍,以知偷走后剩余的劍的數量a1,a2 .. an,問最少偷劍的人是多少。

idea:拿剩余數量中最大的數和其它an相減,計算出相減這些數的最大公約數就是每個人偷走的劍的數量,差值除gcd累加就是人數

1 include <iostream> 2 #include <cstdio> 3 #include <bits/stdc++.h> 4 5 using namespace std; 6 typedef long long ll; 7 const int MAXN = 2e5 + 10; 8 ll n, a[MAXN], b[MAXN], ss; 9 10 int main() 11 { 12 scanf("%lld",&n); 13 for (int i = 0; i < n; i ++ ) 14 { 15 scanf("%lld",&a[i]); 16 ss = max(a[i], ss); 17 } 18 for (int i = 0; i < n; i ++ ) b[i] = ss - a[i]; 19 20 ll c = 0; 21 for (int i = 0; i < n; i ++ ) 22 { 23 c = __gcd(c,b[i]); 24 } 25 ll ans = 0; 26 for (int i = 0; i < n; i ++ ) 27 { 28 ans += b[i] / c; 29 } 30 cout << ans << " " << c; 31 return 0; 32 } View Code

?

轉載于:https://www.cnblogs.com/chuyds/p/11572650.html

總結

以上是生活随笔為你收集整理的Codeforces Round #587 (Div. 3)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。