D. Suit and Tie
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Allen is hosting a formal dinner party. 2n2n people come to the event in nn pairs (couples). After a night of fun, Allen wants to line everyone up for a final picture. The 2n2n people line up, but Allen doesn't like the ordering. Allen prefers if each pair occupies adjacent positions in the line, as this makes the picture more aesthetic.

Help Allen find the minimum number of swaps of adjacent positions he must perform to make it so that each couple occupies adjacent positions in the line.

Input

The first line contains a single integer nn (1≤n≤1001≤n≤100), the number of pairs of people.

The second line contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n. For each ii with 1≤i≤n1≤i≤n, ii appears exactly twice. If aj=ak=iaj=ak=i, that means that the jj-th and kk-th people in the line form a couple.

Output

Output a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.

Examples
input

Copy
4
1 1 2 3 3 2 4 4
output

Copy
2
input

Copy
3
1 1 2 2 3 3
output

Copy
0
input

Copy
3
3 1 2 3 1 2
output

Copy
3
Note

In the first sample case, we can transform 11233244→11232344→1122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244→11323244→1133224411233244→11323244→11332244 also works in the same number of steps.

The second sample case already satisfies the constraints; therefore we need 00 swaps.

题意  如何让一对一对匹配成功

1和1 匹配 2和2 匹配。。。(总感觉在虐狗)

只能两两交换位置移动

题解

从第一个开始找是否匹配,如果不匹配就从前往后找,找到后‘那一段’往后挪一个单位

代码如下

#include<bits/stdc++.h>
using namespace std;
int a[];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=;i<*n;i++){
scanf("%d",&a[i]);
}
int ans=;
int pos;
for(int i=;i<*n;i+=){
if(a[i]!=a[i-]){
int t=a[i];
for(int j=i+;j<*n;j++){
if(a[j]==a[i-]){
ans+=j-i;
pos=j;
a[i]=a[j];
break;
}
}
//这个就是那一段
for(int j=pos;j>i;j--){
a[j]=a[j-];
}
a[i+]=t; }
// for(int j=0;j<2*n;j++){
// printf("%d ",a[j]);
// }
// printf("\n"); }
printf("%d\n",ans);
}
return ;
}

code forces 996D Suit and Tie的更多相关文章

  1. 思维题--code forces round# 551 div.2

    思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...

  2. CF995B Suit and Tie 贪心 第十三

    Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  3. Code Forces 796C Bank Hacking(贪心)

    Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...

  4. Code Forces 833 A The Meaningless Game(思维,数学)

    Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...

  5. Code Forces 543A Writing Code

    题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...

  6. code forces 994B

    B. Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes inp ...

  7. code forces 383 Arpa's loud Owf and Mehrdad's evil plan(有向图最小环)

    Arpa's loud Owf and Mehrdad's evil plan time limit per test 1 second memory limit per test 256 megab ...

  8. code forces 382 D Taxes(数论--哥德巴赫猜想)

    Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...

  9. code forces Watermelon

    /* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...

随机推荐

  1. Unity基础

    unity unity 3大场景 Asset Scene Component Asset :资源导入导出 右击资源,选择导出Unity包 导入可以直接将只有复制到Asset文件夹 创建场景 File- ...

  2. yii2邮箱发送

    yii2 邮件发送  163邮箱 1.在配置文件main-local.php components=>[]里面配置 'mailer' => [ 'class' => 'yii\swi ...

  3. ECSHOP快递物流单号查询插件

    本ECSHOP快递物流单号跟踪插件提供国内外近2000家快递物流订单单号查询服务例如申通快递.顺丰快递.圆通快递.EMS快递.汇通快递.宅急送快递.德邦物流.百世快递.汇通快递.中通快递.天天快递等知 ...

  4. 【菜鸟笔记】记一次django无法正常在ie和edge浏览器渲染html页面

    如图所示,django无法渲染html显示成下载文件了 一步一步的从render ==>HttpResponse ==>HttpResponseBase 找到 即django文件夹下的ht ...

  5. 状压DP详解(位运算)

    前言: 状压DP是一种非常暴力的做法(有一些可以排除某些状态的除外),例如dp[S][v]中,S可以代表已经访问过的顶点的集合,v可以代表当前所在的顶点为v.S代表的就是一种状态(二进制表示),比如 ...

  6. 找回被丢弃怎么找都找不回来的git中的commit

    崩溃的一天,打算提代码走人,结果切分支之后,commit丢了= =,找了三个多小时 接下来分享下如何找回丢失的commit的 打开项目所在位置,打开git bash,在gitBASH中输入 git f ...

  7. B-树 分合之道

    P.s:在代码里会同时用到向量和B-树的search,insert, remove,具体调用的是哪个结构的函数结合上下文就能看懂. 根据上一篇文章,我们对于这棵树的大致结构已经明了,那该如何有效利用并 ...

  8. Ubuntu下配置LAMP + PhpStorm

    本文仅作为一个记录,以下配置在Ubuntu 14.10 64-bit上验证通过. 安装Apache 2:sudo apt-get install apache2 安装成功能够后,通过浏览器访问loca ...

  9. salt demo 环境

    demo 环境 安装 virtualBox和vagrant 安装工具包:virtualBox, vagrant 下载 https://github.com/UtahDave/salt-vagrant- ...

  10. CentOS 7 安装Oracle 11gR2

    概述 Oracle 在Linux和window上的安装不太一样,公司又是Linux系统上的Oracle,实在没辙,研究下Linux下Oracle的使用,oracle默认不支持CentOS系统安装,所以 ...