[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 个座位上,想要牵到对方的手. 计算最少交换座位的次数,以便每对情侣可以并 ...
 
随机推荐
- oracle数据库无法连接 The Network Adapter could not establish
			
Caused by: java.sql.SQLException: Io 异常: The Network Adapter could not establish the connection 这个错误 ...
 - ActiveSync之HTTP
			
Exchange ActiveSync:HTTP协议让客户端能够同步存储在服务器上的数据. 互联网工程工作小组(IETF)要求所有互联网协议都比支持UTF-8编码.因此,ActiveSync传输所使用 ...
 - 饮冰三年-人工智能-Python-15Python基础之文件处理
			
# 写文件--开始 f=open('test','w',encoding="utf-8") #如果有则写,没有就创建 print(f.writable()) #True f.wri ...
 - Ajax爬虫必用到的字典转换器
			
1.使用情景 在我们Ajax爬虫时需要用到以下这样的数据的时候我们会一个一个地复制粘贴,这样会很麻烦 def dictionary_converter(key_value): '''主要用于爬虫时复制 ...
 - 插件使用一进度条---nprogress
			
nprogress 是像youtube一样在顶部出现进度条,用在一些加载比较缓慢的场景中. 官方网站是 http://ricostacruz.com/nprogress/ 源码在 https://gi ...
 - C. cltt的幸运数LCAtarjan
			
/*C: cltt的幸运数 Time Limit: 1 s Memory Limit: 128 MB Submit Problem Description 一棵树有n个节点,共m次查询,查询 ...
 - Python深度学习案例1--电影评论分类(二分类问题)
			
我觉得把课本上的案例先自己抄一遍,然后将书看一遍.最后再写一篇博客记录自己所学过程的感悟.虽然与课本有很多相似之处.但自己写一遍感悟会更深 电影评论分类(二分类问题) 本节使用的是IMDB数据集,使用 ...
 - C# 之 判断一个字符是否是汉字
			
判断一个字符是不是汉字通常有三种方法: [1] 用 ASCII 码判断:[2] 用汉字的 UNICODE 编码范围判断:[3] 用正则表达式判断. 1.用ASCII码判断 在 ASCII码表中,英文的 ...
 - Django Models的数据类型汇总
			
https://blog.csdn.net/devil_2009/article/details/41735611 Django Models的数据类型 汇总 AutoField IntegerFie ...
 - python基础——函数
			
1.内置函数的调用: 可以在官方网站查找内置函数及说明,也可以通过help(abs)函数查看相应的信息. https://docs.python.org/3/library/functions.htm ...