Codeforces Round #563 (Div. 2) 题解

施工中。。。

A. Ehab Fails to Be Thanos

题意

给你一个长度为 \(2n\) 的序列,重排该序列使得前一半和不等于后一半和。无解输出 \(-1\)

题解

显然如果所有数相等肯定无解,否则排序即可。

代码太丑不贴……

B. Ehab Is an Odd Person

题意

给你一个长度为 \(n\) 的序列 \(a_1,\dots,a_n\),若 \(a_i+a_j\) 为奇数你可以交换这两个数。求你能得到的字典序最小的序列。\(n\le 10^5\)

题解

显然如果所有数都是奇数或偶数答案为原数列。否则对于两个奇偶性相同的数我们可以藉由一个奇偶性不同的点交换,答案为排序后的序列。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int gi()
{
    char c; int x=0,f=1;
    for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
    for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
    return x*f;
}
const int N=1e5+5;
int n,a[N],sa[N],rk[N],sum1,sum2;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("b.in","r",stdin);
#endif
    n=gi();
    for(int i=1;i<=n;++i) a[i]=gi(),sum1+=(a[i]%2==0),sum2+=(a[i]%2);
    if(sum1&&sum2) sort(a+1,a+1+n);
    for(int i=1;i<=n;++i) printf("%d ",a[i]);
}

C. Ehab and a Special Coloring Problem

题意

构造一个长度为 \(n\) 的序列,使得下标互质的两个位置的数不等且最大值尽可能小。\(n\le 10^5\)

题解

直接跑筛法即可。

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int gi()
{
    char c; int x=0,f=1;
    for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
    for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
    return x*f;
}
const int N=2e5+5;
int a[N];
bool p[N];
int main()
{
#ifndef ONLINE_JUDGE
    freopen("c.in","r",stdin);
#endif
    int n; scanf("%d",&n);
    int cnt=0;
    for(int i=2;i*i<=n;++i)
    {
        if(!p[i])
        {
            a[i]=++cnt;
            for(int j=i*i;j<=n;j+=i) p[j]=true,a[j]=cnt;
        }
    }
    for(int i=2;i<=n;++i) printf("%d ",a[i]?a[i]:++cnt);
}

D. Ehab and the Expected XOR Problem

题意

给你 \(n,x\) ,构造一个尽可能长的每个数 \(\le 2^n\) 的序列使得不存在子段的异或和为 \(0\)\(x\)

\(1\le n\le 18, 1\le x < 2^{18}\)

题解

考虑原序列的前缀异或和 \(s_i\) ,条件转化为不存在 \(s_i \oplus s_j=x\)\(s_i=s_j\) ,即只能出现最多一个 \(s_i\)\(s_i\oplus x\)

code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int gi()
{
    char c; int x=0,f=1;
    for(;c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
    for(;c>='0'&&c<='9';c=getchar())x=(x<<1)+(x<<3)+c-'0';
    return x*f;
}
const int N=3e5+5;
bool vis[N];
int v[N],cnt;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("d.in","r",stdin);
#endif
    int n,x,lst=0;
    scanf("%d%d",&n,&x);
    vis[x]=true;
    for(int i=1;i<(1<<n);++i)
        if(!vis[i]) vis[i^x]=true,v[++cnt]=lst^i,lst=i;
    printf("%d\n",cnt);
    for(int i=1;i<=cnt;++i) printf("%d ",v[i]);
}

猜你喜欢

转载自www.cnblogs.com/farway17/p/10971355.html