ZOJ4103 Traveler(2019浙江省赛)

构造+思维~

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+14;
int N,M,T;
int visit[maxn];
stack<int> st;
vector<int> vi;
int main () {
    visit[0]=1;
    scanf ("%d",&T);
    while (T--) {
        scanf ("%d",&N);
        for (int i=1;i<=N;i++) visit[i]=0;
        vi.clear();
        while (1) {
            if (N<=1) break;
            st.push(N);
            if (N%2) N=(N-1)/2-1;
            else N=N/2-1;
        }
        int now=1;
        while (1) {
            vi.push_back(now);
            visit[now]=1;
            if (!visit[now-1]) now--;
            else {
                if (st.empty()) break;
                else if (now*2==st.top()) {
                    now*=2;
                    st.pop();
                }
                else if (now*2+1==st.top()) {
                    now=now*2+1;
                    st.pop(); 
                }
                else {
                    if (!visit[now*2]) now=now*2;
                    else now=now*2+1;
                }
            }
        } 
        for (int i=0;i<vi.size();i++) {
            if (i!=0) printf (" ");
            printf ("%d",vi[i]);
        }
        printf ("\n");
    }
    return 0;
}

 

posted @ 2020-02-13 14:51  zlc0405  阅读(352)  评论(0编辑  收藏  举报