[CC-COUPLES]Couples sit next to each other
[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的更多相关文章
- [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 ...
- [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 ...
- ZOJ 3161 Damn Couples 动态规划 难度:2
Damn Couples Time Limit: 1 Second Memory Limit: 32768 KB As mentioned in the problem "Coup ...
- 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 ...
- [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 ...
- 【LeetCode】765. Couples Holding Hands 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/couples- ...
- Sicily 1021. Couples
题目地址:1021. Couples 思路: 想清楚了这道题其实很简单.利用夫妻出现的位置作为下标,并设为同一值,第一对夫妻值为1,第二对为2,以此类推,存储完毕即可进入下一步. 利用栈这个数据结构: ...
- 每日英语: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 ...
- Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands)
Leetcode之并查集专题-765. 情侣牵手(Couples Holding Hands) N 对情侣坐在连续排列的 2N 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
随机推荐
- Jmeter测试demo
复制代码,保存为.jmx文件 需要安装插件: JMeterPlugins-ExtrasLibs E:\软件\apache-jmeter-3.0\lib\ext <?xml version=&qu ...
- c++实现 给定直角停车位两个点,求取剩余两点坐标。
//2018-09-08-fourmi /*************************include head files************************************ ...
- stream to byte[], byte[] to srting
byte[] myBinary = new byte[paramFile.Length]; paramFile.Read(myBinary, , (int)paramFile.Length); str ...
- PyOpenCV图像逆时针旋转90度
warpAffine方法效果很搓,留下大片黑色区域. 使用flip和transpose可以实现逆时针旋转90度.先flip或先transpose均可. #coding:utf-8 import cv2 ...
- DevExpress中的GridControl控件设置了列Readonly后,想双击弹出明细的实现
OptionsBehavior.Editable = true时,会有二个对象触发事件:view触发ShownEditor 事件(第一单击时)和内置编辑器的DoubleClick事件所以必须处理这二个 ...
- XmlSerializer 实现序列化CDATA
[XmlIgnore] public string GuestRemarks { get; set; } [XmlElement("GuestRemarks")] public X ...
- MySQL应用异常问题解决
MySQL错误:Every derived table must have its own alias 派生表都必须有自己的别名 一般在多表查询时,会出现此错误. 因为,进行嵌套查询的时候子查询出来的 ...
- JSP基础知识➣结构及生命周期(一)
概述 网络服务器需要一个JSP引擎,也就是一个容器来处理JSP页面.容器负责截获对JSP页面的请求.本教程使用内嵌JSP容器的Apache来支持JSP开发. JSP容器与Web服务器协同合作,为JSP ...
- la 4490
题解: 这道思路还是比较水的 我们可以等价变形成hi<=7 我们的最优决策是把抽出来的那些相同颜色的书最后插在一起(所以要统计序列中还有没有相同元素的书) f[i][j][k][x]表示前i本书 ...
- 一起学Hadoop——二次排序算法的实现
二次排序,从字面上可以理解为在对key排序的基础上对key所对应的值value排序,也叫辅助排序.一般情况下,MapReduce框架只对key排序,而不对key所对应的值排序,因此value的排序经常 ...