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] 输出 ...
随机推荐
- WebView跳转到底部
webview中有个computeVerticalScrollRange方法,是protected的,可以用反射,也可以自己写一个view继承webview,实现computeVerticalScro ...
- 快速掌握RabbitMQ(三)——消息确认、持久化、优先级的C#实现
1 消息确认 在一些场合,如转账.付费时每一条消息都必须保证成功的被处理.AMQP是金融级的消息队列协议,有很高的可靠性,这里介绍在使用RabbitMQ时怎么保证消息被成功处理的.消息确认可以分为两种 ...
- BZOJ——1202: [HNOI2005]狡猾的商人
http://www.lydsy.com/JudgeOnline/problem.php?id=1202 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
- DELPHI方法注释的标准写法
/// <summary> /// 查询数据 /// </summary> /// <param name="accountno">帐套号< ...
- 使用git 高效多人合作
复习一下... 附加学习链接: http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/) ...
- 自动化运维工具Fabric - 密码管理(env.password and ssh key)
在使用 Fabric 的过程中,如果大批量服务器处理的话,我们就需要针对配置主机的密码,每台主机的密码相同还好,不同的话,就需要针对不同的主机做配置了,以下有两种配置方式 注:本文主要参考官方文档 P ...
- 零基础学python-3.2 变量赋值
这一节我们来具体展开变量赋值 1.以下我们举各种赋值的样例 anInt=12 anFloat=2.2 anStr='string' aList=['a','a','a'] anArray=(1,2,3 ...
- SolidWorks如何绘制抽壳零件
1 绘制一个零件,点击抽壳 2 你可以一个一个面选,也可以直接选中一个零件,对他的所有面都薄壳处理(右击弹出菜单选择确定即可) 3 可以用剖视图检查是否抽壳成功 4 对于复杂的零件,一个一 ...
- 【Leetcode】经典的Jump Game in JAVA
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [Elasticsearch] 部分匹配 (四) - 索引期间优化ngrams及索引期间的即时搜索
本章翻译自Elasticsearch官方指南的Partial Matching一章. 索引期间的优化(Index-time Optimizations) 眼下我们讨论的全部方案都是在查询期间的.它们不 ...