CSAcademy Beta Round #4 Swap Pairing
题目链接:https://csacademy.com/contest/arhiva/#task/swap_pairing/
大意是给2*n个包含n种数字,每种数字出现恰好2次的数列,每一步操作可以交换相邻的两个数字,问最少需要操作多少次,可以使得所有的同种数字都相邻。
我的做法是考虑不同的数对的数字在原来数列中的位置关系,有三大类,如果我们用[]和{}表示的话就是:
[]{}
[{]}
[{}]
这三种位置情况。
第一种情况对答案的贡献应当是0。
第二种情况对答案的贡献应当是1。
第三种情况对答案的贡献应当是2。
接下来问题就是如何统计。我的方法是先计算所有的pair对,
总共n种数字,那么数对的个数就是(n-1)*n/2。
接下来需要减去第一种情况的个数,再加上第三种情况的个数。
第一种情况的统计应当是比较简单的,第三种情况可以规约到求某个线段内部有多少线段的问题。
统计可以用BIT来维护。
代码如下:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <ctime>
#include <set>
using namespace std; const int N=;
int a[N];
map<int,int> le;
map<int,int> ri; pair<int,int> pos[N];
int v[N];
int lowbit(int x) {
return x & -x;
}
int get(int x) {
int ret=;
while (x) {
ret+=v[x];
x-=lowbit(x);
}
return ret;
}
void add(int x,int add) {
while (x<N) {
v[x]+=add;
x+=lowbit(x);
}
}
int main () {
int n;
while (scanf("%d",&n)!=EOF) {
memset(v,, sizeof(v));
le.clear();
ri.clear();
for (int i=;i<=n;i++) {
scanf("%d",a+i);
if (le[a[i]]==)
le[a[i]]=i;
else ri[a[i]]=i;
}
int cnt=;
for (map<int,int>::iterator it=le.begin();it!=le.end();it++) {
int key=it->first;
int val1=it->second;
int val2=ri[key];
pos[cnt].first=val1;
pos[cnt++].second=val2;
}
sort(pos+,pos+cnt);
int tot=n/;
long long ret=(tot-)*1LL*tot/2LL;
for (int i=tot;i>=;i--) {
int l=pos[i].first;
int r=pos[i].second;
ret+=get(r);
add(r,);
}
memset(v,,sizeof v);
for (int i=;i<=tot;i++) {
int l=pos[i].first;
int r=pos[i].second;
ret-=get(l);
add(r,);
}
cout<<ret<<endl; }
}
题解给了另一种做法,可以有一种最优解第一个数字不用移动,则接下来所有数字怎么移动都被固定了,中间仍然是用BIT来维护。
CSAcademy Beta Round #4 Swap Pairing的更多相关文章
- CSAcademy Beta Round #5 Force Graph
题目链接:https://csacademy.com/contest/arhiva/#task/force_graph/ 大意是有若干个节点,每个节点对应一个二维坐标,节点之间相互有斥力存在.同时有些 ...
- CSAcademy Beta Round #5 Long Journey
题目链接:https://csacademy.com/contest/arhiva/#task/long_journey/ 大意是有一张无向不带权的图,两个人同时从s点出发,分别前往a点和b点,且每个 ...
- CSAcademy Beta Round #3 a-game
题目连接 a-game 大意:有一个只包含A和B的字符串,两个人分别取这个串的子串,但是每一次取不能与之前取到过的子串有交集,最后谁取到的所有串中A的总数量少的判为胜.如果一样,则为平手. 给出这样的 ...
- Codeforces Beta Round #69 (Div. 2 Only)
Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...
- Codeforces Beta Round #67 (Div. 2)
Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #49 (Div. 2)
Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #35 (Div. 2)
Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看 ...
- Codeforces Beta Round #32 (Div. 2, Codeforces format)
Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...
- Codeforces Beta Round #31 (Div. 2, Codeforces format)
Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include< ...
随机推荐
- Struts文件下载
/* 文件下载的先决条件 * 1. 在xml配置文件中必须配置一个type="stream"的result, result中不需要填写任何内容 * 2. 在Action中编写一个接 ...
- HTML5—canvas绘制图形(1)
1.canvas基础知识 canvas元素是HTML5中新增的一个重要的元素,专门用来绘制图形,不过canvas本身不具备画图的能力,在页面中放置了canvas元素,就相当于在页面中放置了一块矩形的“ ...
- fopen中的mode(20161115)
mode mode 参数指定了所要求到该流的访问类型.可以是以下: fopen() 中 mode 的可能值列表 mode 说明 'r' 只读方式打开,将文件指针指向文件头. 'r+' 读写方式打开,将 ...
- Win7下Nginx的安装与配置,win7nginx配置
环境介绍:Win7 64位SP1 Nginx版本:nginx/1.8.0 参考链接http://nginx.org/en/docs/windows.html 1. 下载nginx1.8.0版本: h ...
- Fibonacci数列前n项值的输出(运用递归算法)
1.斐波那契数列: 又称黄金分割数列,指的是这样一个数列:0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ... 在数学上,斐波纳契数列以如下被以递归的方法 ...
- 基于EM的多直线拟合
作者:桂. 时间:2017-03-22 06:13:50 链接:http://www.cnblogs.com/xingshansi/p/6597796.html 声明:欢迎被转载,不过记得注明出处哦 ...
- Java完成简单猜数字游戏v2.0
猜数字游戏v2.0 优化了获取随机数.输入数据超出边界值的代码,并增加了异常处理,能够在玩家输入错误数据错误时给出可靠指引,希望对和我一样的新人有帮助, 最后希望有大神愿意帮我解决代码优化的问题,谢谢 ...
- vue组件递归
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- IOS各种手势操作实例
先看下效果 手势相关的介绍 IOS中手势操作一般是 UIGestureRecognizer 类的几个手势子类去实现,一般我们用到的手势就这么5种: 1.点击 UITapGestureRecogniz ...
- sass ruby环境 安装配置,使用sublime text3 中sass
首先,你想要使用sass的话,就必须依赖于ruby环境.所以,你要下一个ruby.具体的链接应该是(http://rubyinstaller.org/downloads).下载相应的版本.- 下载好之 ...