[X] Dragonball problem solution

Tomorrow evening I like to test the list to understand the next.

Topic Link

solution:

For this question, because the front and rear to reconnect them, so we consider the list.

Let's use the list to maintain normal relations. We then descending enumeration.

For this number, if it is behind several (because it is one-entering), put them into a queue pressure (output to)

This will use the list to complete the process of a greedy.

Code:

 

#include<list>
#include<iostream>
#include<cstdio>
using namespace std;
int head[500000],Next[500000];
int n,a[500000];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;++i){
        scanf("%d",&a[i]);
        head[a[i]]=a[i-1];
        Next[a[i-1]]=a[i];
    }
    for(int i=n;i>=1;--i){
        if(Next[i]){//这里是i,故从小到大。 
            printf("%d %d ",i,Next[i]);
            Next[head[i]]=Next[Next[i]];
            head[Next[Next[i]]]=head[i];//i Next[Next[i]].head=head[i].
            Next[Next[i]]=0;//即 head[Next[Next[i]]]=head[i]. 
        }
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/h-lka/p/11227657.html