题解:数组a存储答案,数组p存储剩余位置,每次的到抛弃的数d,如果d大于其中一个则数组满足,放置位置数组p更新为0,数组a更新为d,空余的位置更新为max(d);
另外 ,每次输入d迭代下最大值ma,与最后胜利者比较。
代码:
点击查看代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PLL;
#define IOS cin.tie(nullptr)->sync_with_stdio(false);
#define se second
#define fi first
#define mem(a,b) memset(a,b,sizeof a);
#define pri priority_queue<int,vector<int>,greater<int> >
#define low(a,b,c) lower_bound(a,a+b,c)-a;
#define upp(a,b,c) upper_bound(a,a+b,c)-a;
const int N=1e7+7;
int p[N];
int a[N];
void solve()
{
int k,d;
cin>>k;
int flag=0;
int n=(1<<k);
//n=n;
// cout<<n;
int ma=0;
for(int i=1;i<=n/2;i++) p[i]=i*2-1;
for(int i=1;i<=n;i+=2)
{
cin>>d;
ma=max(d,ma);
a[i]=a[i+1]=d;
}
for(int i=k-2;i>=0;i--)
{
int w=1<<i;
// cout<<w<<endl;
int s=1;
for(int j=1;j<=w;j++)
{
cin>>d;
ma=max(ma,d);
int a1=a[p[s]];
int a2=a[p[s+1]];
// cout<<p[s]<<" "<<p[s+1]<<" ";
if(d>=a1)
{
a[p[s]]=d;
p[s]=0;
a[p[s+1]]=max(a[p[s+1]],d);
}
else if(d>=a2)
{
a[p[s+1]]=d;
p[s+1]=0;
a[p[s]]=max(a[p[s]],d);
}
else
flag=1;
s+=2;
}
//cout<<endl;
int o=0;
for(int j=1;j<=w*2;j++)
{
if(p[j]) p[++o]=p[j];
}
}
int sp;
cin>>sp;
if(sp<ma) flag=1;
a[p[1]]=sp;
if(flag) cout<<"No Solution\n";
else
{
cout<<a[1];
for(int i=2;i<=(1<<k);i++) cout<<" "<<a[i];
}
}
int main()
{
int t=1;
while(t--)
{
solve();
}
return 0;
}