A.Remove Duplicates
题目链接:https://codeforc.es/contest/978/problem/A
题意:删除数列里相同的元素,只保留最右边的元素
1 #include <iostream> 2 3 4 using namespace std; 5 6 const int N = 1e3 + 10; 7 int vis[N]; 8 int a[55]; 9 10 int main() { 11 int n; 12 cin >> n; 13 int cnt = 0; 14 for (int i = 0; i < n; i++) { 15 cin >> a[i]; 16 vis[a[i]]++; 17 if (vis[a[i]] == 1) cnt++; 18 } 19 cout << cnt << endl; 20 for (int i = 0; i < n; i++) { 21 if (vis[a[i]] == 1) cout << a[i] << ' '; 22 if (vis[a[i]] > 1) vis[a[i]]--; 23 } 24 return 0; 25 }
B. File Name
题目链接:https://codeforc.es/contest/978/problem/B
题意:不能有3个或以上的x连续出现,求删除几个x之后,不会有3个连续的x出现
#include <iostream> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; string s; cin >> n; cin >> s; int count = 0; for (int i = 2; i < n; i++) if (s[i - 1] == 'x' && s[i - 2] == 'x' && s[i] == 'x') count++; cout << count << endl; return 0; }
C. Letters
题目链接:https://codeforc.es/contest/978/problem/C
题意:m个楼,n个信件,求信件在第几个楼的第几封(前缀和+low_bound)
样例分析:
input:
3 6
10 15 12
1 9 12 23 26 37
output:
1 1
1 9
2 2
2 13
3 1
3 12
m为3,n为6
一号楼能装10封信,2号楼能装15封信,3号楼能装12封信
第1封信在1号楼第一个,第9封信在1号楼第9个,第12封信在2号楼第2个……
#include <iostream> #include <algorithm> #define LL long long using namespace std; const int maxn = 2e5 + 10; LL sum[maxn], a[maxn]; int m, n; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> m >> n; LL b; for (int i = 1; i <= m; i++) { cin >> a[i]; sum[i] = sum[i - 1] + a[i]; } while (n--) { cin >> b; int ans = lower_bound(sum + 1, sum + 1 + m, b) - sum; cout << ans << ' ' << b - sum[ans - 1] << endl; } return 0; }
D. Almost Arithmetic Progression
题目链接:https://codeforc.es/contest/978/problem/D
题意:一组数,只能操作一次+1或者-1或者不变,让这组数成为等差数列
思路:只要前两个数定下来后就好判断了,往后遍历看绝对值
#include <iostream> #include <algorithm> #include <cmath> using namespace std; const int maxn = 1e5 + 10; const int INF = 0x3f3f3f3f; int a[maxn], b[maxn]; int n; int ans = INF; int check(int u) { int cnt = 0; for (int i = 2; i < n; i++) { b[i] = b[i - 1] + u; if (abs(b[i] - a[i]) > 1) return -1; if (abs(b[i] - a[i]) == 1) cnt++; } return cnt; } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; if (n < 3) { cout << 0 << endl; return 0; } for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { b[0] = a[0] + i; b[1] = a[1] + j; int tmp = check(b[1] - b[0]); if (tmp != -1) { ans = min(ans, tmp + (i != 0) + (j != 0)); } } } if (ans == INF) cout << -1 << endl; else cout << ans << endl; return 0; }
E. Bus Video System
题目链接:https://codeforc.es/contest/978/problem/E
题意:有一辆车, 有n站, 车最多可以装下m个人,输入每站上车人数,求始发点人数可能的情况有多少种, 无解就输出0
思路:求车上人数最多时候与最少时候的差,m - 人数差 + 1
#include <iostream> #include <algorithm> using namespace std; int main() { int n, m; cin >> n >> m; int r = 0, l = 0; int bus_num = 0, t; while (n--) { cin >> t; bus_num += t; l = min(l, bus_num); r = max(r, bus_num); } int ans = m - r + l + 1; if (ans < 0) ans = 0; printf("%d\n", ans); return 0; }