A.Remove Duplicates 题目链接:https://codeforc.es/contest/978/problem/A 题意:删除数列里相同的元素,只保留最右边的元素 #include iostream using namespace std; const int maxn = 1050 ; int a[ 55 ]; int ans[ 55 ]; in
A.Remove Duplicates
题目链接:https://codeforc.es/contest/978/problem/A
题意:删除数列里相同的元素,只保留最右边的元素
#include <iostream> using namespace std; const int maxn = 1050; int a[55]; int ans[55]; int vis[maxn]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int j = 0; for (int i = 0; i < n; i++) { cin >> a[i]; vis[a[i]]++; if (vis[a[i]] == 1) j++; } cout << j << endl; for (int i = 0; i < n; i++) { if (vis[a[i]] > 1) { vis[a[i]]--; continue; } if (vis[a[i]] == 1) cout << a[i] << endl; } return 0; }View Code
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; }View Code
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; }View Code