题目链接:https://codeforces.com/contest/1234
A:
题意:找出一个数,使得以这个数为价格卖出所有商品的钱大于等于以之前的各个商品初始价格卖出的钱。
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,让我又学到了,菜啊...
B1:
题意:n次查询,若消息未出现,就加入,若长度超过k,就删除最后一条(相对最后一条加入的消息)
idea:由于 n 和 k 的范围较小,暴力模拟即可
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:
题意:同B1
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:待补