zoukankan      html  css  js  c++  java
  • 集训第四周(高效算法设计)P题 (构造题)

    Description

    Download as PDF
     

    There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<tex2html_verbatim_mark> . The N<tex2html_verbatim_mark> marbles are put in a circular track in an arbitrary order. In the top part of the track there is a ``lazy Susan", which is a tray that can hold exactly 4 marbles. The tray can be rotated, reversing the orientation of the four marbles. The tray can also be moved around the track in both directions.

    For example, 9 marbles 1, 9, 8, 3, 7, 6, 5, 4, 2 are put in the circular track in clockwise order as shown in the following figure. This figure also shows how the tray is moved and rotated.

    epsfbox{p4000.eps}<tex2html_verbatim_mark>

    Trung wants you to arrange the marbles by moving and rotating the tray so that when listing the marbles from some position in the track in clockwise order, we get (1, 2,..., N)<tex2html_verbatim_mark> . Your task is to write a program to tell Trung that either this can be done or not.

    Input 

    The input file consists of several data sets. The first line of the input file contains the number of data sets which is a positive integer and is not bigger than 100. The following lines describe the data sets.

    For each data set, the first line contains the integer N<tex2html_verbatim_mark>(8$ le$N$ le$500)<tex2html_verbatim_mark> . The second line describes the initial state of the track. It contains N<tex2html_verbatim_mark>numbers which are the labels of the marbles when listing in clockwise order.

    Output 

    For each test case, write in one line ``possible" if there exists a solution to arrange the marbles. If not so, write ``impossible".

    Sample Input 

    2 
    9 
    1 9 8 3 7 6 5 4 2 
    11  
    1 3 2 4 5 6 7 8 9 10 11
    

    Sample Output 

    possible 
    impossible

    这个题的如果执行结果是可行的话,必须满足两个条件之一:1.数组的长度为偶。2.数组的逆序数为偶。

    #include"iostream"
    using namespace std;
    
    const int maxn=500+10;
    
    int T[maxn];
    int a[maxn];
    
    long long sum;
    
    void merge_sort(int *a,int x,int y,int *T)
    {
        if(y-x>1)
        {
            int m=x+(y-x)/2;
            int p=x,q=m,i=x;
            merge_sort(a,x,m,T);
            merge_sort(a,m,y,T);
            while(p<m||q<y)
            {
                if(q>=y||(p<m&&a[p]<a[q])) T[i++]=a[p++];
                else {sum+=m-p;T[i++]=a[q++];}
            }
            for(int i=x;i<y;i++) a[i]=T[i];
        }
    }
    int main()
    {
        int n;
        int t;
        cin>>t;
        while(t--)
        {
            cin>>n;
            sum=0;
            for(int i=0;i<n;i++) cin>>a[i];
            merge_sort(a,0,n,T);
            if(sum%2==0||n%2==0) cout<<"possible"<<endl;
            else  cout<<"impossible"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    计算机编程语言有哪些?
    JS/Jquery遍历JSON对象、JSON数组、JSON数组字符串、JSON对象字符串
    原生js弹力球
    js中的位置属性
    javascript中常见的表单验证项
    深入理解系统调用
    计一次后怕的排错经历
    Oracle 11G ASM新加磁盘无法init disk
    Oracle需要清理的日志
    openstack-neutron
  • 原文地址:https://www.cnblogs.com/zsyacm666666/p/4707248.html
Copyright © 2011-2022 走看看