code forces 996D Suit and Tie
2 seconds
256 megabytes
standard input
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.
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 a single integer, representing the minimum number of adjacent swaps needed to line the people up so that each pair occupies adjacent positions.
4
1 1 2 3 3 2 4 4
2
3
1 1 2 2 3 3
0
3
3 1 2 3 1 2
3
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的更多相关文章
- 思维题--code forces round# 551 div.2
思维题--code forces round# 551 div.2 题目 D. Serval and Rooted Tree time limit per test 2 seconds memory ...
- CF995B Suit and Tie 贪心 第十三
Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Code Forces 796C Bank Hacking(贪心)
Code Forces 796C Bank Hacking 题目大意 给一棵树,有\(n\)个点,\(n-1\)条边,现在让你决策出一个点作为起点,去掉这个点,然后这个点连接的所有点权值+=1,然后再 ...
- Code Forces 833 A The Meaningless Game(思维,数学)
Code Forces 833 A The Meaningless Game 题目大意 有两个人玩游戏,每轮给出一个自然数k,赢得人乘k^2,输得人乘k,给出最后两个人的分数,问两个人能否达到这个分数 ...
- Code Forces 543A Writing Code
题目描述 Programmers working on a large project have just received a task to write exactly mm lines of c ...
- code forces 994B
B. Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes inp ...
- 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 ...
- code forces 382 D Taxes(数论--哥德巴赫猜想)
Taxes time limit per test 2 seconds memory limit per test 256 megabytes input standard input output ...
- code forces Watermelon
/* * Watermelon.cpp * * Created on: 2013-10-8 * Author: wangzhu */ /** * 若n是偶数,且大于2,则输出YES, * 否则输出NO ...
随机推荐
- [Ljava.lang.String; cannot be cast to java.lang.String报错的原因
完整错误信息: java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String 报这个错的原因 ...
- 【Kaggle】泰坦尼克号
引言 Kaggle官方网站 这是泰坦尼克号事件的基本介绍: 我们需要做的就是通过给出的数据集,通过对特征值的分析以及运用机器学习模型,分析什么样的人最可能存活,并给出对测试集合的预测. 对于Kaggl ...
- GItHub 建立仓库克隆仓库
Linux环境 建立本地仓库 mkdir git cd git git init 获取仓库地址 找到你的仓库,Clone or download,复制 克隆仓库到本地 git clone https: ...
- MySQL之查询性能优化(一)
为什么查询速度会慢 通常来说,查询的生命周期大致可以按照顺序来看:从客户端,到服务器,然后在服务器上进行解析,生成执行计划,执行,并返回结果给客户端.其中“执行”可以认为是整个生命周期中最重要的阶段, ...
- 容器技术的落地还要依靠SDN
容器能够实现新应用程序的快速部署,代表着目前IT开发社区的最热门趋势之一.然而,想要实现容器部署生产环境,IT人员还需要使用SDN技术,在分布式微应用程序之间实现可扩展.可管理且安全的通信. 什么是容 ...
- 树莓派网线连接后通过ssh远程连接
新安装的树莓派默认是不支持ssh远程连接的,通过PuTTY (64-bit)会提示连接被拒绝. 本文支持笔记本或pc直接通过网线连接树莓派,具体操作步骤如下: 一.确定树莓派的ip地址及是否开启ssh ...
- javascript类式继承模式#4——共享原型
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- nginx清除反向代理缓存
nginx重启无法清除反向代理的缓存,可以清空安装目录下的proxy_cache文件夹里的内容来清除.
- 抓取网站访问者的QQ号码
开源,是一种精神.但不开源,并不是没有精神,而可能是代码写得惨不忍睹,我属于后者.(首先申明:对代码提出意见可接受,虚心接受,但不能人身攻击啊!) 最近闲的蛋疼,喜欢到处看看做得好的站点, 莫 ...
- windows 10的资源管理器不显示映射的网络驱动器怎么办?
最近在使用映射网络驱动器的时候出现一个奇怪的现象.事情源于我在资源管理器里面映射了来自多个不同账号的网络驱动器.使用的是win10系统.映射不同账号的网络驱动器是不允许的.于是只能删掉其他账号和凭证重 ...