Test instructions: There is a bunch of data, some sample data (assuming x), some big data (assuming y), but the naming of these data files is very confusing. Want you to give them one by one to rename, guarantee at any time without the name of the file under the premise, so that the sample data named 1~x, Big Data named X+1~x+y.
The unused name is pressed into two stacks first.
Divided into three rounds: the first round to occupy the other name of the sample data and occupy the other name of the big data into two queues, and then repeatedly try to queue the two queues, each time will occupy the name of the other person into an unused correct name (removed from the stack), and then press the name of the other stack. Since each data will only be out of the queue once, it is O (n).
The second round tries to make the incorrect file name as correct as possible until the stack is empty, just loop once.
The rest must be the case:
Formed a cycle of occupation, assuming that the remaining n, only need to use n+1 times to rename, similar to this.
WA 144 Note: The initial file name may contain leading zeros.
#include <cstdio> #include <queue> #include <stack> #include <iostream> #include <string> Using namespace Std;int lim;bool used[100005];int trans (string Name,int type) {int len=name.length (); for (int i=0;i< Len;++i) {if (name[i]< ' 0 ' | | name[i]> ' 9 ') {return-1;}} if (name[0]== ' 0 ') {return-1;} int shu=0;for (int i=0;i<len;++i) {shu=shu*10+name[i]-' 0 ';} Return Shu;} struct Data{int id;string name;int type;data (const int &ID,CONST string &name,const int &type) {this->id= Id;this->name=name;this->type=type;} Data () {}}a[100005];stack<int>st[2];queue<data>q[2];int N;bool Changed[100005];queue<string>ans [2];int path[15],e;string Trans (int x) {e=0;string res= ""; while (x) {path[++e]=x%10;x/=10;} for (int i=e;i>=1;--i) {res+= (path[i]+ ' 0 ');} return res;} int Anss;int Main () {//freopen ("c.in", "R", stdin), scanf ("%d", &n), for (int i=1;i<=n;++i) {Cin>>a[i].name >>a[i].type;a[i].id=i;if (a[i].type==1) {++lim;}} for (int i=1;i<=n;++i) {if (A[i]. type==1) {int T=trans (a[i].name,a[i].type), if (t>=1 && t<=n) used[t]=1;if (T>lim && t<=n) { Q[1].push (A[i]);}} Else{int T=trans (A[i].name,a[i].type); if (t>=1 && t<=n) used[t]=1;if (T<=lim && t>=1) {q[0] . push (A[i]);}} for (int i=1;i<=n;++i) {if (!used[i]) {if (I<=lim) {St[1].push (i);} Else{st[0].push (i);}}} while (1) {bool Flag=0;while (!q[1].empty ()) {Data U=q[1].front (), if (St[1].empty ()) {break;} int To=st[1].top (); St[1].pop (); Ans[0].push (u.name); Ans[1].push (trans (to)); ++anss;changed[u.id]=1;flag=1;st[0].push (trans (U.name, U.type)); Q[1].pop ();} while (!q[0].empty ()) {Data U=q[0].front (), if (St[0].empty ()) {break;} int To=st[0].top (); St[0].pop (); Ans[0].push (u.name); Ans[1].push (trans (to)); ++anss;changed[u.id]=1;flag=1;st[1].push (trans (U.name, U.type)); Q[0].pop ();} if (!flag) {break;}} int tmp=0;for (int i=1;i<=n;++i) {if (!changed[i]) {if (a[i].type==1) {int T=trans (a[i].name,a[i].type); T<=lim && t>=1) {if (!st[1].empty ()) {Ans[0].push (a[i].name); Ans[1].push (trans (St[1].top ())); ++anss;++tmp;changed[i]=1;st[1].pop ();}}} Else{int T=trans (A[i].name,a[i].type); T>lim && t<=n) {if (!st[0].empty ()) {Ans[0].push (a[i].name); Ans[1].push (trans (St[0].top ())); ++anss;+ +tmp;changed[i]=1;st[0].pop ();}}}} if (a[1].name== "7x6kel") {//for (int i=1;i<=n;++i) {//if (trans (a[i].name,a[i].type) >=1 && Trans (A[i]. Name,a[i].type) <=n) {//cout<<a[i].name<<endl;//}//}//printf ("%d\n", tmp);//return 0;//}while (!q[1] . empty ()) Q[1].pop (), while (!q[0].empty ()) Q[0].pop (), for (int i=1;i<=n;++i) {if (!changed[i]) {if (a[i].type==1) { int T=trans (a[i].name,a[i].type); T>=1 && T<=lim) {Q[1].push (a[i]);}} Else{int T=trans (A[i].name,a[i].type); T>lim && t<=n) {Q[0].push (a[i]);}}}} String Last;int cnt=0;//if (Q[0].size ()!=q[1].size ()) {//return 0;//}while (!q[0].empty ()) {++cnt;data U=q[1].front (), V=q[0].front (); Ans[0].push (u.name); if (cnt==1) {Ans[1].push (trans (n+1));} Else{ans[1].push (last);} Last=u.naMe;ans[0].push (V.name); Ans[1].push (last); Last=v.name;anss+=2;q[0].pop (); Q[1].pop ();} if (CNT) {Ans[0].push (trans (n+1)); Ans[1].push (last); ++anss;} printf ("%d\n", Anss), while (!ans[0].empty ()) {cout<< "Move" <<ans[0].front () << "<<ans[1". Front () <<endl;ans[0].pop (); Ans[1].pop ();} return 0;}
Impersonation Codeforces Round #434 (Div. 1, based on Technocup 2018 Elimination Round 1) c. Tests renumeration