集训第四周(高效算法设计)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; }

转载于:https://www.cnblogs.com/zsyacm666666/p/4707248.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值