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;
}

最新文章

  1. C++ 模版
  2. ASP.NET MVC Application_Error 无效不执行
  3. python virtualenv
  4. [RabbitMQ] Connection failed
  5. SQL 日期转换(阳历转阴历)
  6. Linux Kernel File IO Syscall Kernel-Source-Code Analysis(undone)
  7. firework便捷截LOGO
  8. mysql的部分基础知识....
  9. ios简单更改系统TabBar的高度
  10. 多个inline元素、block元素、inline-block元素在父容器中的换行情况
  11. 深度学习:Keras入门(二)之卷积神经网络(CNN)
  12. 已实现乐观锁功能,FreeSql.DbContext 准备起航
  13. hdu 3065 AC自动机 标记数组不清零
  14. Nginx详解二:Nginx基础篇之Nginx的优点
  15. python3 json.dump乱码问题
  16. HTML-JS 数组 内置对象
  17. 微软开源的Trill是什么?
  18. Swagger 常用注解
  19. Unity中巧用协程和游戏对象的生命周期处理游戏重启的问题
  20. PHP 获取上月,本月,近15天,近30天日期

热门文章

  1. 开源基于asio的网络通信框架asio2,支持TCP,UDP,HTTP,RPC,SSL,跨平台,支持可靠UDP,支持TCP自动拆包,TCP数据报模式等
  2. 状压入门--bzoj1087: [SCOI2005]互不侵犯King【状压dp】
  3. setsockopt()函数功能介绍
  4. 题解报告:hdu 2087 剪花布条(KMP入门)
  5. ActionEvent之TextField
  6. javaweb-JSP action中附件下载的写法
  7. [转]ASP.net MVC 2 自定义模板来显示数据
  8. [BZOJ2002][Hnoi2010]Bounce弹飞绵羊 LCT
  9. WPF 实时绘图的逻辑
  10. MySQL学习随笔--通过实例理解merge ,temptable算法的差异