BNUOJ-26474 Bread Sorting 逆序对
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26474
题意:给一个数列,可以对三个数操作:把最后一个数放到第一个,前两个数后移一位。问最后能否到达相应的目标序列。。
先考虑三个数A B C,变换后两种情况B C A和C A B,可以证得(列举3个数的大小情况,枚举证),这三个序列变换后的逆序对个数的奇偶性是相同的,而且只有这3个序列相同,所以A B C只能到达与之奇偶相同的序列,而且是全部能到达。那么多个数的情况也是一样的,就是多个3元组的扩展。因此如果变换后的逆序奇偶性相同,那么有解,否则无解。。
//STATUS:C++_AC_868MS_2220KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
//#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef long long LL;
typedef unsigned long long ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=1e9+,STA=;
//const LL LNF=1LL<<60;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End int num[N],temp[N];
int n;
int ans; void sort(int l,int r)
{
if(l==r)return;
int i,j,k,mid=(l+r)>>;
sort(l,mid);
sort(mid+,r);
for(i=k=l,j=mid+;i<=mid && j<=r;){
if(num[i]<num[j]){
ans=(ans+j-mid-)&;
temp[k++]=num[i++];
}
else temp[k++]=num[j++];
}
while(i<=mid){
ans=(ans+j-mid-)&;
temp[k++]=num[i++];
}
while(j<=r)
temp[k++]=num[j++];
for(i=l;i<=r;i++)
num[i]=temp[i];
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,ok;
while(~scanf("%d",&n))
{
ok=ans=;
for(i=;i<n;i++)
scanf("%d",&num[i]);
sort(,n-);
ok=ans;
ans=;
for(i=;i<n;i++)
scanf("%d",&num[i]);
sort(,n-);
ok^=ans; printf("%s\n",ok?"Impossible":"Possible");
}
return ;
}
BNUOJ-26474 Bread Sorting 逆序对的更多相关文章
- BNUOJ 26474 Bread Sorting
/*给出n个原始顺序的数,再给出要排成目标状态顺序,每次从第一个数列中选择三个,把这三个数中最右边的数放在最左边,然后其他两个数右 移一个单位,为你从原始状态能不能排序成目标状态. 本人YY的结论,从 ...
- hdu_2838_Cow Sorting(树状数组求逆序对)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 题意:给你一串数,让你排序,只能交换相邻的数,每次交换花费交换的两个树的和,问最小交换的价值 题 ...
- hdu 2838 Cow Sorting (树状数组+逆序对)
题目 题意:给你N个排列不规则的数,任务是把它从小到大排好,每次只能交换相邻两个数,交换一次的代价为两数之和,求最小代价 拿到这道题,我根本看不出这道题和树状数组有半毛钱关系,博客之,全说用树状数组做 ...
- hdu2838 cow sorting用树状数组求逆序对
题目链接:http://icpc.njust.edu.cn/Problem/Hdu/2838/ 题目解法:题目给出一个1-n的排列,操作只有一种:交换相邻的元素,代价是两个元素之和,问将该序列变成升序 ...
- 【CF1591】【数组数组】【逆序对】#759(div2)D. Yet Another Sorting Problem
题目:Problem - D - Codeforces 题解 此题是给数组排序的题,操作是选取任意三个数,然后交换他们,确保他们的位置会发生改变. 可以交换无限次,最终可以形成一个不下降序列就输出&q ...
- POJ2299Ultra-QuickSort(归并排序 + 树状数组求逆序对)
树状数组求逆序对 转载http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html 转载: 树状数组,具体的说是 离散化+树 ...
- BNU 2418 Ultra-QuickSort (线段树求逆序对)
题目链接:http://acm.bnu.edu.cn/bnuoj/problem_show.php?pid=2418 解题报告:就是给你n个数,然后让你求这个数列的逆序对是多少?题目中n的范围是n & ...
- POJ 2299 逆序对
Crossings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463 Description I ...
- 【POJ】2299 Ultra-QuickSort(逆序对)
http://poj.org/problem?id=2299 在两个元素相同的数列里,其中一个数列要移动到另一个数列相同元素相同的位置,那么要移动的次数就是这个数列关于另一个数列的逆序对数(hash后 ...
随机推荐
- spoj 227
留着 #include <cstdio> #include <cstring> #include <cstdlib> #define lson l, m, rt & ...
- kafka.utils.Utils阅读
这个类实现了一些工具性质的方法,正如其名. 记下自己觉得有意思的方法: readFileAsString(path: String, charset: Charset = Charset.defaul ...
- C++11的for循环,以及范围Range类的实现
C++11支持range-based for循环.这是一个很方便的特性,能省挺多代码.以下代码就能很方便的遍历vector中的元素,并打印出来: 1 2 3 4 5 6 7 8 std::vector ...
- hdu 1849 Rabbit and Grass 博弈论
水题,转化Nim 代码如下: #include<iostream> #include<stdio.h> #include<algorithm> #include&l ...
- C#操作xml文档,cuid,dategridview显示数据
界面 //所有的数据全部在集合中 //dgv控件绑定List集合,List集合中有User类的对象,对象里有属性,把属性绑定到没列的数据上 dgv.Da ...
- ubuntu装机
备份: .bashrc profile .vimrc exports defults/ 各种workspace中的源码 goagent/ 重转后安装: apt-get install openjdk- ...
- How to download a website for offline usage
wget -U Mozilla --recursive --no-clobber --page-requisites --html-extension --convert-links -- restr ...
- 【HDOJ】4355 Party All the Time
好久没做过三分的题目了. /* 4355 */ #include <iostream> #include <sstream> #include <string> # ...
- C#和.net之间的关系
What is the difference between C# and .NET? In addition to what Andrew said, it is worth noting that ...
- 函数fsp_seg_inode_page_find_free
/**********************************************************************//** Looks for an unused segm ...