$ CH0503 $ odd digital issue

link

background

This problem is actually a \ (M \ N Times Puzzle \) , \ (POJ2893 \) is a weaker version, Li Yudong, Peking data structures and algorithms exam, \ (CH0503 \) .

The meaning of problems

Given \ (n-\ n-Times \) ( \ (n-\) is an odd integer, \ (. 3 \ n-leqslant \ leqslant 500 \) ) board, above \ (n \ times n-1 \) number and a space. Situation before and after a given number of two, each only allows the exchange of space around it and the adjacent (if any), asked whether there was a way to move the first situation for the second situation.

solution

The number of taken line by line on the board in a line (not take space), a sequence can be obtained.
Thinking mathematically operation. Exchange about the number of spaces and to reverse the sequence of numbers no effect. And spaces are vertically exchange sequences are exchanged \ (n-1 \) position number. Also, because \ (n-\) is an odd integer, so \ (n-1 \) must be an even integer, the reverse change before and after the switching must be an even number. Concrete evidence of a long process, not expand here. This means that the two situations can be another if and only if the number of reverse same as a parity from the operation into two sequences corresponding to the situation, must not be otherwise transformed.
Then the two merge sort situation seeking to reverse logarithmic. Time complexity is \ (O (^ n-2) \) . (Bottleneck read) request to reverse the time complexity of the array tree \ (O ((n-m +) \) \ (log \) \ (n-) \) ( \ (m \) is the value of the size range) , in this problem \ (m = 249 001 \) , it can also present problems. Like to write what you write what chant \ (QAQ \)

\(trick\)

\ (1 \) when there is no clue to operate the grid map, which was converted to a sequence operation, thinking mathematical sense.

\ (2 \) around the space and the digital exchange has no effect on the number of reverse sequence, up and down and a space switching sequence numbers are exchanged \ (n-1 \) position number, before and after switching of the number of reverse change must be an even number. (Important conclusion, keep in mind !!!)

detail

\ (1 \) sorting the scope of the problem is \ (1 \ sim n \ times n-1 \) instead of \ (. 1 \ n-SIM \) . (Good sucker mistakes ah)

\ (2 \) sets of data read in note empty. (Good sucker mistakes ah)

Code

$View$ $Code$
  
  
   
   
//省略头文件
using namespace std;
inline int read()
{
    int ret=0,f=1;
    char ch=getchar();
    while(ch>'9'||ch<'0')
    {
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        ret=(ret<<1)+(ret<<3)+ch-'0';
        ch=getchar();
    }
    return ret*f;
}
int n,tmp,k,rr,a[249005],b[249005],c[249005],ans1,ans2;
void msort1(int l,int r,int mid)
{
    if(l==r)
        return;
    msort1(l,mid,(l+mid)>>1);
    msort1(mid+1,r,(mid+1+r)>>1);
    int i=l,j=mid+1;
    for(register int k=l;k<=r;k++)
    {
        if(j>r||(i<=mid&&a[i]
   
   
    
    >1);
    msort2(mid+1,r,(mid+1+r)>>1);
    int i=l,j=mid+1;
    for(register int k=l;k<=r;k++)
    {
        if(j>r||(i<=mid&&b[i]
    
    
     
     >1);
        msort2(1,rr,(1+rr)>>1);
        if((ans1+ans2)&1)
            puts("NIE");
        else
            puts("TAK");
    }
    return 0;
}

    
    
   
   
  
  

Guess you like

Origin www.cnblogs.com/Peter0701/p/11250361.html