Problem UVA1620-Lazy Susan

Accept: 81  Submit: 375
Time Limit: 3000 mSec

Problem Description

There are N marbles, which are labeled 1,2,...,N. The N 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). 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 (8 ≤ N ≤ 500). The second line describes the initial state of the track. It contains N 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

题解:这种题真的是驾驭不了,简单说一下大致的证明吧:每次翻转四个数,会发现逆序对数的改变一定是偶数,因此如果该序列不管从谁开始都是奇数个逆序对,那就很明显不可能成立,接下来有个结论就是对于环形序列,只有n为奇数且从某一位开始逆序对数是奇数时,从任一位开始的逆序对数才会都是奇数。我们只用看一下偶数为什么一定有偶数个逆序对的序列就好,不妨设从第一位开始有共有奇数个逆序对,第一个数和后面的数构成的二元组共有奇数个,此时选取新的序列从第二位开始,那么原来的第一个数就变成了最后一个,原来的逆序对与非逆序对互换,就有偶数个逆序对了。至于为什么只要逆序对为偶数就一定有解,我也不清楚,请各位大佬指教orz.

 #include <bits/stdc++.h>

 using namespace std;

 const int maxn =  + ;

 int n, cnt;
int num[maxn], tmp[maxn]; void merge_sort(int l, int r) {
if (l + >= r) return;
int m = (l + r) >> ;
merge_sort(l, m);
merge_sort(m, r);
int p = l, q = m, i = l;
while (p < m || q < r) {
if (q >= r || (p < m && num[p] < num[q])) tmp[i++] = num[p++];
else {
tmp[i++] = num[q++];
cnt += m - p;
}
}
for (int i = l; i < r; i++) {
num[i] = tmp[i];
}
} int main()
{
//freopen("input.txt", "r", stdin);
int iCase;
scanf("%d", &iCase);
while (iCase--) {
scanf("%d", &n);
for (int i = ; i < n; i++) {
scanf("%d", &num[i]);
} cnt = ;
merge_sort(, n);
if ((n & ) && (cnt & )) printf("impossible\n");
else printf("possible\n");
}
return ;
}

UVA1620-Lazy Susan(思维+逆序对)的更多相关文章

  1. CodeForces - 987E Petr and Permutations (思维+逆序对)

    题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...

  2. Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)

    题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同 ...

  3. Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal (思维,逆序对)

    题意:给你一个字符串,每次可以调换现字符串的相邻两个字符,问最少操作多少次使得这个字符串等于其反转过来的字符串. 题解:先考虑字符串中没有相同字符的情况,那么我们每次将目前字符串的最后一个字符一直调换 ...

  4. UVA1620 Lazy Susan(结论证明)

    结论: 当 \(n\geq 6\) 时,若 \(n\) 是奇数且输入序列的逆序对数是奇数,则无解,否则有解. 当 \(n=4\) 或 \(n=5\) 时,答案个数及其有限,只有这个环是 \(1\) 到 ...

  5. UVA - 1620 Lazy Susan(逆序数)

    题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...

  6. uva1620 Lazy Susan

    留坑(p.256) 什么找规律啊 坑爹 #include<cstdio> #include<cstring> #include<cstdlib> #include& ...

  7. 【Codeforces】CF 911 D. Inversion Counting(逆序对+思维)

    题目 传送门:QWQ 分析 思维要求比较高. 首先我们要把原图的逆序对q算出来. 这个树状数组或归并排序都ok(树状数组不用离散化好评) 那么翻转$[l,r]$中的数怎么做呢? 暴力过不了,我试过了. ...

  8. Japan POJ - 3067 转化思维 转化为求逆序对

    Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...

  9. NOIp2013 火柴排队【逆序对/思维】 By cellur925

    题目大意:给你两列数\(ai\)和\(bi\),你可以交换每列数中相邻的两个数,求一个最小交换次数使\(\sum_{i=1}^{n}(a_i-b_i)^2\) 最小. 最后满足条件的两个序列一定是各个 ...

随机推荐

  1. java连接MySQL数据库的方式

    Java连接数据库的几种方法 *说明 1.以MySQL数据库为例 2.分为四个步骤: 建立数据库连接, 向数据库中提交sql 处理数据库返回的结果 关闭数据库连接 一:JDBC 1.建立数据库连接 只 ...

  2. JSTL_Format标记库

    JSTL_Format 一:JSTL Format标记库 如有转载,请标明出处 介绍标记属性的时候,按照顺序必须写的->带有默认值的->其他的,中间用回车隔开 在jsp问价开头加上 < ...

  3. python基础学习(三)变量和类型

    变量的作用:变量就是用来存储数据的. 变量的定义 在python中,变量在使用之前需要进行赋值,变量只有赋值后才能使用,如果变量没有赋值就使用会出现什么情况呢?如下图,使用之前变量未定义,会报错,如下 ...

  4. js发送邮件确定email地址

    <a href="mailto:wjl@tom.com?subject=aaa&body=11111">test</a>

  5. eclipse编写js代码没有提示

    安装插件 点击Help,选择Eclipse Marketplace... 搜索js,安装AngularJS Eclipse 重启eclipse,右键项目,选择Configure(配置),选择Conve ...

  6. Java自动内存管理机制学习(一):Java内存区域与内存溢出异常

    备注:本文引用自<深入理解Java虚拟机第二版> 2.1 运行时数据区域 Java虚拟机在执行Java程序的过程中把它所管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创 ...

  7. Laravel 系列入门教程(一)【最适合中国人的 Laravel 教程】

    热烈庆祝 Laravel 5.5 LTS 发布! 实际上 Laravel 上一个 LTS 选择 5.1 是非常不明智的,因为 5.2 增加了许许多多优秀的特性.现在好了,大家都用最新的长期支持版本 5 ...

  8. git pull遇到错误:error: Your local changes to the following files would be overwritten by merge:

    方法1:如果你想保留刚才本地修改的代码,并把git服务器上的代码pull到本地(本地刚才修改的代码将会被暂时封存起来) git stash git pull origin master git sta ...

  9. ionic 确认提示操作框

    //确认框 .factory('ActionSheet', function ($ionicActionSheet, TipsPort, Service,Loading) { var ActionSh ...

  10. 使用 float 存储小数?

    很多程序员就会使用 float 类型来存储小数.sql 的 float 类型和其他大多数编程语言的 float 类型一样, 根据IEEE 754 标准使用二进制格式编码实数数据. 但是很多程序员并不清 ...