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恤,垮裤,运动鞋)或白领风格( ...
随机推荐
- .NETCoreCSharp 中级篇2-3 Linq简介
.NETCoreCSharp 中级篇2-3 本节内容为Linq及其拓展方法.Linq中表达式树的使用 简介 语言集成查询(LINQ)是一系列直接将查询功能集成到C#语言的技术统称.数据查询历来都表示为 ...
- 【Java例题】5.3 线性表的使用
3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...
- 【Java例题】5.4 子串出现的次数
4. 输入一个字符串s,再输入另一个字符串t,在s中查找t出现的次数. package chapter5; import java.util.Scanner; public class demo4 { ...
- Element UI系列:Upload图片自定义上传
HTML部分代码 Javascript部分代码 CSS代码 样式部分可以自由调整 主要实现的原理是利用 http-request 的属性对上传进行自定义
- 浅谈 JavaScript 垃圾回收机制
github 获取更多资源 https://github.com/ChenMingK/WebKnowledges-Notes 在线阅读:https://www.kancloud.cn/chenmk/w ...
- Sqlserver 查询分组 记录
select b.* from (select a.*,row_number() over (partition by 列1 order by 列2 desc) rn from a) b ; --如需 ...
- Linux的crond和crontab
一.crond cron是一个linux下的定时执行工具(相当于windows下的scheduled task),可以在无需人工干预的情况下定时地运行任务task. 由于cron 是Linux的ser ...
- 06 css选择器
选择器的作用:选中标签 1.基本选择器 标签选择器 id选择器 class选择器 *通配符选择器 权重:行内样式 1000 > id选择器 100 > 类选择器10 > 标签选择器 ...
- reponse.addHeader中文名字乱码
- 企查查app (完结)
在经历前两次探索之后,终于可以把所有的加密关键参数搞定了. 已删除!!!! 好了现在基本结束了. 根据这个我写了一自动抓取企查查每日新增数据,需要的话可以去看看 企查查app新增企业数据抓取 关注小白 ...