簡體   English   中英

我們缺少 n>2 的一些條件,有人可以幫助找到我們的代碼缺少的特殊情況嗎?

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

問題陳述:

給定一個序列 A1、A2、...、AN,您必須執行以下操作正好 X 次:

  • 選擇兩個整數 i 和 j 使得 1≤i<j≤N。
  • 選擇非負 integer p。
  • 將 Ai 更改為 Ai⊕2p,並將 Aj 更改為 Aj⊕2p,其中⊕ 表示按位異或。

找到可以通過恰好執行此操作 X 次獲得的字典最小序列。

問題的副本可以是https://www.codechef.com/DEC20B/problems/HXOR

方法:

在這里,我們正在處理 X 次操作的 N 個整數,我們正在對數組中的所有整數執行 xor function,具有 2 個元素的冪 (2^p),p 表示非 -ve integer。

為了找到字典上最小的序列,我們對每個元素進行異或,直到它變得最小,然后我們轉移到下一個元素。 同時,我們還存儲了前面步驟中使用的元素來進行配對。

測試用例:

輸入:

4 3

2 2 3 3

Output:

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;
} 

建議將不勝感激,我已經為這個問題嘗試了將近 5 天,我不想要一個解決方案。 我只想知道我的代碼提供 WA 的一些特殊 TC。

這是一場持續的比賽。 所以這是我能幫助你的最好的。

輸入:

1
4 4
1 2 3 4

您的 output:

0 0 0 4

右 output:

0 0 1 5

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM