简体   繁体   English

我们缺少 n>2 的一些条件,有人可以帮助找到我们的代码缺少的特殊情况吗?

[英]We are missing some condition for n>2, can someone help to find special cases which our code is missing?

Problem Statement:问题陈述:

You are given a sequence A1, A2,…, AN and you have to perform the following operation exactly X times:给定一个序列 A1、A2、...、AN,您必须执行以下操作正好 X 次:

  • Choose two integers i and j such that 1≤i<j≤N.选择两个整数 i 和 j 使得 1≤i<j≤N。
  • Choose a non-negative integer p.选择非负 integer p。
  • Change Ai to Ai⊕2p, and change Aj to Aj⊕2p, where ⊕ denotes bitwise XOR.将 Ai 更改为 Ai⊕2p,并将 Aj 更改为 Aj⊕2p,其中⊕ 表示按位异或。

Find the lexicographically smallest sequence which can be obtained by performing this operation exactly X times.找到可以通过恰好执行此操作 X 次获得的字典最小序列。

A copy of the question can be https://www.codechef.com/DEC20B/problems/HXOR问题的副本可以是https://www.codechef.com/DEC20B/problems/HXOR

Approach:方法:

Here, we are dealing with N integers for X times operations, we are performing the xor function for all integers in an array with the power of 2 elements (2^p), p represents the non -ve integer.在这里,我们正在处理 X 次操作的 N 个整数,我们正在对数组中的所有整数执行 xor function,具有 2 个元素的幂 (2^p),p 表示非 -ve integer。

For finding lexicographically smallest sequence, we are doing xor of each element until it becomes smallest and then we shift to the next element.为了找到字典上最小的序列,我们对每个元素进行异或,直到它变得最小,然后我们转移到下一个元素。 Meanwhile, we also store the elements used in the previous steps to make pairs.同时,我们还存储了前面步骤中使用的元素来进行配对。

Test Case:测试用例:

Input:输入:

4 3 4 3

2 2 3 3 2 2 3 3

Output: Output:

0 0 0 0 0 0 0 0

#include <bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);

#define ll long long

#define pb push_back


ll bs(vector<ll> check, ll key){
    ll n = check.size();
    for(ll i=0; i<n; i++)
    {
        if(check[i]==key)
        {
            return i;
        }
    }

    return -1;
}

ll highestPowerof2(ll n)
{
   ll p = (ll)log2(n);
   return (ll)pow(2, p);
}

void smallestSequence()
{
    ll t;
    cin >> t;
    while(t--)
    {
        ll n,x;
        cin >> n >> x;
        ll a[n];
        for(ll i=0;i<n;i++)
        {
            cin >> a[i];
        }

        if(n==2)
        {
            for(int i=1; i<n; i++)
            {
                a[i]^=a[0];
            }
            a[0]=0;

            if(x%2==0)
            {
                a[0]^=1;
                for(int i=1; i<n; i++)
                {
                    a[i]^=1;
                }   
            }
            
            for(ll i=0;i<n;i++)
            {
                cout << a[i] <<" ";
            }
            cout << endl;
            continue;
        }
        ll temp = x;
        x = 2*x;

        vector <ll> check;
        ll i;
        ll flag=0;
        for(i=0;i<n;i++)
        {
            ll p=1;
            while(a[i]!=0 && x>0)
            {
                if(bs(check, highestPowerof2(a[i]))>=0)
                {
                    check.erase(check.begin()+ bs(check, highestPowerof2(a[i])));
                }
                else{
                    if(temp>0)
                    {
                        check.pb(highestPowerof2(a[i]));
                        temp--;
                    }else {
                        ll j = 0;
                        ll si = check.size();
                        for(j=0; j<si; j++)
                        {
                            ll res = check[j]^a[i];
                            if(res<=a[i])
                            {
                                a[i]^=check[j];
                                x--;
                                check.erase(check.begin()+j);
                                j--;
                                si--;
                            }
                        }
                        break;
                    }
                }
                a[i]^=highestPowerof2(a[i]);
                x--;
            }

            if(a[i]!=0)
            {
                flag=1;
            }
        }
        
        // if(flag==0 && temp>0 && (temp*2)==x)
        // {
        //  if(temp%2==1)
        //  {
        //      a[n-1]=a[n-2]=1;
        //  }
        // }

        while(x>=0 && !check.empty())
        {
            a[i-1]^=check[0];
            check.erase(check.begin());
        }

        for(ll i=0;i<n;i++)
        {
            cout << a[i] <<" ";
        }
        cout << endl;
    }
}


int main()
{
    fast;

    smallestSequence();

    return 0;
} 

Suggestions will be appreciated, I had been trying for this problem for almost 5 days now, I don't want a solution.建议将不胜感激,我已经为这个问题尝试了将近 5 天,我不想要一个解决方案。 I just want to know some special TCs where my code is giving WAs.我只想知道我的代码提供 WA 的一些特殊 TC。

This is a on going contest.这是一场持续的比赛。 So this is the best i could help you.所以这是我能帮助你的最好的。

Input:输入:

1
4 4
1 2 3 4

Your output:您的 output:

0 0 0 4

Right output:右 output:

0 0 1 5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM