Codeforces Round #587 (Div. 3)

題目連接:https://codeforces.com/contest/1216ios


A:c++

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

idea:遍歷一遍就行了,不知足條件的就換。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:spa

題意:射擊n個目標,每次會有不一樣的消耗,問按什麼順序射擊能夠消耗最少。3d

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

 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:blog

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

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

 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
相關文章
相關標籤/搜索