Codeforces Round #435 (Div. 2) A. B. C

A.Mahmoud and Ehab and the MEX

题目链接

http://codeforces.com/contest/862/problem/A

题解

给组数 问最少经过几次增加或者减少数字的操作可以得到 x
其中x为数列中最小的自然数

简单模拟就可以了

AC代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e5*4;
const int mod = 1e9+7;
bool num[MAXM];
int main()
{
    int n, k, x;
    scanf("%d%d",&n,&k);
    for(int i = 0; i < n; i++) {
        scanf("%d",&x);
        num[x] = true;
    }
    int ans = 0;
    for(int i = 0; i < k; i++) {
        ans += !num[i];
    }
    if(num[k]) ans++;
    printf("%d\n",ans);
return 0;
}

B. Mahmoud and Ehab and the bipartiteness

题目链接 http://codeforces.com/contest/862/problem/B

题解

给一棵树 问最多还能加几条边 就可以变成二分图
我们知道二分图的定义,可以人为的把点分成两个集合(奇数偶数),这样我们就可以DFS跑出这个树最多可以连的边数 ab

AC代码

#include <bits/stdc++.h>
using namespace std;
#define LL long long
vector<int> G[100010];
bool vis[100010];
LL a, b;
void dfs(int x,int st)
{
    for(int i = 0; i < G[x].size(); i++) {
        if(!vis[G[x][i]]) {
            vis[G[x][i]] = true;
            if(st&1) a++;
            else b++;
            dfs(G[x][i],st+1);
        }
    }
}
int main()
{
    int n;
    scanf("%d",&n);
    int x, y;
    for(int i = 0; i < n-1; i++) {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    vis[1] = true;
    a = 1, b = 0;
    dfs(1,0);
    printf("%lld\n",(a*b-n+1));
return 0;
}

C. Mahmoud and Ehab and the xor

题目链接

http://codeforces.com/contest/862/problem/C

题解

很多有意思的一道题
问是否有n个数异或的值为x
考察异或的性质 我们知道a^a = 0, x^0 =0,那么 根据这个性质 我们可以把前n-3个数字直接输出,然后 第n-2 ,n-1个为 1<<17 , 1<<18(防止和前面异或出来的值一样)

AC代码

#include <bits/stdc++.h>
using namespace std;

#define LL long long
#define CLR(a,b) memset(a,(b),sizeof(a))

const int MAXM = 1e3+10;
const int MAXN = 1e5+10;
const int mod = 1e9+7;

int main()
{
    int n, x;
    scanf("%d%d",&n,&x);
    if(n == 1) {
        puts("YES"); printf("%d\n",x); return 0;
    }
    if(n == 2) {
        if(x==0) {
            puts("NO");
        }
        else  {
            puts("YES");
            printf("0 %d\n",x);
        }
        return 0;
    }
    int ans = 0;
    puts("YES");
    for(int i = 1; i < n-2; i++) {
        printf("%d ",i);
        ans ^= i;
    }
    int k1 = 1<<18;
    int k2 = 1<<17;
    printf("%d %d %d\n",k1,k2,k1^k2^ans^x);


return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值