/*给出n个原始顺序的数,再给出要排成目标状态顺序,每次从第一个数列中选择三个,把这三个数中最右边的数放在最左边,然后其他两个数右
移一个单位,为你从原始状态能不能排序成目标状态。
本人YY的结论,从特殊到一般,虽然一般的只是手证了数值比较小的:结论应该就是1到n的n个数的全排列,分成相等的奇排序和偶排序,且个数一样,同个集合中的排列可以互相转换
比如1 2 3 4的全排1 2 3 4
1 4 2 3
1 3 4 2
4 1 3 2
4 2 1 3
4 3 2 1
2 4 3 1
2 1 4 3
2 3 1 4
3 4 1 2(可由4 1 3 2变成,一样的)
3 2 4 1
3 1 2 4
这个偶排序占了12个,其他十二个是奇排序
所以这道题直接比较两个数列的奇偶性
*/
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],b[maxn],c[maxn];
int n;
struct point
{
int num,index;
}p[maxn];
int cmp(const point a,const point b)
{
return a.num<b.num;
}
int lowbit(int x)
{
return x&(-x);
}
void updata(int i,int x)
{
while(i<=n)
{
c[i]+=x;
i=i+lowbit(i);
}
}
int sum(int x)
{
int ans=;
while(x>)
{
ans+=c[x];
x=x-lowbit(x);
}
return ans;
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
memset(c,,sizeof(c));
for(i=;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].index=i;
}
sort(p+,p+n+,cmp);
for(i=;i<=n;i++)
b[p[i].index]=i; int ans1=,ans2=;
for(i=;i<=n;i++)
{
updata(b[i],);
ans1=ans1+(sum(n)-sum(b[i]));//sum(p[i])是前面比b[i]小的数的个数
}
memset(c,,sizeof(c));
memset(p,,sizeof(p));
memset(b,,sizeof(b));
for(i=;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].index=i;
}
sort(p+,p+n+,cmp);
for(i=;i<=n;i++)
b[p[i].index]=i; for(i=;i<=n;i++)
{
updata(b[i],);
ans2=ans2+(sum(n)-sum(b[i]));
}
printf("%d %d\n",ans1,ans2);
if(ans1%==ans2%) printf("Possible\n");
else printf("Impossible\n");
}
return ;
}
PS:这道题置换群也可以破
有奇数个有偶数个元素的轮换就是不可以
有偶数个有偶数个元素的轮换就是可以

BNUOJ 26474 Bread Sorting的更多相关文章

  1. BNUOJ-26474 Bread Sorting 逆序对

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26474 题意:给一个数列,可以对三个数操作:把最后一个数放到第一个,前两个数后移一位.问最 ...

  2. BNUOJ-26579 Bread Sorting YY

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26579 考虑两个性质:蚂蚁的相对位置不变,蚂蚁碰撞时相当于对穿而过,然后排两次序就可以了. ...

  3. BNUOJ-29364 Bread Sorting 水题

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29364 题意:给一个序列,输出序列中,二进制1的个数最少的数.. 随便搞搞就行了,关于更多 ...

  4. BNUOJ-29357 Bread Sorting 模拟

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29357 直接模拟就可以了.. //STATUS:C++_AC_190MS_1884KB # ...

  5. NCPC 2012 Bread Sorting

    逆序对数的应用: 逆序对数的写法有,二分,树状数组,分治: 学习一下: 树状数组版: 代码: #include<cstdio> #include<cstring> #inclu ...

  6. BNUOJ 12756 Social Holidaying(二分匹配)

    题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=12756 Social Holidaying Time Limit: 3000ms Memo ...

  7. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  8. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  9. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

随机推荐

  1. vs删除空行 —— 正则表达式以及其他

    \r:return 到当前行的最左边. \n: newline 向下移动一行,并不移动左右. 1. ^\s*\n               查找空行 2. ^\s*(?=\r?$)\n  查找空行即 ...

  2. java基础---->java调用oracle存储过程

    存储过程是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,存储在数据库中,经过第一次编译后再次调用不需要再次编译,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它.今天 ...

  3. LeetCode——Sqrt(x)

    Description: Implement int sqrt(int x). Compute and return the square root of x. 好好学习数学还是非常有用的,牛顿迭代法 ...

  4. linux如何设置用户权限

    linux与用户权限设置: 1.添加用户 首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy //修改密码 ...

  5. node中的对象

    1. class的概念 定义一个class,属性都是private,方法都是public. Hello.js: 使用class index.js: 2. 单例类 使用exports而不是module. ...

  6. java如何随机生成定长的字符串

    小数,字符串.时间等示例代码 String base = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 public c ...

  7. angular -- ng-ui-route路由及其传递参数?script标签版

    考虑到 多视图等因素,所以 angular 的路由考虑使用 ng-ui-route来做,而不使用 ng-route来做! <!DOCTYPE html> <html lang=&qu ...

  8. HOJ Recoup Traveling Expenses(最长递减子序列变形)

    A person wants to travel around some places. The welfare in his company can cover some of the airfar ...

  9. CH1602 The XOR Largest Pair【Trie树】

    1602 The XOR Largest Pair 0x10「基本数据结构」例题 描述 在给定的N个整数A1,A2……AN中选出两个进行xor运算,得到的结果最大是多少? 输入格式 第一行一个整数N, ...

  10. 沈阳网络赛K-Supreme Number【规律】

    26.89% 1000ms 131072K A prime number (or a prime) is a natural number greater than 11 that cannot be ...