QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#401716 | #6667. Prosjek | Kevin5307 | 0 | 0ms | 0kb | C++23 | 3.8kb | 2024-04-29 10:56:47 | 2024-04-29 10:56:48 |
answer
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define ll long long
#define ull unsigned ll
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define pii pair<int,int>
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
#define longer __int128_t
void die(string S){puts(S.c_str());exit(0);}
mt19937_64 rnd(20210448);
vector<pair<ll,ll>> op;
bool dfs(vector<ll> vec)
{
if(sz(vec)==1) return true;
for(int i=0;i<sz(vec);i++)
for(int j=i+1;j<sz(vec);j++) if((vec[i]^vec[j]^1)&1)
{
op.pb(vec[i],vec[j]);
vector<ll> vec2;
for(int k=0;k<sz(vec);k++)
if(i!=k&&j!=k)
vec2.pb(vec[k]);
vec2.pb(vec[i]+vec[j]>>1);
if(dfs(vec2)) return true;
op.pop_back();
}
return false;
}
void brute_force(vector<ll> vec)
{
assert(dfs(vec));
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
op.clear();
int n;
cin>>n;
vector<ll> vec;
multiset<ll> ms[4];
for(int i=0;i<n;i++)
{
ll x;
cin>>x;
vec.pb(x);
ms[x&3].insert(x);
}
while(true)
{
if(sz(ms[0])&&sz(ms[2])) break;
if(sz(ms[1])&&sz(ms[3])) break;
int x=rnd()%4;
if(sz(ms[0])<2&&sz(ms[1])<2&&sz(ms[2])<2&&sz(ms[3])<2) break;
while(sz(ms[x])<2) x=rnd()%4;
ll val=rnd()%(*ms[x].rbegin());
ll A=*ms[x].upper_bound(val);
val=rnd()%(*ms[x].rbegin());
ll B=*ms[x].upper_bound(val);
ms[x].erase(ms[x].lower_bound(A));
ms[x].erase(ms[x].lower_bound(B));
op.pb(A,B);
ll C=A+B>>1;
ms[C&3].insert(C);
}
if(sz(ms[0])&&sz(ms[2]))
{
vector<ll> vec;
vec.pb(*ms[0].begin());
vec.pb(*ms[2].begin());
ms[0].erase(ms[0].begin());
ms[2].erase(ms[2].begin());
multiset<ll> ms2[2];
for(int i=0;i<4;i++)
for(auto x:ms[i])
ms2[i&1].insert(x);
while(sz(ms2[0])>1||sz(ms2[1])>1)
{
if(sz(ms2[0])>1)
{
ll A=*ms2[0].begin();
ll B=*ms2[0].rbegin();
ms2[0].erase(ms2[0].lower_bound(A));
ms2[0].erase(ms2[0].lower_bound(B));
ll C=A+B>>1;
op.pb(A,B);
ms2[C&1].insert(C);
}
else
{
ll A=*ms2[1].begin();
ll B=*ms2[1].rbegin();
ms2[1].erase(ms2[1].lower_bound(A));
ms2[1].erase(ms2[1].lower_bound(B));
ll C=A+B>>1;
op.pb(A,B);
ms2[C&1].insert(C);
}
}
for(int i=0;i<2;i++)
for(auto x:ms2[i])
vec.pb(x);
brute_force(vec);
}
else if(sz(ms[1])&&sz(ms[3]))
{
vector<ll> vec;
vec.pb(*ms[1].begin());
vec.pb(*ms[3].begin());
ms[1].erase(ms[1].begin());
ms[3].erase(ms[3].begin());
multiset<ll> ms2[2];
for(int i=0;i<4;i++)
for(auto x:ms[i])
ms2[i&1].insert(x);
while(sz(ms2[0])>1||sz(ms2[1])>1)
{
if(sz(ms2[0])>1)
{
ll A=*ms2[0].begin();
ll B=*ms2[0].rbegin();
ms2[0].erase(ms2[0].lower_bound(A));
ms2[0].erase(ms2[0].lower_bound(B));
ll C=A+B>>1;
op.pb(A,B);
ms2[C&1].insert(C);
}
else
{
ll A=*ms2[1].begin();
ll B=*ms2[1].rbegin();
ms2[1].erase(ms2[1].lower_bound(A));
ms2[1].erase(ms2[1].lower_bound(B));
ll C=A+B>>1;
op.pb(A,B);
ms2[C&1].insert(C);
}
}
for(int i=0;i<2;i++)
for(auto x:ms2[i])
vec.pb(x);
brute_force(vec);
}
if(sz(op)!=n-1)
cout<<-1<<'\n';
else for(auto pr:op)
cout<<pr.first<<" "<<pr.second<<'\n';
}
return 0;
}
詳細信息
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 0
Runtime Error
input:
99 4 739880851158065302 19206582949872932 883064254701115295 222072661880779376 7 148399819461615003 209088712310207988 53191076581680214 445068618251612752 230505279594622109 115754892157516718 804173775829453588 2 77979357045500669 41693388829622019 3 341612949433488278 609808714829036935 19994167...
output:
result:
Subtask #2:
score: 0
Runtime Error
Test #16:
score: 0
Runtime Error
input:
100 3 3 3 2 3 4 1 1 4 1 3 4 4 6 4 4 2 3 1 2 4 0 2 1 4 3 0 2 0 7 3 3 1 1 3 4 0 2 0 4 4 1 4 2 3 7 4 0 0 3 2 3 4 4 4 2 0 3 7 0 2 2 1 4 2 4 7 3 0 3 1 2 0 3 4 4 3 1 4 6 2 3 0 1 3 4 5 1 4 0 3 4 5 4 2 0 4 2 3 0 1 2 6 4 1 4 2 0 4 7 4 2 4 3 1 3 1 4 1 4 4 0 2 1 1 6 0 3 3 0 0 4 7 4 3 0 3 3 3 4 4 4 1 1 3 6 2 0 ...
output:
result:
Subtask #3:
score: 0
Runtime Error
Test #34:
score: 0
Runtime Error
input:
100000 5 846784256447769304 457696478728961702 128469521648960690 597630796847754190 104256763714529164 5 658897822238868978 472135077337628566 399538027669313322 622703684108675696 425723088635325654 5 917704960887390986 140817562615382054 877934664521057998 782212806618666818 616380973421914538 8 ...
output:
result:
Subtask #4:
score: 0
Runtime Error
Test #46:
score: 0
Runtime Error
input:
100 211957 911918942087402387 344230223346742784 16289402153237664 528890583619159010 886281719684850237 865484734102017297 321851390502278959 754268375474197153 237414161302130571 135637002716682378 824412491959977735 162505521049217610 246319278060116103 666703181591704279 650875500699154233 96397...
output:
31085673180124 999989589595986634 33205888651982 999967720480985884 34309878991530 999926091055107568 36334989799378 999921514477613516 43680954048084 999893240413030728 44955319228942 999830205792296778 46096509730566 999828945334385854 46513046285636 999827896360143458 50005704499586 9998174126302...