NCPC 2012 Bread Sorting
逆序对数的应用;
逆序对数的写法有,二分,树状数组,分治;
学习一下;
树状数组版:
代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],b[maxn],c[maxn];
int n;
struct point
{
int num,index;
bool operator<(const point& t)const
{
return num<t.num;
}
}p[maxn]; 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-=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+);
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]));
}
memset(c,,sizeof c);
memset(b,,sizeof b);
for(i=;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].index=i;
}
sort(p+,p+n+);
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]));
}
if(ans1%==ans2%) printf("Possible\n");
else printf("Impossible\n");
}
return ;
}
分治版:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=;
int b[maxn],c[maxn];
int n,cnt;
struct point
{
int num,index;
bool operator<(const point& t)const
{
return num<t.num;
}
}p[maxn]; void merge_sort(int *a,int x,int y,int *t)
{
if(y-x>)
{
int m=(y+x)/;
int p=x,q=m,i=x;
merge_sort(a,x,m,t);
merge_sort(a,m,y,t);
while(p<m||q<y)
{
if(q>=y||(p<m&&a[p]<=a[q])) t[i++]=a[p++];
else
{
t[i++]=a[q++];
cnt+=m-p;
}
}
for(int i=x; i<y; i++)a[i]=t[i];
}
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].index=i;
}
sort(p+,p+n+);
for(i=;i<=n;i++)
b[p[i].index]=i;
cnt=;
merge_sort(b,,n+,c);
int ans1=cnt; cnt=;
for(i=;i<=n;i++)
{
scanf("%d",&p[i].num);
p[i].index=i;
}
sort(p+,p+n+);
for(i=;i<=n;i++)
b[p[i].index]=i;
merge_sort(b,,n+,c);
int ans2=cnt;
if(ans1%==ans2%) printf("Possible\n");
else printf("Impossible\n");
}
return ;
}
线段树版的话就不写了,麻烦!
NCPC 2012 Bread Sorting的更多相关文章
- BNUOJ-26579 Bread Sorting YY
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26579 考虑两个性质:蚂蚁的相对位置不变,蚂蚁碰撞时相当于对穿而过,然后排两次序就可以了. ...
- BNUOJ-29364 Bread Sorting 水题
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29364 题意:给一个序列,输出序列中,二进制1的个数最少的数.. 随便搞搞就行了,关于更多 ...
- BNUOJ-29357 Bread Sorting 模拟
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=29357 直接模拟就可以了.. //STATUS:C++_AC_190MS_1884KB # ...
- BNUOJ-26474 Bread Sorting 逆序对
题目链接:http://www.bnuoj.com/bnuoj/problem_show.php?pid=26474 题意:给一个数列,可以对三个数操作:把最后一个数放到第一个,前两个数后移一位.问最 ...
- NCPC 2012 Cookie Selection
题目要求每次输出中间的那个数,如果数据很大肯定扛不住: 所以用两个优先队列来维护: 这样的话中间的那个数反正会在两个队列的任何一个的头部: 时间复杂度肯定比较小: 代码: #include <c ...
- NCPC 2012 Galactic Warlords
湖南大学的oj上有这套比赛: 这题是个简单的计算几何,首先去掉重复的边,然后判断是否全部平行: 代码: #include<cstdio> #define maxn 105 using na ...
- BNUOJ 26474 Bread Sorting
/*给出n个原始顺序的数,再给出要排成目标状态顺序,每次从第一个数列中选择三个,把这三个数中最右边的数放在最左边,然后其他两个数右 移一个单位,为你从原始状态能不能排序成目标状态. 本人YY的结论,从 ...
- 开始VS 2012中LightSwitch系列的第4部分:太多信息了!使用查询来排序和筛选数据
[原文发表地址] Beginning LightSwitch in VS 2012 Part 4: Too much information! Sorting and Filtering Data ...
- WebGrid with filtering, paging and sorting 【转】
WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...
随机推荐
- 使用ExpandableListView实现一个时光轴
在许多App上都能看到时光轴的效果,比如携程等等,那么我们今天就利用ExpandableListView来实现一个时光轴效果,先来看看效果图: 效果还是挺简单的,这里我们主要是采用Expandable ...
- android和javascript之间相互通信实例分析
1. AndroidManifest.xml中必须使用许可 "android.permission.INTERNET", 否则会出Web page not available错误 ...
- 解决无法获取 GridView 隐藏列值问题
今天遇到了一个要获取GridView隐藏列值的问题,试了好几种方法,要么获取不到,要么获取到了类列的值也隐藏了,但在样式中这一列会多出一块,,但最后找到了一个功能实现而且实现了列完美隐藏的方法和大家分 ...
- sql: 生日三个月内有效
DECLARE @birthday datetime,@stat datetime,@end datetime,@statbirthday datetime,@endbirthday datetime ...
- ASP和PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP
/** * 检测访问的ip是否为规定的允许的ip * Enter description here ... */ function check_ip(){ $ALLOWED_IP=array('192 ...
- Fibonacci数列的java实现
关于Fibonacci应该都比较熟悉,0,1,1,2,3..... 基本公式为f(n) = f(n-1) + f(n-2); f(0) = 0; f(1) =1; 方法1:可以运用迭代的方法实现: p ...
- android studio安装后卡在fetching Android sdk compoment information
解决办法: 找到android studio 安装目录下bin目录中(D:\Android\Android Studio\bin)的idea.properties文件,在文件末尾处添加disable. ...
- Moving Tables
Moving Tables Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
- C++ 智能指针auto_ptr详解
1. auto_ptr 的设计动机: 函数操作经常依照下列模式进行: 获取一些资源 执行一些动作 释放所获取的资源 那么面对这些资源的释放问题就会出现下面的两种情况: 一开始获得的资源被绑定于局部对象 ...
- 九度OJ 1042 Coincidence -- 动态规划(最长公共子序列)
题目地址:http://ac.jobdu.com/problem.php?pid=1042 题目描述: Find a longest common subsequence of two strings ...