A. AquaMoon and Strange Sort

叉人题

如果数字各不相同,只需要统计每个数参与构成的逆序对总数,如果是奇数一定最终朝左,偶数朝右。无意义的数字交换对方向是没有影响的

继续考虑相同数字带来的影响。逆序对考虑的交换次数是最小交换,是保守排序,相同数字的相对位置不变。把交换次数的序列按奇偶提取出来,比如001110 001100。每次操作交换两个数字然后把它翻转,那么只有对相同的相邻数进行操作才有意义。

模型转化为给一个01序列,每次选择两个相邻的相同数,把它们翻转,问最后能否化成全0序列。

100100也是可行的。100100->111111->000000

把序列想象成多个连续的01段拼接。偶数段是可以任意转化的,对答案不产生影响,能把左右直接相连。奇数段则不行,需要把它压成一个数。用栈来考虑这个问题,如果是偶数段,什么也不插入,如果是奇数段,会插入一个0/1,如果栈尾和新插入的元素相同,说明能合并,如果不同就推进栈尾。在最终的栈中,如果栈空或者只有一个0,则合法,其他情况我们处理不掉1,均不合法。

 1 #include <vector>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 #define ll long long
6 using namespace std;
7 const int maxn=1e5, N1=maxn+5;
8
9 template <typename _T> void read(_T &ret)
10 {
11 ret=0; _T fh=1; char c=getchar();
12 while(c<'0'||c>'9'){ if(c=='-') fh=-1; c=getchar(); }
13 while(c>='0'&&c<='9'){ ret=ret*10+c-'0'; c=getchar(); }
14 ret=ret*fh;
15 }
16
17 int n,T,ma;
18 int a[N1],num[N1];
19 vector<int>to[N1];
20 int stk[N1],tp;
21
22 struct BIT{
23 int sum[N1];
24 void upd(int x,int w){ for(int i=x;i<=ma;i+=i&(-i)) sum[i]+=w; }
25 int query(int x){ int ans=0; for(int i=x;i;i-=i&(-i)) ans+=sum[i]; return ans; }
26 void clr(){ for(int i=0;i<=ma;i++) sum[i]=0; }
27 }s;
28
29 int main()
30 {
31 // freopen("a.in","r",stdin);
32 scanf("%d",&T);
33 while(T--){
34
35 scanf("%d",&n); ma=0;
36 for(int i=1;i<=n;i++) read(a[i]), ma=max(a[i],ma);
37 for(int i=1;i<=n;i++)
38 {
39 num[i]=s.query(ma)-s.query(a[i]);
40 s.upd(a[i],1);
41 }
42 s.clr();
43 for(int i=n;i>=1;i--)
44 {
45 num[i]+=s.query(a[i]-1);
46 s.upd(a[i],1);
47 to[a[i]].push_back(num[i]&1);
48 }
49 s.clr();
50 int fl=1;
51 for(int k=1;k<=ma;k++)
52 {
53 tp=0;
54 for(int i=0,j;i<to[k].size();)
55 {
56 for(j=i;j<to[k].size() && to[k][j]==to[k][i];j++);
57 if((j-i)&1)
58 {
59 if(!tp) stk[++tp]=to[k][i];
60 else if(stk[tp]==to[k][i]) tp--;
61 else stk[++tp]=to[k][i];
62 }
63 i=j;
64 }
65 if(tp>1||(tp==1&&stk[tp]==1)) fl=0;
66 to[k].clear(); tp=0;
67 }
68 puts(fl?"YES":"NO");
69
70 }
71 return 0;
72 }

CF1545X Codeforces Round #732的更多相关文章

  1. Codeforces Round #732 (Div. 2)【ABCD】

    比赛链接:https://codeforces.com/contest/1546 A. AquaMoon and Two Arrays 题意 给出两个大小为 \(n\) 的数组 \(a, b\) ,每 ...

  2. Codeforces Round #732 (Div.1) 题解

    实在是打击人信心的一场比赛啊--一不注意就掉了 50+ 分,rating 没了啊/ll/dk/wq/kk A Weak pretest!!!!!11 /fn/fn/fn 一个很显然的注意点是在交换前后 ...

  3. Codeforces Round #366 (Div. 2) ABC

    Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...

  4. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

  5. Codeforces Round #368 (Div. 2)

    直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...

  6. cf之路,1,Codeforces Round #345 (Div. 2)

     cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅.....   ...

  7. Codeforces Round #279 (Div. 2) ABCDE

    Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems     # Name     A Team Olympiad standard input/outpu ...

  8. Codeforces Round #262 (Div. 2) 1003

    Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...

  9. Codeforces Round #262 (Div. 2) 1004

    Codeforces Round #262 (Div. 2) 1004 D. Little Victor and Set time limit per test 1 second memory lim ...

随机推荐

  1. Solution -「多校联训」朝鲜时蔬

    \(\mathcal{Description}\)   Link.   破案了,朝鲜时蔬 = 超现实树!(指写得像那什么一样的题面.   对于整数集 \(X\),定义其 好子集 为满足 \(Y\sub ...

  2. 【ybtoj】贪心算法例题

    [基础算法]第二章 贪心算法 例一 奶牛晒衣服 题目描述 有n件衣服,第i件衣服的湿度为h. 在自然条件下,每件衣服每分钟都可以自然晒干A点湿度. 在烘干机作用下,可以选择一件衣服,用一分钟的时间晒干 ...

  3. 【论文考古】Training a 3-Node Neural Network is NP-Complete

    今天看到一篇1988年的老文章谈到了训练一个简单网络是NPC问题[1].也就是下面的网络结构,在线性激活函数下,如果要找到参数使得输入数据的标签估计准确,这个问题是一个NPC问题.这个文章的意义在于宣 ...

  4. 【Azure 应用服务】部署Jar到App Service for Linux,因启动命令路径配置错误而引起:( Application Error 问题

    问题描述 App Service for Linux 资源创建完成后,通过FTP方式把 .jar包(logdemo.jar)包上传到 /site/wwwroot/ 文件夹后,在App Service的 ...

  5. CentOS8系统上Zookeeper安装启动

    下载Zookeeper安装包,下载地址为:https://zookeeper.apache.org/releases.html.注意选择bin的gz包,否则安装不成功,提示找不到相应的类. 一.单机模 ...

  6. nginx负载均衡中利用redis解决session一致性问题

    关于session一致性的现象及原因不是本小作文的重点,可以另行找杜丽娘O(∩_∩)O哈哈~重点是利用redis集中存储共享session的实际操作. 一.业务场景:nginx/tomcat/redi ...

  7. mysql视图,索引

    一.视图 View 视图是一个虚拟表,是sql语句的查询结果,其内容由查询定义.同真实的表一样,视图包含一系列带有名称的列和行数据,在使用视图时动态生成.视图的数据变化会影响到基表,基表的数据变化也会 ...

  8. git 提交忽略文件

    target/ */target/ **/logs/ !.mvn/wrapper/maven-wrapper.jar ### STS ### .apt_generated .classpath .fa ...

  9. 搭建开源跳板机——jumpserver

    搭建开源跳板机mobaxterm 官方文档:https://jumpserver.readthedocs.io/zh/master/ $ yum update -y # 防火墙 与 selinux 设 ...

  10. k8s中运行busybox

    简介 参考百度百科 BusyBox 是一个集成了三百多个最常用Linux命令和工具的软件. BusyBox 包含了一些简单的工具,例如ls.cat和echo等等,还包含了一些更大.更复杂的工具,例gr ...