Codeforces Round #563 (Div. 2)

A. Ehab Fails to Be Thanos
题意:给一个长度为 2 n 2n 2n的数组,问是不是能否重排数组,使得数组前 n n n个数之和和后 n n n个数之和不同

解:如果数组每个元素都相同,那么就肯定不能了,反之就直接一个sort即可

#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
const ll MAX = 1e3+5;
ll arr[MAX << 1];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;     cin >> n;
    for(int i = 0; i < (n<<1); ++i){
        cin >> arr[i];
    }
    sort(arr,arr+2*n);
    ll tot = 0,tot2= 0;
    for(int i = 0; i < n; ++i){
        tot+= arr[i];
        tot2 += arr[i+n];
    }
    if(tot==tot2){
        cout << -1 << endl;
    }
    else{
        for(int i = 0; i < 2*n; ++i){
            if(i)   cout << ' ';
            cout << arr[i];
            if(i==2*n-1)    cout << endl;
        }
    }

    return 0;
}

B题:
题意: 定义一种操作:Pick two integers i and j (1≤i,j≤n) such that a i a_i ai+ a j a_j aj is odd, then swap a i a_i ai and a j a_j aj.
即在数组中选出两个数,如果两者相加的是奇数,那么就可以交换,问能否使得该数组有序。

解:
如果一个数组里只有奇数或者只有偶数,那么数组就不能交换,所以就直接输出即可了
反之只要一个数组里又有奇数又有偶数,那么就能随意交换,所以sort之后输出即可。

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll;
const ll MAX = 1e5+5;
ll arr[MAX];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    int n;  cin >> n;
    bool haveodd = false,haveeven = false;
    for(int i = 0; i < n; ++i){
        cin >> arr[i];
        if(arr[i]&1) haveodd = true;
        if(arr[i]%2==0) haveeven = true;
    }
    if(haveodd && haveeven){
        sort(arr,arr+n);
        for(int i = 0; i < n; ++i){
            if(i)   cout << ' ';
            cout << arr[i];
            if(i==n-1)  cout << endl;
        }
    }
    else{
        for(int i = 0; i < n; ++i){
            if(i)   cout << ' ';
            cout << arr[i];
            if(i==n-1)  cout << endl;
        }
    }
    return 0;
}

C题:
题意: 构造一个序列 a a a,如果其中的 i i i j j j 是互质的,满足 a i ! = a j a_i != a_j ai!=aj,还得使 a i a_i ai尽量小

#include <iostream>
using namespace std;
const int MAX = 1e5+5;
bool prime[MAX];
int a[MAX];
int main()
{
    int n;  cin >> n;
    int cnt = 0;
    for(int i = 2; i*i <= n; ++i){
        if(!prime[i]){
            a[i] = ++cnt;
            for(int j = i*i; j <= n; j+=i)  prime[j] = true,a[j] = cnt;
        }
    }
    for(int i = 2; i <= n; ++i){
        if(i>2) cout << ' ';
        cout << (a[i] ? a[i] : ++cnt);
    }
    cout << endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值