codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)
题目链接:
C. Mashmokh and Reverse Operation
4 seconds
512 megabytes
standard input
standard output
Mashmokh's boss, Bimokh, didn't like Mashmokh. So he fired him. Mashmokh decided to go to university and participate in ACM instead of finding a new job. He wants to become a member of Bamokh's team. In order to join he was given some programming tasks and one week to solve them. Mashmokh is not a very experienced programmer. Actually he is not a programmer at all. So he wasn't able to solve them. That's why he asked you to help him with these tasks. One of these tasks is the following.
You have an array a of length 2n and m queries on it. The i-th query is described by an integer qi. In order to perform the i-th query you must:
- split the array into 2n - qi parts, where each part is a subarray consisting of 2qi numbers; the j-th subarray (1 ≤ j ≤ 2n - qi) should contain the elements a[(j - 1)·2qi + 1], a[(j - 1)·2qi + 2], ..., a[(j - 1)·2qi + 2qi];
- reverse each of the subarrays;
- join them into a single array in the same order (this array becomes new array a);
- output the number of inversions in the new a.
Given initial array a and all the queries. Answer all the queries. Please, note that the changes from some query is saved for further queries.
The first line of input contains a single integer n (0 ≤ n ≤ 20).
The second line of input contains 2n space-separated integers a[1], a[2], ..., a[2n] (1 ≤ a[i] ≤ 109), the initial array.
The third line of input contains a single integer m (1 ≤ m ≤ 106).
The fourth line of input contains m space-separated integers q1, q2, ..., qm (0 ≤ qi ≤ n), the queries.
Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.
Output m lines. In the i-th line print the answer (the number of inversions) for the i-th query.
2
2 1 4 3
4
1 2 0 2
0
6
6
0
1
1 2
3
0 1 1
0
1
0
If we reverse an array x[1], x[2], ..., x[n] it becomes new array y[1], y[2], ..., y[n], where y[i] = x[n - i + 1] for each i.
The number of inversions of an array x[1], x[2], ..., x[n] is the number of pairs of indices i, j such that: i < j and x[i] > x[j].
题意:
问2^n个数进行如题的操作每次操作后逆序对是多少;
思路:
像归并排序那样先划分,然后再求出每个划分里面的逆序对,合并后再求两个区间之间的逆序对,倒序把逆序对和正序对的数目交换了;
最近抄代码抄的厉害,哎;
AC代码:
#include <bits/stdc++.h>
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=1e6+5e5;
LL sum[][];
int n,a[<<];
void dfs(int l,int r,int deep)
{
if(l>=r)return ;
int mid=(l+r)>>;
dfs(l,mid,deep-);
dfs(mid+,r,deep-);
for(int i=l;i<=mid;i++)
{ int temp=lower_bound(a+mid+,a+r+,a[i])-(a+mid+);
sum[deep][]+=(LL)temp;
temp=r-mid-(upper_bound(a+mid+,a+r+,a[i])-(a+mid+));
sum[deep][]+=(LL)temp;
}
sort(a+l,a+r+);
}
int main()
{
scanf("%d",&n);
int y=(<<n);
Riep(y)scanf("%d",a+i);
dfs(,y,n);
int q,x;
scanf("%d",&q);
while(q--)
{
scanf("%d",&x);
while(x)
{
swap(sum[x][],sum[x][]);
x--;
}
LL ans=;
Riep(n)ans+=sum[i][];
printf("%I64d\n",ans);
}
return ;
}
codeforces 414C C. Mashmokh and Reverse Operation(归并排序求逆序对)的更多相关文章
- 2014 HDU多校弟五场A题 【归并排序求逆序对】
这题是2Y,第一次WA贡献给了没有long long 的答案QAQ 题意不难理解,解题方法不难. 先用归并排序求出原串中逆序对的个数然后拿来减去k即可,如果答案小于0,则取0 学习了归并排序求逆序对的 ...
- 归并排序&&归并排序求逆序对
归并排序 归并排序(MERGE-SORT)是建立在归并操作上的一种有效的排序算法,该算法是采用分治法(Divide and Conquer)的一个非常典型的应用.将已有序的子序列合并,得到完全有序的序 ...
- 【BZOJ4769】超级贞鱼 归并排序求逆序对
[BZOJ4769]超级贞鱼 Description 马达加斯加贞鱼是一种神奇的双脚贞鱼,它们把自己的智慧写在脚上——每只贞鱼的左脚和右脚上各有一个数.有一天,K只贞鱼兴致来潮,排成一列,从左到右第i ...
- poj2299(归并排序求逆序对)
题目链接:https://vjudge.net/problem/POJ-2299 题意:给定一个序列,每次只能交换邻近的两个元素,问要交换多少次才能使序列按升序排列. 思路:本质就是求逆序对.我们用归 ...
- 归并排序+归并排序求逆序对(例题P1908)
归并排序(merge sort) 顾名思义,这是一种排序算法,时间复杂度为O(nlogn),时间复杂度上和快排一样 归并排序是分治思想的应用,我们先将n个数不断地二分,最后得到n个长度为1的区间,显然 ...
- 归并排序求逆序对(poj 2299)
归并排序求逆序对 题目大意 给你多个序列,让你求出每个序列中逆序对的数量. 输入:每组数据以一个数 n 开头,以下n行,每行一个数字,代表这个序列: 输出:对于输出对应该组数据的逆序对的数量: 顺便在 ...
- HDU 3743 Frosh Week(归并排序求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3743 题目意思就是给你一个长为n的序列,让你求逆序对.我用的是归并排序来求的.归并排序有一个合并的过程 ...
- 浙江工商大学15年校赛I题 Inversion 【归并排序求逆序对】
Inversion Time Limit 1s Memory Limit 131072KB Judge Program Standard Ratio(Solve/Submit) 15.00%(3/20 ...
- 归并排序(归并排序求逆序对数)--16--归并排序--Leetcode面试题51.数组中的逆序对
面试题51. 数组中的逆序对 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组中的逆序对的总数. 示例 1: 输入: [7,5,6,4] 输出 ...
随机推荐
- 最短路中部分点只能从中任意选取K个问题
题意:给N个点,还有另外m个点(其中只能选K个),求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其他点了),若该点是m个点中的,则count[i]=原来的+1:若不是,则 ...
- solus系统配置
#更新软件源 清华稳定源 sudo eopkg ar Tuna https://mirrors.tuna.tsinghua.edu.cn/solus/shannon/eopkg-index.xml 清 ...
- T1063 合并果子 codevs
http://codevs.cn/problem/1063/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个 ...
- KS103超声波测距模块
max232:电平转换芯片,将电脑的RS-232标准串口(高+12V,低-12V)转换为(高+5V,低0V). 电脑串口(RS -232) => 单片机串口(TTL串口) SIPEX SP323 ...
- springboot整合mybatis+jetty笔记以及遇到的问题
图文创建一个springboot Demo(IDEA创建)+目录图+返回json springboot创建参考 pom.xml <dependencies> <!--<depe ...
- BUPT复试专题—哈夫曼树(2010)
https://www.nowcoder.com/practice/162753046d5f47c7aac01a5b2fcda155?tpId=67&tqId=29635&tPage= ...
- memcached优化方法
工作原理 基本概念:slab,page.chunk. slab,是一个逻辑概念. 它是在启动memcached实例的时候预处理好的,每一个slab相应一个chunk size.也就是说 ...
- weexpack build android 和 weexpack run android 报错 及 解决方案
1. weexpack build android (1)Configuring > 0/3 projects > root project > Resolving dependen ...
- 用python编写的简易端口扫描器
#coding = utf-8 ''' python 3.4 通过简单的TCP端口连接来判断指定IP是否开放了指定端口. ''' import socket import optparse impor ...
- mysql的DUPLICATE KEY
经常遇到这样的情景,向一个表里插入一条数据,如果已经存在就更新一下,用程序实现麻烦而且在并发的时候可能会有问题,这时用mysql的DUPLICATE KEY 很方便 用法如下: INSERT INTO ...