[CC-COUPLES]Couples sit next to each other

题目大意:

有\(n(n\le5\times10^5)\)对小伙伴共\(2n\)个人坐成一圈。刚开始编号为\(i\)的人坐在第\(i\)个座位上。每次可以让相邻的两个人交换座位。问要让每一对小伙伴的座位都相邻至少需要多少次交换?

思路:

答案为每一对两个人距离之和-“交叉”的小伙伴的对数。树状数组维护即可。

时间复杂度\(\mathcal O(n\log n)\)。

源代码:

#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=5e5+1;
int n,a[N*2],pos[N][2],cnt[N];
class FenwickTree {
private:
int val[N*2];
int lowbit(const int &x) const {
return x&-x;
}
public:
void modify(int p,const int &x) {
for(;p<=n*2;p+=lowbit(p)) {
val[p]+=x;
}
}
int query(int p) const {
int ret=0;
for(;p;p-=lowbit(p)) {
ret+=val[p];
}
return ret;
}
int query(const int &l,const int &r) const {
return query(r)-query(l-1);
}
};
FenwickTree t;
int main() {
for(register int T=getint();T;T--) {
n=getint();
std::fill(&cnt[1],&cnt[n]+1,0);
for(register int i=1;i<=n*2;i++) {
const int &x=a[i]=getint();
pos[x][cnt[x]++]=i;
}
int64 ans=0;
for(register int i=1;i<=n;i++) {
ans+=std::min(pos[i][1]-pos[i][0],n*2+pos[i][0]-pos[i][1])-1;
}
for(register int i=1;i<=n*2;i++) {
const int &x=a[i];
if(i==pos[x][0]) {
t.modify(i,1);
}
if(i==pos[x][1]) {
t.modify(pos[x][0],-1);
ans-=t.query(pos[x][0],i);
}
}
printf("%lld\n",ans);
}
return 0;
}

[CC-COUPLES]Couples sit next to each other的更多相关文章

  1. [LeetCode] Couples Holding Hands 两两握手

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  2. [Swift]LeetCode765. 情侣牵手 | Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  3. ZOJ 3161 Damn Couples 动态规划 难度:2

    Damn Couples Time Limit: 1 Second      Memory Limit: 32768 KB As mentioned in the problem "Coup ...

  4. LeetCode765. Couples Holding Hands

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  5. [LeetCode] 765. Couples Holding Hands 情侣牵手

    N couples sit in 2N seats arranged in a row and want to hold hands. We want to know the minimum numb ...

  6. 【LeetCode】765. Couples Holding Hands 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/couples- ...

  7. Sicily 1021. Couples

    题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...

  8. 每日英语:Why Rate Your Marriage? A Numerical Score Can Help Couples Talk About Problems

    When marriage therapist Sharon Gilchrest O'Neill met with new clients recently, she asked them why t ...

  9. Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)

    Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...

随机推荐

  1. C++ Primer 笔记——类

    1.定义在类内部的函数是隐式的inline函数. 2.因为this的目的总是指向“这个”对象,所以this是一个常量指针,我们不允许改变this中保存的地址. 3.常量成员函数:允许把const关键字 ...

  2. windows 下命令行启动停止mysql

    MySQL比较好玩一点就是它可以用多种方式启动,当然它也可以用多种方式关闭.下面我就mysql的几种启动方式简单的谈一谈,希望可以给大家提供一些参考. 第一种,用mysqld-nt来启动. 在没有进行 ...

  3. python面向对象三大特性-多态

    import abc #利用abc模块实现抽象类 class All_file(metaclass=abc.ABCMeta): all_type='file' @abc.abstractmethod ...

  4. python--使用递归优雅实现列表相加和进制转换

    咦,好像坚持了一段时间,感觉又有新收获啦. # coding: utf-8 class Stack: def __init__(self): self.items = [] # 是否为空 def is ...

  5. expdp、impdp 使用sys用户操作时的注意事项

    https://blog.csdn.net/ctypyb2002/article/details/78420711

  6. RSA javascript加密 lua解密

    一个在线RSA非对称加密解密,可以用这个地址生成公钥和私钥 https://blog.zhengxianjun.com/online-tool/rsa/ javascript加密 jsencrypt. ...

  7. PHP微信模板消息发送

    <?php class Wxtemplate extends Base { function __construct() { $this->appid = config('WXAPP_AP ...

  8. zTree的使用教程

    1.首先去官网下载http://www.ztree.me/v3/main.php#_zTreeInfo 2.之后引入: <script src="js/jquery.ztree.all ...

  9. order by 导出数据不一致的问题

    转自 http://blog.csdn.net/wwwwei_csdn/article/details/78181197 前言   数据库分页是后台经常要使用的技术手段,有时候进行数据库查询会根据业务 ...

  10. yum安装的时候报错,关于python的函数库

    我在执行yum -y install nc命令的时候出现如下报错 There was a problem importing one of the Python modulesrequired to ...