標籤:ack ring long names cin cassert with int pre
觀察範例得知就是和離‘_‘左邊最近的‘w‘交換位置,然後和離‘_‘右邊最近的‘b‘交換位置,輪流進行。
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <iomanip>#include <cstring>#include <map>#include <queue>#include <set>#include <cassert>#include <stack>#include <bitset>#define mkp make_pairusing namespace std;const double EPS=1e-8;typedef long long lon;const lon SZ=1030,INF=0x7FFFFFFF,mod=9901;void work(string str,string tmp,int n){ vector<int> res; int pos=str.find(‘_‘); for(int time=1;;++time) { if(str==tmp)break; for(int i=0;i<min(time,n);++i) { if(str==tmp)break; int dst; if(time&1)dst=str.find_last_of(‘w‘,pos); else dst=str.find(‘b‘,pos); if(dst==-1)break; //cout<<str<<endl; //cout<<dst<<endl; res.push_back(dst); swap(str[pos],str[dst]); pos=dst; } } for(int i=0;i<res.size();++i) { if(i%20!=0)cout<<" "; cout<<res[i]+1; if((i+1)%20==0)cout<<endl; }cout<<endl;}int main(){ std::ios::sync_with_stdio(0); //freopen("d:\\1.txt","r",stdin); lon casenum; //cin>>casenum; //for(lon time=1;time<=casenum;++time) { int n; cin>>n; string str(n,‘w‘); str+=‘_‘; str+=string(n,‘b‘); string tmp(n,‘b‘); tmp+=‘_‘; tmp+=string(n,‘w‘); work(str,tmp,n); } return 0;}
p2739 Shuttle Puzzle