Codeforces Round #563 (Div. 2)

CF

题目链接:https://codeforces.com/contest/1174

总结

ABC都比较水,27分钟写完了,但是D题想偏了,一直WA,AC代码确实太巧妙了。

A题

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
int n,sum1,sum2,a[N];
template<typename T>void read(T&x)
{
    ll k=0; char c=getchar();
    x=0;
    while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
    if (c==EOF)exit(0);
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("aa.in","r",stdin);
#endif
    read(n);
    for(int i=1;i<=2*n;i++)read(a[i]);
    sort(a+1,a+n*2+1);
    for(int i=1;i<=n;i++)sum1+=a[i];
    for(int i=n+1;i<=2*n;i++)sum2+=a[i];
    if (sum1==sum2){printf("-1");return 0;}
    for(int i=1;i<=2*n;i++)printf("%d ",a[i]);

B题

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
int n,sum1,sum2,a[N],f[3];
template<typename T>void read(T&x)
{
    ll k=0; char c=getchar();
    x=0;
    while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
    if (c==EOF)exit(0);
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("aa.in","r",stdin);
#endif
    read(n);
    for(int i=1;i<=n;i++)read(a[i]),f[a[i]%2]++;
    if (f[0]>0&&f[1]>0)sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)printf("%d ",a[i]);
}

C题

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 100050
int n,ans,a[N],f[N];
template<typename T>void read(T&x)
{
    ll k=0; char c=getchar();
    x=0;
    while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
    if (c==EOF)exit(0);
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("aa.in","r",stdin);
#endif
    read(n);
    for(int i=2;i<=n;i++)
    {
        if (f[i]==0)
        {
            ans++;
            f[i]=ans;
            int x=i;
            while(x+i<=n)f[x+i]=ans,x+=i;
        }
    }
    for(int i=2;i<=n;i++)printf("%d ",f[i]);
}

D题

题意:

一道构造题,给你n,x 构造尽量长的序列\({a_l}\),满足:

\[
\begin{aligned}
&\bullet 1\le a_i\le 2^n \ &\bullet 没有一个子序列的异或和等于零或x
\end{aligned}
\]

题解

首先假设我们已经知道了这个序列,另s[i]表示i的亦或前缀和,那么对于任意一段子序列(i,j)都可以用s[i]^s[j]表示,

所以也就是不存在s[i]^s[j]=0或x。

所以我们可以先构造s[i]数组,不难发现一个性质:s[i]一定小于\(2^n\),我们从\(1->2^n\)枚举,如果s[i]=k,那么k^x和k就不能再出现。

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 1000050
int n,num,f[N],a[N];
template<typename T>void read(T&x)
{
    ll k=0; char c=getchar();
    x=0;
    while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
    if (c==EOF)exit(0);
    while(isdigit(c))x=x*10+c-'0',c=getchar();
    x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("aa.in","r",stdin);
#endif
    int x;
    read(n); read(x);
    int all=(1<<n)-1;
    f[x]=1;
    for(int i=1;i<=all;i++)if (!f[i]){a[++num]=i;f[i^x]=1;}

    printf("%d\n",num);
    for(int i=1;i<=num;i++)
        printf("%d ",a[i]^a[i-1]);
}

原文地址:https://www.cnblogs.com/mmmqqdd/p/10986757.html

时间: 2024-08-30 17:09:55

Codeforces Round #563 (Div. 2)的相关文章

Codeforces Round #563 (Div. 2)C

C. Ehab and a Special Coloring Problem 题目链接:http://codeforces.com/contest/1174/problem/C 题目 You're given an integer n. For every integer i from 2 to n, assign a positive integer ai such that the following conditions hold: For any pair of integers (i,

Codeforces Round #563 (Div. 2)/Codeforces1174

CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i\)与\(\sum\limits_{n+1}^{2n}a_i\)差值最大,排一下序就好了 CF1174B Ehab Is an Odd Person 一个显然的性质就是如果至少有一个奇数和一个偶数,那么是可以随意调整的,也就是升序排序 否则不可以进行任何操作 CF1174C Ehab and a Special Coloring Problem 把每个质数预处理出来,\(x

Codeforces Round 563 (Div. 2) 题解

自己开了场镜像玩. 前三题大水题.D有点意思.E完全不会.F被题意杀了……然而还是不会. 不过看过(且看懂)了官方题解,所以这里是六题题解齐全的. A 水题.给原序列排序,如果此时合法则直接输出,否则说明所有数相同,一定无解. 时间复杂度 $O(n\log n)$. #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> PII; const int maxn=

Codeforces Round #563 (Div. 2) D、Ehab and the Expected XOR Problem

D. Ehab and the Expected XOR Problem Given two integers n and x, construct an array that satisfies the following conditions: for any element ai in the array, 1≤ai<2^n there is no non-empty subsegment with bitwise XOR equal to 0 or x, its length l sho

Codeforces Round #563 (Div. 2)C. Ehab and a Special Coloring Problem

原文链接:传送门思路:素数筛代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstring" 4 using namespace std; 5 long long a[2000006],n; 6 int main(){ 7 cin>>n; 8 long long flag = 1; 9 memset(a,0,sizeof(a)); 10 for(int i=2;i&l

Codeforces Round #563 (Div. 2)B;Ehab Is an Odd Person

原文链接:任意门 题目大意:给你一组数,让你交换两个数的位置,让它们的和为奇数,且使其交换后,顺序满足最小字典序列.思路:这就是一道狗题,看代码,你就会******了,只需要sort排序.代码: 1 #include"iostream" 2 #include"algorithm" 3 #include"cstdio" 4 #include"cstring" 5 long long a[10000006],n,js=0,os=0

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i

Codeforces Round #424 (Div. 2) D. Office Keys(dp)

题目链接:Codeforces Round #424 (Div. 2) D. Office Keys 题意: 在一条轴上有n个人,和m个钥匙,门在s位置. 现在每个人走单位距离需要单位时间. 每个钥匙只能被一个人拿. 求全部的人拿到钥匙并且走到门的最短时间. 题解: 显然没有交叉的情况,因为如果交叉的话可能不是最优解. 然后考虑dp[i][j]表示第i个人拿了第j把钥匙,然后 dp[i][j]=max(val(i,j),min(dp[i-1][i-1~j]))   val(i,j)表示第i个人拿

Codeforces Round #424 (Div. 2) C. Jury Marks(乱搞)

题目链接:Codeforces Round #424 (Div. 2) C. Jury Marks 题意: 给你一个有n个数序列,现在让你确定一个x,使得x通过挨着加这个序列的每一个数能出现所有给出的k个数. 问合法的x有多少个.题目保证这k个数完全不同. 题解: 显然,要将这n个数求一下前缀和,并且排一下序,这样,能出现的数就可以表示为x+a,x+b,x+c了. 这里 x+a,x+b,x+c是递增的.这里我把这个序列叫做A序列 然后对于给出的k个数,我们也排一下序,这里我把它叫做B序列,如果我