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\) 最小. 最后满足条件的两个序列一定是各个 ...
随机推荐
- crontab架构和格式
crontab架构图 分时日月周*****my command(可以是一个linux命令,也可以是一个脚本文件,可以是shell格式也可以是python格式,也可是java格式.....) 按照格式编 ...
- [jQuery]相对父级元素的fixed定位
(function($) { var DNG = {}; //----------------------------------------------------/ // ...
- 洛谷P4589 [TJOI2018]智力竞赛(二分答案 二分图匹配)
题意 题目链接 给出一个带权有向图,选出n + 1n+1条链,问能否全部点覆盖,如果不能,问不能覆盖的点权最小值最大是多少 Sol TJOI怎么净出板子题 二分答案之后直接二分图匹配check一下. ...
- 【代码笔记】Web-JavaScript-javascript while循环
一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Dynamics 365工作流报错:您无法登陆系统。原因可能是您的用户记录或您所属的业务部门在Microsoft Dynamics 365中已被禁用。
本人微信和易信公众号: 微软动态CRM专家罗勇 ,回复265或者20170926可方便获取本文,同时可以在第一间得到我发布的最新的博文信息,follow me!我的网站是 www.luoyong.me ...
- svn基本常见操作设置
代码管理工具一开始用的确会有点懵,但是永久了就会发现都是那几下套路,记录下来 托管好了代码一般起冲突了还是想重新搞一下,有个万能的重置操作,那就是重新关联svn项目,以前有时更换地址也是,发现遇到很多 ...
- (后端)spring的@Transactional注解详细用法(转)
转自一人浅醉-博客园: 事务隔离级别 隔离级别是指若干个并发的事务之间的隔离程度.TransactionDefinition 接口中定义了五个表示隔离级别的常量: TransactionDefinit ...
- (网页)websocket后台调用Service层
百度论坛里面有很多好的方法,借鉴. 重点:因为项目是StringBoot所以我用的是下面的方法很好使: Service.... service = (Service....) ContextLoade ...
- 作为IT,你的价值在哪里?
也许最近是真的被无穷无尽的数据整理.导入.再整理.再导入给恶心到了. 业务部提交的数据只是一个非常初始的数据,IT还得在这个基础上七整八整,对导出的结果还要再做二次导入三次导入,不仅要帮业务部批导生成 ...
- Spark算子代码实践
package com.dingxin.datainit import org.apache.log4j.{Level, Logger} import org.apache.spark.sql.Spa ...