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
Sample Input
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(思维+逆序对)的更多相关文章
- CodeForces - 987E Petr and Permutations (思维+逆序对)
题意:初始有一个序列[1,2,...N],一次操作可以将任意两个位置的值互换,Petr做3*n次操作:Alxe做7*n+1次操作.给出最后生成的新序列,问是由谁操作得到的. 分析:一个序列的状态可以归 ...
- Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal 题解(思维+逆序对)
题目链接 题目大意 给你一个长度为n的字符串,可以交换相邻两个元素,使得这个字符串翻转,求最少多少种次数改变 题目思路 如果要求数组排序所需要的冒泡次数,那其实就是逆序对 这个也差不多,但是如果是相同 ...
- Educational Codeforces Round 96 (Rated for Div. 2) E. String Reversal (思维,逆序对)
题意:给你一个字符串,每次可以调换现字符串的相邻两个字符,问最少操作多少次使得这个字符串等于其反转过来的字符串. 题解:先考虑字符串中没有相同字符的情况,那么我们每次将目前字符串的最后一个字符一直调换 ...
- UVA1620 Lazy Susan(结论证明)
结论: 当 \(n\geq 6\) 时,若 \(n\) 是奇数且输入序列的逆序对数是奇数,则无解,否则有解. 当 \(n=4\) 或 \(n=5\) 时,答案个数及其有限,只有这个环是 \(1\) 到 ...
- UVA - 1620 Lazy Susan(逆序数)
题目: 把1~n(n≤500)放到一个圆盘里,每个数恰好出现一次.每次可以选4个连续的数字翻转顺序.问能不能变成1.2.3....n的顺序. 思路: 这样的题的规律真的是一点都不好推,看了网上的博客知 ...
- uva1620 Lazy Susan
留坑(p.256) 什么找规律啊 坑爹 #include<cstdio> #include<cstring> #include<cstdlib> #include& ...
- 【Codeforces】CF 911 D. Inversion Counting(逆序对+思维)
题目 传送门:QWQ 分析 思维要求比较高. 首先我们要把原图的逆序对q算出来. 这个树状数组或归并排序都ok(树状数组不用离散化好评) 那么翻转$[l,r]$中的数怎么做呢? 暴力过不了,我试过了. ...
- Japan POJ - 3067 转化思维 转化为求逆序对
Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Jap ...
- NOIp2013 火柴排队【逆序对/思维】 By cellur925
题目大意:给你两列数\(ai\)和\(bi\),你可以交换每列数中相邻的两个数,求一个最小交换次数使\(\sum_{i=1}^{n}(a_i-b_i)^2\) 最小. 最后满足条件的两个序列一定是各个 ...
随机推荐
- Java学习笔记之——常用类
Random,String,Math 1.Random Random() 创建一个新的随机数生成器 案例:产生3个5-10的随机数 运行结果 注意: 2.StingBuffer,StringBu ...
- Hibernate入门(七)一对多入门案例
一对多 场景模拟:用户(一)对订单(多) 1.建表 创建客户表,字段有:客户id,客户姓名,客户性别,客户年龄,客户年纪,客户电话. 创建订单表,字段有:订单编号,明细编号,客户编号(外键) DROP ...
- 函数多个返回值与unpack的用法
-- return the index of max number and himself -- 函数可以返回多个值 function get_max( T ) ] for i, v in ipair ...
- 我是这样搞懂一个神奇的BUG
摘要: 通过分析用户的行为,才想得到为什么会出现这种情况! 前两天在BearyChat收到这样的一个报警消息: 409 ?Conflict ? 平时很少遇到这样的错误,貌似很严重的样子,吓得我赶紧查看 ...
- NPOI 读取Excel文件
private void buttonExcel_Click(object sender, EventArgs e) { FileStream fs = null; List<ISheet> ...
- 设置dataGridView单元格颜色、字体、ToolTip、字体颜色
this.dataGridView3.Rows[e.RowIndex].Cells["你的那个要判断的列名"].Style.BackColor = Color.MediumPurp ...
- git常用命令总结以及用github来展示你的前端页面
命令小结 命令 功能 git init 把当前文件夹初始化为默认的git库 git add 文件名 向git库中添加一个文件 git rm 文件名 从git库中删除一个文件 git status 查 ...
- 【20190221】HTTP-URI与URL
URI(uniform resource identifier)统一资源标识符是由某个协议方案表示的资源的定位标识符, URI 用字符串标识某一互联网资源,而 URL 表示资源的地点(互联网上所处的位 ...
- Nodejs全局/缓存路径配置
$ npm config set prefix "D:\Program Files\nodejs\node_global" $ npm config set cache " ...
- C# 8.0的三个值得关注的新特性
本文翻译自:https://dzone.com/articles/3-new-c-8-features-we-are-excited-about 转载请注明出自:葡萄城官网,葡萄城为开发者提供专业的开 ...