ACDream - Dynamic Inversions II
先上题目:
A - Dynamic Inversions II
Problem Description
逆序对的意思是1 <= i < j <= N 且a[i] > a[j].
Input
多组数据,每组数据:
两个数N,M,接下来一行有N个数a[1]... a[N]
最后M行每行两个数x,y
1 <= N,M <= 10^5, 1 <= x < y <= N,1 <= a[i] <= N
Output
Sample Input
2 1
1 2
1 2
Sample Output
0
1 因为结果要求的是逆序对的二进制最低位是多少,所以我们需要分析一下变换了位置以后的就变化情况。
先求一次逆序对。
再分析情况,发现逆序对的奇偶性变化只有两个数之间的数会带来变化。
① ······大····小······ --> ······小····大······
中间的数有三种情况:a.比大的大 b.比大的小,比小的大 c.比小的小
对于三种情况逆序对的变化情况:
| 小 | 大 | ||||
| 大 | 减少 | 减少 | 增加 | ||
| 小 | 增加 | 减少 | 减少 |
其中减少和增加的量是相等的,那就是说这样变化的结果是偶数对-1对。
② ······小····大······ --> ······大····小······
中间的数有三种情况:a.比大的大 b.比大的小,比小的大 c.比小的小
对于三种情况逆序对的变化情况:
| 小 | 大 | ||||
| 大 | 增加 | 增加 | 减少 | ||
| 小 | 减少 | 增加 | 增加 |
其中减少和增加的量是相等的,那就是说这样变化的结果是偶数对+1对。 ③ ······a····a······ -->不变 所以我们需要做的是判断交换的两个数是不是相等,如果是相等就不变化奇偶性,否则奇偶性变化一次。 上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) (x & (-x))
#define MAX 100002
#define LL long long
using namespace std; int n,m; int c[MAX],a[MAX]; void add(int x){
for(;x<=n;x+=lowbit(x)) c[x]++;
} LL sum(int x){
LL ans=;
for(;x>;x-=lowbit(x)) ans+=c[x];
return ans;
} int main()
{
int x,y;
LL s;
while(scanf("%d %d",&n,&m)!=EOF){
memset(c,,sizeof(c));
s=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
add(a[i]);
s+=sum(n)-sum(a[i]);
}
bool f=s&;
if(f) puts("");
else puts("");
for(int i=;i<m;i++){
scanf("%d %d",&x,&y);
if(x!=y && a[x]!=a[y]) f=f^;
swap(a[x],a[y]);
if(f) puts("");
else puts("");
}
}
return ;
}
Dynamic InversionsII
ACDream - Dynamic Inversions II的更多相关文章
- Dynamic Inversions II 逆序数的性质 树状数组求逆序数
Dynamic Inversions II Time Limit: 6000/3000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Other ...
- Dynamic Inversions 50个树状数组
Dynamic Inversions Time Limit: 30000/15000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others ...
- [Express] Level 3: Massaging User Data
Flexible Routes Our current route only works when the city name argument matches exactly the propert ...
- Kooboo中怎么新增一个关联的Details 动态页面。
Kooboo中怎么新增一个关联的Details 动态页面. 有几个要点: 1. Sub Page的Parent Page 必须是英文书写.如果是中文会出现找不到页面 500错误 2. 要在Page M ...
- 国外成熟的程序交易系统的思路 z
波涛(1998)在<系统交易方法>中提出,一个设计良好的交易系统,必须对投资决策的各个相关环节做出相应明确的规定,同时还必须符合使用者的心理特征.投资对象的统计特征以及投资资金的风险特征. ...
- Reading task(Introduction to Algorithms. 2nd)
Introduction to Algorithms 2nd ed. Cambridge, MA: MIT Press, 2001. ISBN: 9780262032933. Introduction ...
- oracle已知会导致错误结果的bug列表(Bug Issues Known to cause Wrong Results)
LAST UPDATE: 1 Dec 15, 2016 APPLIES TO: 1 2 3 4 Oracle Database - Enterprise Edition - Versi ...
- LEETCODE —— Unique Paths II [动态规划 Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
- LEETCODE —— Unique Paths II [Dynamic Programming]
唯一路径问题II Unique Paths II Follow up for "Unique Paths": Now consider if some obstacles are ...
随机推荐
- ConfigSections配置
如果配置文件中包含了configSections,那么configSections的下一个节点,必须是configSections里面的元素 <configSections> <se ...
- (Go)07.strings与strconv的示例
package main import ( "strconv" "fmt" "strings" ) func main() { str := ...
- mysql重设root的密码 mac
创建: 2017/09/14 第一步: 关闭已开启的mysql服务器 mysql.server stop 第二步: 关闭密码识别模式 /usr/local/bin/mysqld_safe ...
- Drainage Ditches(网络流(EK算法))
计算最大流,EK算法模板题. #include <stdio.h> #include <string.h> #include <queue> using names ...
- 安装Windows包管理工具Chocolatey
1.开始菜单里面用PS的管理员模式打开,执行一下命令. Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object Syste ...
- JS——事件详情(鼠标事件:clientX、clientY的用法)
鼠标位置 >可视区位置:clientX.clientY 跟着鼠标移动的div案例 代码如下图: 这个案例,运用到前一篇文章中的event事件来处理.获取div的left和top值,当鼠标移动 ...
- GStreamer系列 - 基本介绍
什么是Gstreamer? Gstreamer是一个支持Windows,Linux,Android, iOS的跨平台的多媒体框架,应用程序可以通过管道(Pipeline)的方式,将多媒体处理的各个步骤 ...
- PHP 二维数组排序 可以按指定 键值排序
<?php header("Content-Type:utf-8"); $arr = array( 0 => array( 'name' => '国际原油价格', ...
- java中强制类型转换时,高位数截取成低位数的方法
/** * 强制类型转换中的补码.反码.原码一搞清楚 */ int b=233;//正整数强转 System.out.println((byte)b); //负数:原码的绝对值取反再加一 符号为不变 ...
- swift-delegate(代理)或者block传值
1:delegate或者block传值 import UIKit class ViewController: UIViewController,TestDelegatePassValueDelegat ...