codeforces 911D
2 seconds
256 megabytes
standard input
standard output
A permutation of size n is an array of size n such that each integer from 1 to n occurs exactly once in this array. An inversion in a permutation p is a pair of indices (i, j) such that i > j and ai < aj. For example, a permutation [4, 1, 3, 2] contains 4 inversions: (2, 1), (3, 1), (4, 1), (4, 3).
You are given a permutation a of size n and m queries to it. Each query is represented by two indices l and r denoting that you have to reverse the segment [l, r] of the permutation. For example, if a = [1, 2, 3, 4] and a query l = 2, r = 4 is applied, then the resulting permutation is [1, 4, 3, 2].
After each query you have to determine whether the number of inversions is odd or even.
The first line contains one integer n (1 ≤ n ≤ 1500) — the size of the permutation.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of the permutation. These integers are pairwise distinct.
The third line contains one integer m (1 ≤ m ≤ 2·105) — the number of queries to process.
Then m lines follow, i-th line containing two integers li, ri (1 ≤ li ≤ ri ≤ n) denoting that i-th query is to reverse a segment [li, ri] of the permutation. All queries are performed one after another.
Print m lines. i-th of them must be equal to odd if the number of inversions in the permutation after i-th query is odd, and even otherwise.
3
1 2 3
2
1 2
2 3
odd
even
4
1 2 4 3
4
1 1
1 4
1 4
2 3
odd
odd
odd
even
The first example:
- after the first query a = [2, 1, 3], inversion: (2, 1);
- after the second query a = [2, 3, 1], inversions: (3, 1), (3, 2).
The second example:
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [3, 4, 2, 1], inversions: (3, 1), (4, 1), (3, 2), (4, 2), (4, 3);
- a = [1, 2, 4, 3], inversion: (4, 3);
- a = [1, 4, 2, 3], inversions: (3, 2), (4, 2).
(这一题思路很巧妙
题意:给n个不相同的数,每次询问都会将l到r的数翻转,判断每次翻转后所有数的逆序数和的奇偶性。
解题思路:因为每段序列的逆序数和最大的情况是该序列从大到小排序,此时逆序数和为max=(r-l+1)*(r-l)/2 。那么设序列的逆序数和为m,则其翻转后为max-m。若max为偶数,则m与max-m的奇偶性一致,也就是说翻转无影响。而若max为奇数,则m与max-m的奇偶性不同,则翻转会改变答案。
也就是说,只需要先求出整个序列的逆序数和的奇偶性,再根据每次翻转,判断是否会改变答案的奇偶性就行了。
ac代码如下:
1 #include <cstdio>
2 #include <cstring>
3 #include <iostream>
4 using namespace std;
5 const int maxn = 1500+10;
6 int nu[maxn];
7 int cnt=0;
8 int main()
9 {
10 int n;
11 scanf("%d",&n);
12 for(int i=1;i<=n;++i)
13 scanf("%d",&nu[i]);
14 for(int i=1;i<=n;++i)
15 {
16 for(int j=1;j<i;++j)
17 {
18 if(nu[j]>nu[i])
19 {
20 cnt++;
21 }
22 }
23 }
24 cnt%=2;
25 int m;
26 scanf("%d",&m);
27 int l,r;
28 for(int i=1;i<=m;++i)
29 {
30 scanf("%d%d",&l,&r);
31 if((r-l+1)*(r-l)/2%2==1) cnt^=1;
32 if(cnt) printf("odd\n");
33 else printf("even\n");
34 }
35 return 0;
36 }
codeforces 911D的更多相关文章
- Codeforces 911D. Inversion Counting (数学、思维)
题目链接:Inversion Counting 题意: 定义数列{ai|i=1,2,...,n}的逆序对如下:对于所有的1≤j<i≤n,若ai<aj,则<i,j>为一个逆序对. ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
- CodeForces - 662A Gambling Nim
http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...
- CodeForces - 274B Zero Tree
http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...
- CodeForces - 261B Maxim and Restaurant
http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...
- CodeForces - 696B Puzzles
http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...
随机推荐
- 容器编排系统K8s之包管理器helm基础使用(二)
前文我们介绍了helm的相关术语和使用helm安装和卸载应用,回顾请参考:https://www.cnblogs.com/qiuhom-1874/p/14305902.html:今天我们来介绍下自定义 ...
- playwright自动化项目搭建
这是关于playwright系列介绍的最后一篇.搭建基于 playwright 的自动化项目. GitHub地址: https://github.com/defnngj/playwright-pro ...
- 低功耗降线性稳压器,24V转5V降压芯片
PW2330开发了一种高效率的同步降压DC-DC变换器3A输出电流.PW2330在4.5V到30V的宽输入电压范围内工作集成主开关和同步开关,具有非常低的RDS(ON)以最小化传导 损失.PW2330 ...
- 注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途
注解 @AutoConfigureBefore 和 @AutoConfigureAfter 的用途 介绍: 如果你想将在SpringBoot项目中的配置类进行排序,那么用到spring-boot-au ...
- 精通MySQL之架构篇
老刘是即将找工作的研究生,自学大数据开发,一路走来,感慨颇深,网上大数据的资料良莠不齐,于是想写一份详细的大数据开发指南.这份指南把大数据的[基础知识][框架分析][源码理解]都用自己的话描述出来,让 ...
- 十九、更改LCD显示屏
一.裸机修改 之前测试用的屏幕是480*272的分辨率,现在要换成800*480的屏,因此要对软件代码进行修改. 对于裸机驱动而言,主要有两个点需要注意,一个是屏幕分辨率变了,因此初始化的时候与屏幕分 ...
- virtualenv安装和配置
安装命令 命令执行结束 配 执行命令:virtualenv testvir 执行完成:会在当前目录下生成如下文件夹 进入到testvir目录 进入Scripts目录: 进入虚拟环境:执行 activa ...
- Asp.netCore 3.1控制器属性注入and异步事务Aop by AutoFac
Aspect Oriented Programming(AOP)是较为热门的一个话题.AOP,国内我们都习惯称之为:面向切面编程 下面直接code 干货展示:(一般人我还不告诉,嘻嘻) 1:导入相关的 ...
- Solon rpc 1.3.1 发布,推出Cloud接口与配置规范
Solon 是一个微型的Java RPC开发框架.项目从2018年启动以来,参考过大量前人作品:历时两年,3500多次的commit:内核保持0.1m的身材,超高的跑分,良好的使用体验.支持:Rpc. ...
- docker版mysql的使用和配置(1)——docker的基本操作
最近实在是忙成狗,其他的内容等稍微闲一点了一起更新. 这篇主要是讲docker版的mysql的使用和配置信息.因为实习公司需要搞一个docker做测试环境用,还需要包括基本的依赖.最重要的是,因为这个 ...