1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| #include<bits/stdc++.h> using namespace std; #define ll long long #define ld long double inline bool isprime(ll num) {if(num==2||num==3)return true; if(num%6!=1&&num%6!=5)return false; for(int i=5;1ll*i*i<=num;i+=6){if(num%i==0||num%(i+2)==0)return false;} return true;} const int mod = 1e9+7; inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;} inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll g = exgcd(b,a%b,y,x);y-=a/b*x;return g;} inline ll quick_pow(ll a,ll b,ll mod){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;} inline ll quick_pow(ll a,ll b){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;} inline ll inv(ll x){return quick_pow(x,mod-2);} inline ll inv(ll x,ll mod){return quick_pow(x,mod-2,mod);} inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} const int N = 5e3+10; vector<int>a,b,res; int aa[N],bb[N]; bool check(int k,int lim){ int res=0,j=0; while(res<lim&&j<lim){ if(bb[j]>aa[res])res++; j++; } return res>=k; } int main(){ int n,x; scanf("%d",&n); for(int i=0;i<n;i++)scanf("%d",&aa[i]),a.push_back(aa[i]); for(int i=0;i<n;i++)scanf("%d",&bb[i]),b.push_back(bb[i]); sort(aa,aa+n); sort(bb,bb+n); sort(b.begin(),b.end()); int ans = 0,dn =0; while(ans<n&&dn<n){ if(bb[dn]>aa[ans])ans++; dn++; } for(int i=0;i<n;i++){ int pos = -1; int blen=upper_bound(b.begin(),b.end(),a[i])-b.begin(); int aj=0; for(int j=i+1;j<n;j++)aa[aj++]=a[j]; sort(aa,aa+aj); int l=blen,r=b.size()-1; while(l<=r){ int mid = l+r>>1; int bj=0; for(int j=0;j<b.size();j++){if(j!=mid)bb[bj++]=b[j];} if(check(ans-1,bj)){ pos=mid; l=mid+1; }else r=mid-1; } if(pos!=-1)ans--; else{ l=0; r=blen-1; while(l<=r){ int mid = l+r>>1; int bj=0; for(int j=0;j<blen;j++)if(j!=mid)bb[bj++]=b[j]; if(check(ans,bj)){ pos=mid; l=mid+1; }else r=mid-1; } } res.push_back(b[pos]); b.erase(b.begin()+pos); } for(auto k : res){ printf("%d ",k); } }
|