Educational Codeforces Round 32 题解

题目链接:http://codeforces.com/contest/888

A. Local Extrema

题意:没啥好说的。

解法:模拟,扫一遍统计答案。

#include 
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; ia[i-1]&&a[i]>a[i+1])||(a[i]


B. Buggy Robot

题意:机器人可以Up,Down,Left,Right四个方向移动一格;现在给出一系列移动指令,因为规定了机器人肯定能在执行完这一系列指令后能回到原点,所以着一系列指令中必然有些是无效指令;求最大有效指令是多少个;

解法:一个U跟一个D抵消,一个L和一个R抵消,对它们计数一下在再计算一下即可。

#include 
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

C. K-Dominant Character

题意:让你找一个最小的长度k,使得所有子串中存在一个相同的字符c

解法:二分答案然后去check。  check的时候直接前缀和判断

#include 
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


D. Almost Identity Permutations

题意:有index-k个数要求和index一致,那么其他和自己的index不一致,当然是错排公式了,但是这个错排最多只算到4个。

解法:我做这个题没考虑错排,直接把4组样例怼过,这题就过了。

#include 
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 
using namespace std;
vector  v;
int n, m, a[40], b[40];

int main(){
    scanf("%d %d", &n,&m);
    for(int i=0; i

F. Connecting Vertices

Educational Codeforces Round 32 题解_第1张图片

G. Xor-MST

Educational Codeforces Round 32 题解_第2张图片


#include 
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<=r) return;
    int mid=l-1;
    while(mid>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);
}



你可能感兴趣的:(Educational Codeforces Round 32 题解)