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 ...
随机推荐
- 转:通过API获取和创建 Oracle GL Account Code Combination ID’s (CCID’s)的几种方法汇总
1] FND_FLEX_EXT.GET_COMBINATION_ID: This API Finds combination_id for given set of key flexfield seg ...
- JVM笔记4:Java内存分配策略
简单来说,对象内存分配主要是在堆中分配.但是分配的规则并不是固定的,取决于使用的收集器组合以及JVM内存相关参数的设定 以下介绍几条基本规则(使用的ParNew+Serial Old收集器组合): 一 ...
- H5 canvas 小demo之小球的随机运动
1:结构之html----balls.html <!DOCTYPE html> <html> <head lang="en"> <meta ...
- 20160327javaweb 之JSP入门
一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...
- MVC中使用jquery的浏览器缓存问题
jquery在浏览器ajax调用的时候,对缓存提供了很好的支持,POST方式不能被缓存,使用POST的原因,明确了数据不能被缓存,或者避免JSON攻击(JSON返回数据的时候可以被入侵) jquery ...
- Mybatis-generator使用和扩展
Mybatis-generator使用和扩展 mybatis-generator使用 pom.xml配置 <plugin> <groupId>org.mybatis.gener ...
- HDU 4707 Pet(BFS)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4707 题目大意:在一个无环的,从0开始发散状的地图里,找出各个距离0大于d的点的个数 Sample I ...
- Poj 3061 Subsequence(二分+前缀和)
Subsequence Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12333 Accepted: 5178 Descript ...
- P1417 烹调方案
P1417 烹调方案 题目提供者tinylic 标签 动态规划 难度 普及+/提高 题目背景 由于你的帮助,火星只遭受了最小的损失.但gw懒得重建家园了,就造了一艘飞船飞向遥远的earth星.不过飞船 ...
- bzoj1007:[HNOI2008]水平可见直线
思路:首先按斜率排序,如果斜率相同就取截距最大的,显然截距小的会被覆盖而对答案没有贡献,然后考虑斜率不同的如何统计答案,可以用一个单调栈维护,当前新插入的直线显然斜率是要比当前栈顶斜率要大的,然后如果 ...