CF995B 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.
题意:给你2*n个数,问你把相同的数放在一起最小需要移动多少次位置
一个简单的贪心,找每次和自己相同的数的位置,中间已经被找到的数记为-1,不可访问。
然后加上每次找到的位置减去数自己的位置再减去中间已经被找到的数再减去一
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define debug(a) cout << #a << " " << a << endl
using namespace std;
const int maxn = 1e3 + ;
const int mod = 1e9 + ;
typedef long long ll;
int main(){
std::ios::sync_with_stdio(false);
ll n;
while( cin >> n ) {
ll a[maxn], sum = ;
for( ll i = ; i < *n; i ++ ) {
cin >> a[i];
}
for( ll i = ; i < *n; i ++ ) {
if( a[i] != - ) {
ll num = ;
for( ll j = i + ; j < *n; j ++ ) {
if( a[j] == - ) {
num ++;
}
if( a[j] == a[i] ) {
sum += ( j - i - num );
a[j] = -;
break;
}
}
}
}
cout << sum << endl;
}
return ;
}
CF995B Suit and Tie 贪心 第十三的更多相关文章
- code forces 996D Suit and Tie
D. Suit and Tie time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- CodeForces 995B Suit and Tie(贪心,暴力)
https://codeforces.com/problemset/problem/995/B 题意: 就是通过每次移动相邻的两位数,来使数值相同的数挨在一起,求最少要移动多少次. 思路: 直接从前往 ...
- [Codeforces Round #492 (Div. 1) ][B. Suit and Tie]
http://codeforces.com/problemset/problem/995/B 题目大意:给一个长度为2*n的序列,分别有2个1,2,3,...n,相邻的位置可以进行交换,求使所有相同的 ...
- CodeForces - 995B Suit and Tie
题面在这里! 明明可以出成n<=1e5但是因为拒绝写数据结构而只出到n<=100,,,出题人真的很棒棒.. 一个显然的贪心就是,把和当前序列最左端的数匹配的数移到它的右边,这样迭代下去总是 ...
- 【Codeforces】Codeforces Round #492 (Div. 2) (Contest 996)
题目 传送门:QWQ A:A - Hit the Lottery 分析: 大水题 模拟 代码: #include <bits/stdc++.h> using namespace std; ...
- 越狱Season 1-Season 1, Episode 3: Cell Test
Season 1, Episode 3: Cell Test -CO: Oh, my God. 我的天 Williamson, get in here. Williamson 快进来 What the ...
- Daily record-November
November 11. I managed to grab her hand. 我抓到了她的手.2. He passed a hand wearily over his eyes. 他疲倦地用手抹了 ...
- 从零开始单排学设计模式「装饰模式」黑铁 I
阅读本文大概需要 3.6 分钟. 本篇是设计模式系列的第四篇,虽然之前也写过相应的文章,但是因为种种原因后来断掉了,而且发现之前写的内容也很渣,不够系统. 所以现在打算重写,加上距离现在也有一段时间了 ...
- [Python设计模式] 第6章 衣服搭配系统——装饰模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格( ...
随机推荐
- Linux基础管道管理
一.I/O重定向 标准输入,标准输出,标准错误 file descriptors (FD, 文件描述符或Process I/O channels); 进程使用文件描述符来管理打开的文件 [root@l ...
- JavaFX 选择文件 导入Excel文件并解析
FXML 控制器 : @FXML public void selectExcel(MouseEvent event) { FileChooser fileChooser = new FileChoos ...
- Java VisualVM监控远程JVM
我们经常需要对我们的开发的软件做各种测试, 软件对系统资源的使用情况更是不可少, 目前有多个监控工具, 相比JProfiler对系统资源尤其是内存的消耗是非常庞大,JDK1.6开始自带的VisualV ...
- python 字符串格式化format
通过{}和:来代替传统%方式 1.位置参数 位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 >>> li ...
- Scala基础语法学习(一)
1. val和var的区别 val定义的是一个常量,无法改变其内容 scala> val s = 0 s: Int = 0 scala> s = 2 <console>:12: ...
- Java——集合类
1.容器的打印 import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import jav ...
- Java学习|HTTP请求头
https://www.cnblogs.com/honghong87/articles/6941436.html 常见http请求报文头属性 Accept:告诉服务端,客户端接受什么类型的响 ...
- 洛谷 P1903 [国家集训队]数颜色
题意简述 给定一个数列,支持两个操作 1.询问l~r有多少不同数字 2.修改某个数字 题解思路 带修莫队 如果修改多了,撤销修改 如果修改少了,进行修改 代码 #include <cmath&g ...
- 性能测试学习第三天-----loadrunner接口测试&中文乱码处理
loadrunner 接口测试: get.post(3种参数格式).cookie及token处理.加密接口.webservice.socket.文件上传接口.文件下载接口 & 中 ...
- C#使用NLOG System.TypeInitializationException,类型初始值设定项引发异常
C#如何使用NLOG,网上有很多介绍,本次使用时遇到一个问题,使用NLOG写日志时,出现初始化异常,基本异常信息如下: System.AggregateException: 发生一个或多个错误. -- ...