題目連接:https://codeforces.com/contest/1234ios
A:c++
題意:找出一個數,使得以這個數爲價格賣出全部商品的錢大於等於以以前的各個商品初始價格賣出的錢。ide
idea:取平均值,向上取整idea
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 int t, n; 7 double a[110]; 8 9 int main() 10 { 11 scanf("%d",&t); 12 while (t -- ) 13 { 14 double sum = 0, ans = 0; 15 scanf("%d",&n); 16 for (int i = 0; i < n; i ++ ) 17 { 18 scanf("%lf",&a[i]); 19 sum += a[i]; 20 } 21 ans = sum / n; 22 double s = ans - (int)ans; 23 if (!s) cout << (int)ans << endl; 24 else cout << (int)(ans + 1) << endl; 25 } 26 return 0; 27 }
ps:對於向上取整,能夠 (sum + n - 1) / n,讓我又學到了,菜啊...spa
B1:3d
題意:n次查詢,若消息未出現,就加入,若長度超過k,就刪除最後一條(相對最後一條加入的消息)code
idea:因爲 n 和 k 的範圍較小,暴力模擬便可blog
1 #include <bits/stdc++.h> 2 3 using namespace std; 4 int n, k, kk, a[210], b[210], id; 5 6 int main() 7 { 8 cin >> n >> k; 9 int ss = min(n,k); 10 for (int i = 0; i < n; i ++ ) cin >> a[i]; 11 12 for (int i = 0; i < n; i ++ ) 13 { 14 int flag = 0; 15 for (int j = kk; j < id; j ++ ) 16 { 17 if (a[i] == b[j]) 18 { 19 flag = 1; 20 break; 21 } 22 } 23 if (!flag) 24 { 25 b[id ++ ] = a[i]; 26 if ((id - kk) > ss) kk ++ ; 27 } 28 } 29 30 cout << id - kk << endl; 31 for (int i = id - 1; i >= kk; i -- ) cout << b[i] << " "; 32 return 0; 33 }
B2:ci
題意:同B1get
idea:因爲 n 和 k 的範圍變大,因此問題就轉變爲如何快速查詢消息是否出現過,天然想到用set
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 5 using namespace std; 6 const int MAXN = 2 * 1e5 + 10; 7 int n, k, q[MAXN], hh, ed; 8 9 int main() 10 { 11 set<int> s; 12 scanf("%d%d",&n,&k); 13 for (int i = 0; i < n; i ++ ) 14 { 15 int x; 16 scanf("%d",&x); 17 if (!s.count(x)) 18 { 19 if ((ed - hh) >= k) 20 { 21 int temp = q[hh]; 22 hh ++ ; 23 s.erase(temp); 24 } 25 q[ed ++ ] = x; 26 s.insert(x); 27 } 28 } 29 cout << ed - hh << endl; 30 for (int i = ed - 1; i >= hh; i -- ) cout << q[i] << " "; 31 return 0; 32 }
C:
題意:給出對應水管,詢問是否能在最後一個區域內出水(即右下角)
idea:模擬發現,水管的水流向是固定的,因此模擬便可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 5 using namespace std; 6 int q, n; 7 string s[2]; 8 9 int main() 10 { 11 scanf("%d",&q); 12 while (q -- ) 13 { 14 scanf("%d",&n); 15 cin >> s[0] >> s[1]; 16 int i, j; 17 j = 0; 18 for (i = 0; i < n; i ++ ) 19 { 20 if (s[j][i] >= '3') 21 { 22 if (s[j ^ 1][i] < '3') break; //^1,讓j上下移動,明顯減小代碼量,十分巧妙 23 else 24 { 25 j ^= 1; 26 } 27 } 28 } 29 if (i == n && j == 1) puts("YES"); 30 else puts("NO"); 31 } 32 return 0; 33 }
D:
題意:定點刪除,定點替換,區間查詢
idea:待補