Codeforces Round #563 (Div. 2)

A Ehab Fails to Be Thanos

题意:给2n个数字,问有没有一种排列使得前n个的和与后n个的和不相等
思路:若2n个数字都相同,则不存在这种排列,否则从小到大排序即可

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int n;
    cin >> n;
    int p[2*n+10];
    cin >> p[0];
    bool flag = true;
    for(int i = 1; i < 2*n ; i++){
        cin >> p[i];
        if(p[i] != p[i-1]) flag = false;
    }
    if(flag) cout << -1;
    else{
        sort(p, p+2*n);
        for(int i = 0; i < 2*n; i++){
            cout << p[i] << " ";
        }
    }

    cout << endl;
    return 0;
}

B. Ehab Is an Odd Person

题意:给一组数据,其中每一个数字可以与其奇偶性不同的数字交换位置,问经过排序后最终得到的字典序最小的序列
思路:若a,b为奇数,c为偶数,则可以讲ac呼唤,后将bc互换,然后再次将ac互换,则课表现为c位置没变,ab互换位置,由此可知,若同时存在奇数以及偶数则可以直接进行排序,若奇偶性缺少一种则不能进行排序

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
    int a = 0, b = 0;
    int n;
    int s[100005];
    cin >> n;
    for(int i = 0; i < n; i ++){
        cin >> s[i];
        if(s[i]%2) a++;
        else
            b++;
    }
    if(a&&b)
        sort(s, s+n);
    for(int i = 0 ; i < n; i++){
        cout << s[i] << " ";
    }
    cout << endl;
    return 0;
}

C.Ehab and a Special Coloring Problem

题意:你得到一个整数n。 对于从2到n的每个整数i,指定一个正整数ai,使得以下条件成立:

  • 对于任何整数对(i,j),如果i和j是互质的,则ai≠aj。
  • 应最小化所有ai的最大值(即尽可能小)。

思路:既然(i,j)互质能导致ai≠a,那么我们可以遍历2到n,若i为素数,则将i的倍数设置成与i相同的数字ai

#include <iostream>

using namespace std;

int s[100005];

int main(){
    int n;
    cin >> n;
    int cnt = 1;
    for(int i = 2; i <= n; i++){
        if(!s[i]){
            for(int j = 1; j*i <= n; j++){
                if(!s[j*i]) s[j*i] = cnt;
            }
            cnt++;
        }
    }
    for(int i = 2; i <= n; i++){
        cout << s[i] << " ";
    }
    cout << endl;
    return 0;
}

D. Ehab and the Expected XOR Problem

题意:
给定两个整数n和x,构造一个满足以下条件的数组:

  • 对于数组中的任何元素ai,1≤ai<2n;
  • 没有非空子段,按位XOR等于0或x,
  • 它的长度l应该最大化。

题目中非空子段的定义:a从头开始删除几个元素(可能是零或全部)并且从末尾删除几个(可能是零或全部)元素得到的子段。

思路:设从末尾删除n-i个元素,从头删除0个元素得到的子段的XOR和为bi,从XOR的性质可知al⊕al+1⋯⊕ar=bl−1⊕br,由此可推得ai = bi⊕bi-1。所以我们只需求出序列b即可。
跟据XOR性质若a⊕b = c,则a⊕c = b,且b⊕c = a,可知若确定a则b,c会成对出现。
对于x来说,放入元素i则抛弃元素i^x即可

#include <iostream>
#include <vector>

using namespace std;


bool p[(1<<18) + 10];

int main(){
    int n, x;
    cin >> n >> x;
    int cnt = 0;
    int k = 1 << n;
    vector<int> st;
    st.push_back(0);
    p[0] = true;
    for(int i = 1; i < k; i++){
        if(!p[i^x]){
            st.push_back(i);
            p[i] = true;
        }
    }
    cout << st.size()-1 << endl;
    for(int i = 1; i < st.size(); i++){
    }
    cout << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值