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] 输出 ...
随机推荐
- sql 注入 及 in 注入
原文地址: http://www.cnblogs.com/lzrabbit/archive/2012/04/22/2465313.html 除了校验参数内容,过滤长度和sql关键字. 解决in条件拼接 ...
- Day 11 正则表达式
正则表达式 一.简介 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配到的行打印出来.grep全称是Globally search for a Regular ...
- compose.yml模板文件
默认的模板文件名称为 docker-compose.yml,格式为 YAML 格式. 示例: version: " services: webapp: image: examples/web ...
- org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32
org.apache.catalina.connector.ClientAbortException: java.io.IOException: APR error:-32 Most likely, ...
- UICollectionView 使用 介绍
1.1. Collection View 全家福: UICollectionView, UITableView, NSCollectionView n 不直接等效于NSCollectionView ...
- Go -- 交叉编译
编译Windows程序和mac程序
- IE浏览器打不开解决的方法
windows 7和windows 8上的IE浏览器打不开.非常可能是权限问题,解决的方法: 点击"開始"-"执行",输入"regedit" ...
- linux cat 文件操作
简略版: cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename 只能创建新文件,不能编辑已有文件.3.将几个文 ...
- 实现多线程的方式Runnable
package com.thread.runnable; /** * 实现多线程的方式有继承Thread类和实现Runnable接口两种方式 * 哪种方式更好呢?实现的方式由于继承的方式. * 原因: ...
- iOS开发核心语言Objective C —— 面向对象思维、setter和getter方法及点语法
本分享是面向有意向从事iOS开发的伙伴们.或者已经从事了iOS的开发人员.假设您对iOS开发有极高的兴趣,能够与我一起探讨iOS开发.一起学习,共同进步.假设您是零基础,建议您先翻阅我之前分享的iOS ...