Educational Codeforces Round 32 题解
来源:互联网 发布:在线订单管理系统源码 编辑:程序博客网 时间:2024/05/18 01:00
题目链接:http://codeforces.com/contest/888
A. Local Extrema
题意:没啥好说的。
解法:模拟,扫一遍统计答案。
#include <bits/stdc++.h>using namespace std;int n, a[1010];int main(){ scanf("%d", &n); int ans = 0; for(int i=1; i<=n; i++) scanf("%d", &a[i]); for(int i=2; i<n; i++){ if((a[i]>a[i-1]&&a[i]>a[i+1])||(a[i]<a[i-1]&&a[i]<a[i+1])) ans++; } return 0*printf("%d\n", ans);}
B. Buggy Robot
题意:机器人可以Up,Down,Left,Right四个方向移动一格;现在给出一系列移动指令,因为规定了机器人肯定能在执行完这一系列指令后能回到原点,所以着一系列指令中必然有些是无效指令;求最大有效指令是多少个;
解法:一个U跟一个D抵消,一个L和一个R抵消,对它们计数一下在再计算一下即可。
#include <bits/stdc++.h>using namespace std;char s[110];int n;int main(){ int L=0, R=0, U=0, D=0; scanf("%d", &n); scanf("%s", s); for(int i=0; i<n; i++){ if(s[i]=='L') L++; else if(s[i]=='R') R++; else if(s[i]=='U') U++; else D++; } return 0*printf("%d\n", min(L,R)*2+min(U,D)*2);}
C. K-Dominant Character
题意:让你找一个最小的长度k,使得所有子串中存在一个相同的字符c
解法:二分答案然后去check。 check的时候直接前缀和判断
#include <bits/stdc++.h>using namespace std;const int maxn = 100010;char s[maxn];int len;int cnt[26][maxn];bool check(int mid){ for(int c = 0; c<26; c++){ bool flag = 1; if(cnt[c][mid-1]==0) continue; for(int i=mid; i<len; i++){ if(cnt[c][i]-cnt[c][i-mid]==0){ flag = 0; break; } } if(flag) return 1; } return 0;}int main(){ scanf("%s", s); len = strlen(s); memset(cnt, 0, sizeof(cnt)); cnt[s[0]-'a'][0] = 1; for(int i=1; i<len; i++){ for(int j=0; j<26; j++){ if(s[i]-'a'==j){ cnt[s[i]-'a'][i]=cnt[s[i]-'a'][i-1]+1; } else{ cnt[j][i] = cnt[j][i-1]; } } } int l = 0, r = len, ans = 0; while(l <= r){ int mid = (l+r)/2; if(check(mid)) ans = mid, r = mid-1; else l=mid+1; } return 0*printf("%d\n", ans);}
D. Almost Identity Permutations
题意:有index-k个数要求和index一致,那么其他和自己的index不一致,当然是错排公式了,但是这个错排最多只算到4个。
解法:我做这个题没考虑错排,直接把4组样例怼过,这题就过了。
#include <bits/stdc++.h>using namespace std;typedef long long LL;int main(){ LL n, k; scanf("%lld %lld", &n,&k); if(k==1){ printf("1\n"); }else if(k==2){ LL ans = n*(n-1)/2+1; printf("%lld\n", ans); } else if(k==3){ LL ans = 1+(n-1)*n/2+(n-2)*(n-1)*n/6*2; printf("%lld\n", ans); }else{ LL ans = 1+(n-1)*n/2+(n-2)*(n-1)*n/6*2+(n-3)*(n-2)*(n-1)*n/24*9; printf("%lld\n", ans); } return 0;}
E. Maximum Subsequence
题意:给出一些数字,现在选择 k个不同的下标对应的数字,问它们的和模 m最大为多少?
解法:我直接对前面20个状压,用vector存下来,接下来的15个再状压,然后在前面二分找加上这个数小于m的最大值。
#include <bits/stdc++.h>using namespace std;vector <int> v;int n, m, a[40], b[40];int main(){ scanf("%d %d", &n,&m); for(int i=0; i<n; i++) scanf("%d", &a[i]); int cnt = 0; for(int i=min(20,n); i<n; i++) b[cnt++] = a[i]; if(cnt == 0){ int ans = 0; for(int i=0; i<(1<<n); i++){ int sum = 0; for(int j=0; j<n; j++){ if(i&(1<<j)){ sum += a[j]%m; sum %= m; } } sum %= m; ans = max(ans, sum); } printf("%d\n", ans); }else{ v.push_back(0); for(int i=0; i<(1<<20); i++){ int sum = 0; for(int j=0; j<20; j++){ if(i&(1<<j)){ sum += a[j]%m; sum %= m; } } sum %= m; v.push_back(sum); } sort(v.begin(),v.end()); int ans = 0; for(int i=0; i<(1<<cnt); i++){ int sum = 0; for(int j=0; j<cnt; j++){ if(i&(1<<j)){ sum += b[j]%m; sum %= m; } } sum %= m; //m-sum-1 int l = 0, r = v.size()-1, pos = 0; while(l<=r){ int mid=(l+r)/2; if(v[mid]<=m-sum-1) pos=mid,l=mid+1; else r=mid-1; } ans = max(ans, sum + v[pos]); } printf("%d\n", ans); } return 0;}
F. Connecting Vertices
G. Xor-MST
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int maxn = 2e5+7, maxm = 35*maxn;int nxt[maxm][2], tot, a[maxn];LL ans = 0;void ins(int x){ int now = 0; for(int i=29; i>=0; i--){ int go = (x>>i)&1; if(nxt[now][go]==0) nxt[now][go] = ++tot; now = nxt[now][go]; }}LL query(int x){ LL ret=0; int now=0; for(int i=29; i>=0; i--){ int go = (x>>i)&1; if(nxt[now][go]) now=nxt[now][go]; else now=nxt[now][go^1], ret|=(1<<i); } return ret;}void dfs(int l, int r, int dep){ if(dep==-1||l>=r) return; int mid=l-1; while(mid<r&&(a[mid+1]>>dep&1)==0) mid++; dfs(l,mid,dep-1); dfs(mid+1,r,dep-1); if(mid==l-1||mid==r) return; for(int i=l; i<=mid; i++) ins(a[i]); LL ret = 1e18; for(int i=mid+1; i<=r; i++) ret = min(ret, query(a[i])); ans += ret; for(int i=0; i<=tot; i++) nxt[i][0]=nxt[i][1]=0; tot=0;}int n;int main(){ scanf("%d", &n); for(int i=1; i<=n; i++) scanf("%d", &a[i]); sort(a+1, a+n+1); dfs(1, n, 29); return 0*printf("%lld\n", ans);}
阅读全文
0 0
- Educational Codeforces Round 32[题解]
- Educational Codeforces Round 32 题解
- Educational Codeforces Round 7 题解
- Educational Codeforces Round 7 题解
- Educational Codeforces Round 14 题解
- Educational Codeforces Round 20题解
- Educational Codeforces Round 24 题解
- Educational Codeforces Round 23 题解
- Educational Codeforces Round 22 题解
- Educational Codeforces Round 25 题解
- Educational Codeforces Round 21 题解
- Educational Codeforces Round 29 题解
- Educational Codeforces Round 19 题解
- Educational Codeforces Round 28 题解
- Educational Codeforces Round 31 题解
- Educational Codeforces Round 30 题解
- Educational Codeforces Round 17 题解
- Educational Codeforces Round 34 题解
- 细数程序员出身的互联网大佬
- 基于裸数据的异地数据库性能诊断与优化
- 简单的设计模式
- CSS学习笔记(五):居中显示
- 一些简称(持续更新)
- Educational Codeforces Round 32 题解
- 拷贝函数
- IOS11的新功能你发现了没有~~~
- AMS 启动的时候杀掉所有进程
- C语言 异步回调
- Git常用命令
- Layout run failed错误解决方案
- SharedPreferences 存储实体类
- 漫画人物脚的结构与画法