集训第四周(高效算法设计)P题 (构造题)
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.
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>(8N
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;
}
集训第四周(高效算法设计)P题 (构造题)的更多相关文章
- 集训第四周(高效算法设计)A题 Ultra-QuickSort
原题poj 2299:http://poj.org/problem?id=2299 题意,给你一个数组,去统计它们的逆序数,由于题目中说道数组最长可达五十万,那么O(n^2)的排序算法就不要再想了,归 ...
- 集训第四周(高效算法设计)O题 (构造题)
A permutation on the integers from 1 to n is, simply put, a particular rearrangement of these intege ...
- 集训第四周(高效算法设计)N题 (二分查找优化题)
原题:poj3061 题意:给你一个数s,再给出一个数组,要求你从中选出m个连续的数,m越小越好,且这m个数之和不小于s 这是一个二分查找优化题,那么区间是什么呢?当然是从1到数组长度了.比如数组长度 ...
- 集训第四周(高效算法设计)M题 (扫描法)
原题:UVA11078 题意:给你一个数组,设a[],求一个m=a[i]-a[j],m越大越好,而且i必须小于j 怎么求?排序?要求i小于j呢.枚举?只能说超时无上限.所以遍历一遍数组,设第一个被减数 ...
- 集训第四周(高效算法设计)I题 (贪心)
Description Shaass has n books. He wants to make a bookshelf for all his books. He wants the bookshe ...
- 集训第四周(高效算法设计)E题 (区间覆盖问题)
UVA10382 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21419 只能说这道题和D题是一模一样的,不过要进行转化, ...
- 集训第四周(高效算法设计)D题 (区间覆盖问题)
原题 UVA10020 :http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19688 经典的贪心问题,区间上贪心当然是右区间越 ...
- 集训第四周(高效算法设计)L题 (背包贪心)
Description John Doe is a famous DJ and, therefore, has the problem of optimizing the placement of ...
- 集训第四周(高效算法设计)K题 (滑窗问题)
UVA 11572 唯一的雪花 题意:给你从1到n的数组,要求求得其中的最长连续不重复子序列,经典的滑窗问题,方法是维护一个窗口,设置左框和右框,然后不断的进行维护和更新 方法一: #include& ...
随机推荐
- macbook 快捷键 home ...
home和end是fn+左右,ctrl+home和end是fn+cmd+左右
- Service官方教程(10)Bound Service的生命周期函数
Managing the Lifecycle of a Bound Service When a service is unbound from all clients, the Android sy ...
- this关键字的构造方法的使用
package com.wh.Object3; public class this_Demo { private String name; private double price; private ...
- 【转】Java中,&&与&,||与|的区别
转自:http://blog.csdn.net/lishiyuzuji/article/details/8116516 在Java的逻辑运算符中,有这么四类:&&(短路与),& ...
- jQuery相关知识总结
1 encodeURIComponent(city)处理js传值乱码问题 2 总体概述 以后项目如果没有特殊情况,一般采用jQuery作为最基础的公共底层库. 另外对于前端的javascript相关的 ...
- Java编程思想总结笔记Chapter 5
初始化和清理是涉及安全的两个问题.本章简单的介绍“垃圾回收器”及初始化知识. 第五章 初始化与清理 目录:5.1 用构造器确保初始化5.2 方法重载5.3 默认构造器5.4 this关键字5.5 清 ...
- 阿里云虚拟主机的域名添加https的方法
第一步:购买CDN套餐,阿里云虚拟主机目前是不支持https的,不过可以通过阿里云的CDN服务来跳转一下实现部署https 静态HTTPS请求数根据你的网站访问量来选择 第二步:申请SSL证书服务,有 ...
- 解决max解析记录与cname不能共存的问题
问题描述: 在腾讯上做了域名邮箱解析,需要将max记录绑定到主机记录为@(即空)的记录下. 而在做域名解析的时候,为了方便,需要将不带3w的域名也要解析到主机记录为@(即空)的记录下. 因此,解析报错 ...
- 关于js中使用close方法无法关闭firefox浏览器
今天遇到一个问题就是在js中使用window.close()方法无法关闭Firefox: 浏览器版本: firefox
- 编写高质量Python代码的59个有效方法
Python学习资料或者需要代码.视频加Python学习群:960410445 1. 用Pythonic方式思考 第一条:确认自己使用的Python版本 (1)有两个版本的python处于活跃状态,p ...