博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
code forces 996D Suit and Tie
阅读量:5098 次
发布时间:2019-06-13

本文共 2545 字,大约阅读时间需要 8 分钟。

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 (1n1001≤n≤100), the number of pairs of people.

The second line contains 2n2n integers a1,a2,,a2na1,a2,…,a2n. For each ii with 1in1≤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 11233244112323441122334411233244→11232344→11223344 in two steps. Note that the sequence 11233244113232441133224411233244→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
using namespace std;int a[205];int main(){ int n; while(~scanf("%d",&n)){ for(int i=0;i<2*n;i++){ scanf("%d",&a[i]); } int ans=0; int pos; for(int i=1;i<2*n;i+=2){ if(a[i]!=a[i-1]){ int t=a[i]; for(int j=i+1;j<2*n;j++){ if(a[j]==a[i-1]){ ans+=j-i; pos=j; a[i]=a[j]; break; } } //这个就是那一段 for(int j=pos;j>i;j--){ a[j]=a[j-1]; } a[i+1]=t; }// for(int j=0;j<2*n;j++){// printf("%d ",a[j]);// }// printf("\n"); } printf("%d\n",ans); } return 0;}

 

转载于:https://www.cnblogs.com/buerdepepeqi/p/9226433.html

你可能感兴趣的文章
mysql-5.7.10-winx64 安装
查看>>
C#-WebService基础01
查看>>
C#-readonly与const不同
查看>>
启动外部exe程序
查看>>
Linux学习笔记(1)
查看>>
iOS判断某中类型是否来自系统NSFoundation
查看>>
CLR via C#深解笔记一 - CLR & C# 基础概念
查看>>
mysql 多表查询
查看>>
49. Group Anagrams
查看>>
OO第四单元博客作业
查看>>
GitHub 的两次故障分析
查看>>
关于android主线程异常NetworkOnMainThread不能訪问网络
查看>>
POJ 3210 : Coins
查看>>
px值转rem值的Sublime Text 3自己主动完毕插件
查看>>
使用GDI+位图数据扫描线处理图像的小技巧 from http://blog.csdn.net/maozefa/article/details/4533770...
查看>>
有 n个人围成一圈,顺序排号。从第一个人开始报数(从 1 到 3 报数),凡报到 3 的人退出圈子, 问最后留下的是原来第几号的那位...
查看>>
游戏编程之六 游戏编程的特点
查看>>
MSSQL之十九 视图
查看>>
模板引擎
查看>>
SQL Server——查看支持的数据类型
查看>>