题目链接: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的更多相关文章

  1. CSAcademy Beta Round #5 Force Graph

    题目链接:https://csacademy.com/contest/arhiva/#task/force_graph/ 大意是有若干个节点,每个节点对应一个二维坐标,节点之间相互有斥力存在.同时有些 ...

  2. CSAcademy Beta Round #5 Long Journey

    题目链接:https://csacademy.com/contest/arhiva/#task/long_journey/ 大意是有一张无向不带权的图,两个人同时从s点出发,分别前往a点和b点,且每个 ...

  3. CSAcademy Beta Round #3 a-game

    题目连接 a-game 大意:有一个只包含A和B的字符串,两个人分别取这个串的子串,但是每一次取不能与之前取到过的子串有交集,最后谁取到的所有串中A的总数量少的判为胜.如果一样,则为平手. 给出这样的 ...

  4. Codeforces Beta Round #69 (Div. 2 Only)

    Codeforces Beta Round #69 (Div. 2 Only) http://codeforces.com/contest/80 A #include<bits/stdc++.h ...

  5. Codeforces Beta Round #67 (Div. 2)

    Codeforces Beta Round #67 (Div. 2) http://codeforces.com/contest/75 A #include<bits/stdc++.h> ...

  6. Codeforces Beta Round #49 (Div. 2)

    Codeforces Beta Round #49 (Div. 2) http://codeforces.com/contest/53 A #include<bits/stdc++.h> ...

  7. Codeforces Beta Round #35 (Div. 2)

    Codeforces Beta Round #35 (Div. 2) http://codeforces.com/contest/35 A 这场的输入输出是到文件中的,不是标准的输入输出...没注意看 ...

  8. Codeforces Beta Round #32 (Div. 2, Codeforces format)

    Codeforces Beta Round #32 (Div. 2, Codeforces format) http://codeforces.com/contest/32 A #include< ...

  9. Codeforces Beta Round #31 (Div. 2, Codeforces format)

    Codeforces Beta Round #31 (Div. 2, Codeforces format) http://codeforces.com/contest/31 A #include< ...

随机推荐

  1. 黑苹果引导工具 Clover 配置详解及Clover Configurator使用

    黑苹果引导工具 Clover 配置详解及Clover Configurator使用  2017-03-11 14:01:40 by SemiconductorKING 转自:@三个表哥   简介: 可 ...

  2. Binary Tree Paths leetcode

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  3. python+request接口自动化框架

    python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...

  4. Django REST framework使用ViewSets的自定义路由实现过程

    在Django中使用基于类的视图(ClassView),类中所定义的方法名称与Http的请求方法相对应,才能基于路由将请求分发(dispatch)到ClassView中的方法进行处理,而Django ...

  5. KVC与KVO理解

    转载:https://magicalboy.com/kvc_and_kvo/ KVC 与 KVO 理解 KVC 与 KVO 是 Objective C 的关键概念,个人认为必须理解的东西,下面是实例讲 ...

  6. WPF实用小工具

    Kaxaml 一款轻量级的Xaml代码编辑器,提供了可视的效果可以看到修改代码后的实时效果图.个人习惯于用它测试系统默认控件的行为和布局,小片段的xaml也可以拿到这个工具上测试效果.这款工具还提供了 ...

  7. TensorFlow anaconda命令备忘

    [查看tensorflow安装的版本] anaconda search -t conda tensorflow [选择版本安装] conda install -c anaconda tensorflo ...

  8. HTTP相关整理(上)

    这次整理HTTP相关知识点的初衷是因为项目中有大量与网络请求相关的知识细节点,所以这次整理的更多的是日常中用得到的点(参考图解HTTP),另外给打算做FE的新人们一些建议:多重视网络这方面的知识.文章 ...

  9. 数字千分位处理,number.js,js保留两位,整数强制保留两位小数

    杨龙飞 杨龙飞 杨龙飞 杨龙飞 杨龙飞 杨龙飞 官方文档:https://www.customd.com/articles/14/jquery-number-format-redux 1.千分位 $. ...

  10. 简单实用的JQuery弹出层

    效果: 初始状态时滚动条是可以滚动的 弹出层出现之后:1.弹窗始终居于整个窗口的中间 2.滚动条不可滚动 实现代码: HTML代码: <div class="container&quo ...