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. iPhone 横屏时默认会放大文字的问题

    有人说用 html { text-size-adjust: 100%; }我发现这个并不能解决问题.下面代码可以完美解决. 添加标签:<meta name="viewport" ...

  2. Mysql:case when then end 的用法

    0.创建一张数据表 表名为 test_when_case CREATE TABLE `test_when_case` ( `id` int(11) unsigned NOT NULL AUTO_INC ...

  3. [BZOJ2427][HAOI2010]软件安装(tarjan+树形DP)

    如果依赖关系出现环,那么对于一个环里的点,要么都选要么都不选, 所以每个环可以当成一个点,也就是强连通分量 然后就可以构造出一颗树,然后树形背包瞎搞一下就行了 注意要搞一个虚拟节点当根节点 Code ...

  4. 一行代码将两个列表拼接出第三个列表(两个可迭代对象相加产生第三个可迭代对象)--map()方法

    map()方法 map(func, *iterables) --> map object lambda方法: lambda  参数 :返回值 a = map(',7]) print(list(a ...

  5. Java石头剪刀布小游戏

    package com.neusoft.test; import java.awt.BorderLayout; import java.awt.Choice; import java.awt.Colo ...

  6. 资料--JavaScript原型链

    JavaScript原型链 原文出处:https://www.cnblogs.com/chengzp/p/prototype.html 目录 创建对象有几种方法 原型.构造函数.实例.原型链 inst ...

  7. 三层还是DDD,ORM还是Ado.Net,何去何从?

    我本想把这个问题放到博问去,前几次有去博问问过之类的问题,无奈大神们可能都不屑回答别人的低级问题.所以放到随笔里,一方面把自己对ORM.架构的一些看法写下来抛砖引玉,另一方面最主要的是想寻求大神们指指 ...

  8. 使用闭包和lambda解决问题与常规方式解决问题的对比。

    先来描述一下问题吧,游戏中的物品原来只有一个属性加成:攻击,防御,获得经验加成,金币加成,等等.现在要增加一个属性,这个属性可以为之前的属性之一. 这个属性加成涉及到类里的三个属性,value,typ ...

  9. thinkPHP 表单自动验证功能

    昨天晚上我们老大叫我弄表单自动验证功能,愁了半天借鉴了好多官网的知识,才出来,诶,总之分享一下我自己的成果吧! thinkphp 在Model基类为我们定义了自动验证的函数和正则表达式,我们只需要在对 ...

  10. eclipse把jar包引入项目的两种方法

    方法一: build path引入jar包 方法二: 把jar包放入lib文件夹 区别: 把jar包放入lib文件夹,以后把程序发给别人,别人可以直接运行而无需再自己添加jar包 总结: 1.有时即使 ...