zoukankan      html  css  js  c++  java
  • [CF1415D] XOR-gun

    [CF1415D] XOR-gun

    Description

    给定一个长度为 N 的不下降序列,每次操作可以选择相邻的两个数,将这两个数替换成它们按位异或的结果。现在想要破坏序列的不降性质,求最少操作次数。

    Solution

    普遍来说,操作相邻两个区间 ([l,x] [x+1,r]) 可以完成任务,所以在 n 较小时可以枚举,这样至少操作两次

    当然,有些情况下,只需要一次操作

    性质:连续三个数的 highbit 相同,那么我们可以直接操作这三个数中的前两个

    而 n>60 时,一定能找到连续相同的三个

    具体到实现上,判断 highbit 其实有点麻烦,我们可以直接对连续的三个数尝试操作即可

    #include <bits/stdc++.h>
    using namespace std;
    
    #define int long long
    const int N = 1e6 + 5;
    
    int n;
    int a[N];
    
    signed main()
    {
        ios::sync_with_stdio(false);
    
        cin >> n;
    
        for (int i = 1; i <= n; i++)
            cin >> a[i];
    
        int ans = 1e9;
    
        for (int i = 2; i < n; i++)
        {
            int t = a[i - 1] ^ a[i];
            if (t < a[i - 2] || t > a[i + 1])
            {
                ans = 1;
            }
        }
    
        for (int i = 1; i <= n; i++)
            a[i] ^= a[i - 1];
    
        if (n < 66)
            for (int l = 1; l <= n; l++)
            {
                for (int r = l + 1; r <= n; r++)
                {
                    for (int k = l; k < r; k++)
                    {
                        if ((a[l - 1] ^ a[k]) > (a[k] ^ a[r]))
                            ans = min(ans, r - l - 1);
                    }
                }
            }
    
        cout << (ans > 1e8 ? -1 : ans) << endl;
    }
    
  • 相关阅读:
    [Effective C++]条款01:视C++为一个语言联邦
    DOTNET
    simulation
    掩码
    motorsimsrc
    Unprivileged User's Account
    LAN WAN
    calloc malloc realloc
    useradd
    change user ID and group ID
  • 原文地址:https://www.cnblogs.com/mollnn/p/14450328.html
Copyright © 2011-2022 走看看