題目連接:https://codeforces.com/contest/1213ios
A:c++
題意:給定數的位置,位置爲整數,每一個數能夠向左或右移動一格或者兩格,移動一格花費一個硬幣,兩格不花費硬幣,問全部硬幣移動到同一位置至少要花費多少硬幣ide
idea:每一個數的奇偶個數idea
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int a[110], n, s1, s2; 5 6 int main() 7 { 8 cin >> n; 9 for (int i = 0; i < n; i ++ ) 10 { 11 cin >> a[i]; 12 if (a[i] % 2) s1 ++ ; //s1爲偶數 13 else s2 ++ ; 14 } 15 int ans = min(s1, s2); 16 cout << ans << endl; 17 return 0; 18 }
B:spa
題意:給出天天的價格,若是後面天數有價格比當前天數價格低,就認爲這一天爲「壞」的一天,問總共有多少天是「壞的」3d
idea:單調棧,從後面往前遍歷一遍,時間複雜度O(n)code
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 const int MAXN = 1e6 + 10; 6 int t, n, a[MAXN]; 7 8 int main() 9 { 10 cin >> t; 11 while (t -- ) 12 { 13 scanf("%d",&n); 14 for (int i = 0; i < n; i ++ ) 15 scanf("%d",&a[i]); 16 17 int ss = a[n - 1], ans = 0; 18 for (int i = n - 2; i >= 0; i -- ) 19 { 20 if (a[i] > ss) ans ++ ; 21 if (a[i] < ss) ss = a[i]; 22 } 23 cout << ans << endl; 24 } 25 return 0; 26 }
C:blog
題意:輸入n和m,求1~n中能整除m的數的個位數累加和ci
idea:數學題,i * m % 10 = (10 + i) * m % 10,0 <= i <= 9get
1 #include <iostream> 2 #include <cstdio> 3 4 using namespace std; 5 typedef long long ll; 6 int q, a[10]; 7 8 int main() 9 { 10 cin >> q; 11 while (q -- ) 12 { 13 ll n, m, k, sum = 0, ans = 0; 14 cin >> n >> m; 15 for (int i = 0; i < 10; i ++ ) 16 { 17 a[i] = m * (1 + i) % 10; 18 sum += a[i]; 19 } 20 21 k = n / m; 22 ll s; 23 s = k % 10; 24 for (int i = 0; i < s; i ++ ) ans += a[i]; 25 ans += (k / 10) * sum; 26 cout << ans << endl; 27 } 28 return 0; 29 }
D1:
題意:給定一些數,數能變成 n / 2 (向下取整),問變成某個數m,且至少有k個數能變成m須要的操做次數至少是多少
idea:記錄每一個數的貢獻,若變成m的數大於等於k個,取前k小操做次數累加便可(純暴力瞎搞...)
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 6 using namespace std; 7 const int MAXN = 1e6; 8 const int _inf = 0x3f3f3f; 9 int k, n, a[MAXN], b[MAXN], c[MAXN], ans = _inf; 10 11 int main() 12 { 13 cin >> n >> k; 14 for (int i = 0; i < n; i ++ ) 15 cin >> a[i]; 16 17 int idd = 0; 18 for (int i = 0; i < n; i ++ ) 19 { 20 int x = a[i]; 21 b[idd ++ ] = x; 22 while (x > 0) 23 { 24 x >>= 1; 25 b[idd ++ ] = x; 26 } 27 } 28 29 for (int i = 0; i < idd; i ++ ) 30 { 31 int id = 0; 32 for (int j = 0; j < n; j ++ ) 33 { 34 int x = a[j], cur = 0; 35 while (x > b[i]) 36 { 37 x >>= 1; 38 cur ++ ; 39 } 40 if (x == b[i]) { 41 c[id ++ ] = cur; 42 } 43 } 44 if (id >= k) { 45 int sum = 0; 46 sort(c, c + id); 47 for (int j = 0; j < k; j ++ ) sum += c[j]; 48 ans = min(ans, sum); 49 } 50 memset(c,0,sizeof c); 51 } 52 cout << ans << endl; 53 return 0; 54 }
PS:因爲本身懶,拖了很久才補的題,之後要第一時間把題補了,專心補題