Codeforces Round #717 (Div. 2)

https://codeforces.com/contest/1516

A. Tit for Tat

题意:给出n个数组成的序列a,可进行至多k次操作,每次操作选择两个数,一个+1另一个-1(操作过程中保证所有数非负),使得a的字典序最小,输出a。

思路:简单贪心,让最后一个数前面的数尽量小,多出来的全加到最后一个数上。

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

const int maxn = 111;

int n, k, t;
int a[maxn];

int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n >> k;
        for(int i = 1;i <= n; i++) cin >> a[i];
        for(int i = 1;i < n; i++)
        {
            if(k == 0) break;
            if(a[i] == 0) continue;
            if(a[i] >= k)
            {
                a[i] -= k;
                a[n] += k;
                k = 0;
            }
            else
            {
                k -= a[i];
                a[n] += a[i];
                a[i] = 0;
            }
        }
        for(int i = 1;i <= n; i++) cout << a[i] << ' ';
        cout << endl;
    }
    return 0;
}

B. AGAGA XOOORRR

题意:给出n个数组成的序列a,每次可选择删掉两个数并将这两个数的异或加入序列a中,问是否可以通过多次操作使得序列a的数均相等且个数 >= 2。

思路:首先我们要明确两点:①异或和顺序无关;②0 ^ x = x ; x ^ x = 0; 也就是说分两种情况:①如果所有数异或起来是0,就说明多次操作之后可以获得偶数个相等的数x;人头如果不是0,设其为x,就说明除了x之外的其他数,可以通过异或操作变成一个0,即除x之外的其他数满足情况①,此时,当且仅当除x外的其他数可以通过异或操作变成x且无剩余,才满足题意,反之不满足。

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

const int maxn = 2010;

int t, n;
int a[maxn];

int main()
{
    cin >> t;
    while(t--)
    {
        cin >> n;
        for(int i = 1;i <= n; i++) cin >> a[i];
        int ans = 0;
        for(int i = 1;i <= n; i++) ans ^= a[i];
        if(ans == 0) puts("YES");
        else
        {
            int res = 0, cnt = 0;
            for(int i = 1;i <= n; i++)
            {
                res ^= a[i];
                if(res == ans)
                {
                    cnt++;
                    res = 0;
                }
            }
            if(cnt >= 2 && res == 0) puts("YES");
            else puts("NO");
        }
    }
    return 0;
}

C. Baby Ehab Partitions Again

题意:给出n个数组成的序列a,删除最少的数使得剩下的序列a不能划分成两个元素和相等的子序列。

思路:借鉴了大佬的博客,下面是链接:
这里是大佬博客的链接
首先想到的是,所有数的和是奇数就满足题意了,直接输出0;
或者是通过可行性背包看一下能否构成sum / 2,不能的话也直接输出0;
然后是可以拆分,如果我们可以减去一个奇数的话,剩下的数的和就是奇数,所以满足题意,但是不只是奇数——如果把所有数提取2的整数次幂,因为题目保证有解,所以把2除干净之后必有一个奇数,删掉它就好啦_(:з」∠)_
这思路好妙,感觉自己没啥思维= =

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

const int maxn = 2020, N = 1e6 + 10;

int lowbit(int x)
{
    return x & (-x);
}

int n;
int a[maxn], dp[N];

int main()
{
    cin >> n;
    int sum = 0;
    for(int i = 1;i <= n; i++)
    {
        cin >> a[i];
        sum += a[i];
    }
    dp[0] = 1;
    for(int i = 1;i <= n; i++)
    {
        for(int j = sum;j >= a[i];j--)
        {
            dp[j] |= dp[j - a[i]];
        }
    }
    if(sum % 2 == 1 || dp[sum / 2] == 0) puts("0");
    else
    {
        int ans = 0x3f3f3f3f, pos = -1;
        for(int i = 1;i <= n; i++)
        {
            int t = lowbit(a[i]);
            if(t < ans)
            {
                ans = t;
                pos = i;
            }
        }
        puts("1");
        cout << pos << endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值