牛客练习赛16
Posted Algorithms Crush Me
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了牛客练习赛16相关的知识,希望对你有一定的参考价值。
倒着贪心
1 #include <bits/stdc++.h> 2 using namespace std; 3 char s[111111]; 4 5 int main(){ 6 string ans = ""; 7 scanf("%s", s + 1); 8 int len = strlen(s + 1); 9 ans += s[len]; 10 for(int i = len - 1; i >= 1; i--){ 11 if(s[i] >= ans[ans.length()-1]) ans += s[i]; 12 } 13 reverse(ans.begin(), ans.end()); 14 cout << ans << endl; 15 return 0; 16 }
减个等差
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e5; 4 int a[maxn]; 5 map<int, int> cnt; 6 map<int, int> :: iterator it; 7 8 int main(){ 9 int n; 10 scanf("%d", &n); 11 for(int i = 1; i <= n; ++i) scanf("%d", a + i); 12 for(int i = 1; i <= n / 2; ++i){ 13 a[i] -= i; 14 a[n-i+1] -= i; 15 cnt[a[i]]++; 16 cnt[a[n-i+1]]++; 17 } 18 if(n % 2) a[n/2+1] -= n/2+1, cnt[a[n/2+1]]++; 19 int ans = n; 20 for(it = cnt.begin(); it != cnt.end(); it++){ 21 ans = min(ans, n - (*it).second); 22 } 23 cout << ans << endl; 24 return 0; 25 }
数连通块
1 #include <bits/stdc++.h> 2 using namespace std; 3 int fa[111]; 4 int Find(int x){ 5 return fa[x] == x ? x : fa[x] = Find(fa[x]); 6 } 7 void Union(int x, int y){ 8 x = Find(x), y = Find(y); 9 fa[x] = y; 10 } 11 int xx[111], yy[111]; 12 int main(){ 13 int n, ans = -1; 14 scanf("%d", &n); 15 for(int i = 1; i <= n; ++i){ 16 fa[i] = i; 17 scanf("%d %d", xx + i, yy + i); 18 for(int j = 1; j < i; ++j) 19 if(xx[i] == xx[j] || yy[i] == yy[j]) Union(i, j); 20 } 21 for(int i = 1; i <= n; ++i){ 22 if(fa[i] == i) ans++; 23 } 24 printf("%d\n", ans); 25 return 0; 26 }
模k-1不变
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 LL a[maxn], sum[maxn]; 6 map<LL, LL> cnt; 7 8 int main(){ 9 LL k, b, n, ans = 0, t = 0, z = 0; 10 scanf("%lld %lld %lld", &k, &b, &n); 11 for(int i = 1; i <= n; ++i){ 12 scanf("%lld", a + i); 13 sum[i] = (sum[i-1] + a[i]) % (k - 1); 14 if(a[i] == 0) t++, z += t; 15 else t = 0; 16 } 17 if(b == 0) ans = z; 18 else { 19 cnt[0]++; 20 for(int i = 1; i <= n; ++i) 21 ans += cnt[(sum[i]-b+k-1)%(k-1)], cnt[sum[i]]++; 22 if(b == k - 1) ans -= z; 23 } 24 printf("%lld\n", ans); 25 return 0; 26 }
找变化点
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 LL a[maxn]; 6 set<int> pos, v; 7 set<int> :: reverse_iterator it; 8 9 int main(){ 10 int n; 11 scanf("%d", &n); 12 for(int i = 1; i <= n; ++i){ 13 scanf("%d", a + i); 14 v.insert(a[i]); 15 int tmp = a[i]; 16 vector<int> cl; 17 for(it = pos.rbegin(); it != pos.rend(); it++){ 18 if((a[*it] | tmp) != tmp) tmp |= a[*it], v.insert(tmp); 19 else cl.push_back(*it); 20 } 21 for(int j = 0; j < cl.size(); j++) pos.erase(cl[j]); 22 pos.insert(i); 23 } 24 printf("%d\n", v.size()); 25 return 0; 26 }
随便加加
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 const int maxn = 1e5 + 10; 5 LL a[maxn]; 6 7 int main(){ 8 int n; 9 LL d, ans = 0; 10 scanf("%d %lld", &n, &d); 11 for(int i = 1; i <= n; ++i){ 12 scanf("%lld", a + i); 13 int j = lower_bound(a + 1, a + 1 + i, a[i] - d) - a; 14 ans += (i - j) * (LL)(i - j - 1) / 2; 15 } 16 cout << ans << endl; 17 return 0; 18 }
以上是关于牛客练习赛16的主要内容,如果未能解决你的问题,请参考以下文章