Description

 

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.

<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>(8N500)<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;
}

集训第四周(高效算法设计)P题 (构造题)的更多相关文章

  1. 集训第四周(高效算法设计)A题 Ultra-QuickSort

    原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...

  2. 集训第四周(高效算法设计)O题 (构造题)

    A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...

  3. 集训第四周(高效算法设计)N题 (二分查找优化题)

    原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...

  4. 集训第四周(高效算法设计)M题 (扫描法)

    原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...

  5. 集训第四周(高效算法设计)I题 (贪心)

    Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...

  6. 集训第四周(高效算法设计)E题 (区间覆盖问题)

    UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...

  7. 集训第四周(高效算法设计)D题 (区间覆盖问题)

    原题 UVA10020  :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...

  8. 集训第四周(高效算法设计)L题 (背包贪心)

    Description   John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...

  9. 集训第四周(高效算法设计)K题 (滑窗问题)

    UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...

随机推荐

  1. echarts 实例demo 详细讲解

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>     & ...

  2. 图的遍历---------开始开始-------o(∩_∩)o 哈哈

    图的遍历 深度优先搜索(Depth First Search , DFS) --深度优先搜索--我的理解就是分身术的另一种实现方法---用分身术将所有能看到的路都走一遍----这就是深度搜索--- 下 ...

  3. overlaps the location of another project Zendstudio导入已经存在的目录

    转 http://blog.csdn.net/kdchxue/article/details/50633745 最近弄zendstuido导入已经存在的项目,找了很多地方终于找到了导入的方法,特别记录 ...

  4. OpenCV2.4.9 + Ubuntu15.04配置

    为了run Car-Detection安装了OpenCV. 基本上就照着这个弄下来: ubuntu14.04 + OpenCV2.4.9 配置方法   1.  安装openCV 所需依赖库或软件: s ...

  5. Codeforces Beta Round #96 (Div. 2) (A-E)

    写份DIV2的完整题解 A 判断下HQ9有没有出现过 #include <iostream> #include<cstdio> #include<cstring> ...

  6. [转]asp.net MVC helper 和自定义函数@functions小结

    本文转自:http://www.cnblogs.com/jiagoushi/p/3904995.html asp.net Razor 视图具有.cshtml后缀,可以轻松的实现c#代码和html标签的 ...

  7. 不重启IIS修改dotnet framework版本

    因为公司现在存在.net站点和asp站点共同运行的情况,所以需要对IIS进行一些修改,运行环境Win2003+IIS6 一.起因 原来的老站是asp开发的,用的是.net 2.0运行环境; 新站是.n ...

  8. 关于react native在window下运行安卓的时候报 could not connect to development server

    当出现这种问题是网上的解答方案都是一模一样的! 我这边先给个地址讲的是解决方案http://blog.csdn.net/qq_25827845/article/details/52974991 但是我 ...

  9. 使用RecyclerView

    tags: 新建,模板,小书匠 RecyclerView 是 Android 团队新推出的控件,不仅能轻松实现 ListView 的同样的效果,还优化了 ListView 中许多不足之处. 目前 An ...

  10. Uml 建模 一(类图建模和startuml的使用)

    本文将分三个部分介绍Uml建模:Uml建模的作用.类图.startuml的使用 Uml的作用 本文以java为例介绍Uml,在当前的软件开发中大多数使用面向对象开发(OO),面向对象的就是将现实世界中 ...