AtCoder Beginner Contest 197(Sponsored by Panasonic)C - ORXOR

该博客主要讨论了一个数组中通过二进制枚举进行分组的问题,旨在找出某种条件下数组元素的最值。博主通过C++编写了代码,利用位操作来判断每个分组,并在每个间隙处可能放置的隔板,计算出每个可能的分组下区间内元素的异或和,最终找到最小的异或和。
摘要由CSDN通过智能技术生成

传送门

这题的关键是如何分组。

想象一下在一个数组中每两个数组间存在一个间隙,我们可以枚举每个间隙中是否存在一个“隔板”,当存在时就代表重新开始一个区间。可采用二进制枚举。

一共有n-1个间隙,共2^(n-1)种情况。

#include<bits/stdc++.h>
using namespace std;
 
typedef long long LL;         
typedef pair<int, int> P;
 
const int maxn = 2e6 + 10;        
const int M_MAX = 50000 + 10;
const int mod = 1e9 + 7;
const LL INF = 1e17;    
const double eps = 1e-6;     
 
// 2^20 =  2e6;
// 10! = 3e6;
 
int arr[2010];
 
void solve() {
    LL res = INF;
    int n; cin >> n;
    for(int i = 0; i < n; i++) cin >> arr[i];
    for(int i = 0; i < 1 << (n - 1); i++) {
        LL temp = 0, re = 0;
        for(int j = 0; j < n; j++) {
            temp |= arr[j];
            if(j == n - 1 || 1 << j & i) {
                re ^= temp;
                temp = 0;
            }
        }
        res = min(re, res);
    }
    cout << res << endl;
}
 
int main()
{
    ios::sync_with_stdio(false);
    //freopen("D:\\in.txt", "r", stdin);
    solve();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值