[Codeforces Round #492 (Div. 1) ][B. Suit and Tie]
http://codeforces.com/problemset/problem/995/B
题目大意:给一个长度为2*n的序列,分别有2个1,2,3,...n,相邻的位置可以进行交换,求使所有相同的数字相邻的最少交换次数.(n<100)
题目分析:n的数据范围特别小,所以只需要想到合理的(贪心)移动方式直接模拟即可,可以从左往右进行遍历,遍历到每个位置时,都把在这个位置之后且与这个数字相同的数字移动到它的旁边直接模拟即可(之所以正确是因为这样移动会使两个相同元素之间的元素都往后移动一次,由于是从左到右遍历,所以这些元素往后移动会减少以后移动的次数).
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
int qwq[],a[];
int main(){
//freopen("in.txt","r",stdin);
int n;
cin>>n;
for(int i=;i<*n;i++){
scanf("%d",&qwq[i]);
a[qwq[i]]=i;
}
int ans=;
for(int i=;i<*n;i++){
if(a[qwq[i]]>i+){
for(int j=i+;j<a[qwq[i]];j++){
if(a[qwq[j]]>a[qwq[i]])ans--;
}
ans+=a[qwq[i]]-i-;
}
}
cout << ans << endl;
return ;
}
[Codeforces Round #492 (Div. 1) ][B. Suit and Tie]的更多相关文章
- 【Codeforces】Codeforces Round #492 (Div. 2) (Contest 996)
题目 传送门:QWQ A:A - Hit the Lottery 分析: 大水题 模拟 代码: #include <bits/stdc++.h> using namespace std; ...
- Codeforces Round #492 (Div. 2)
A. /* 从大往小依次除 */ #include<cstdio> #include<algorithm> #include<cstring> #include&l ...
- Codeforces Round #492 (Div. 2) [Thanks, uDebug!]
这次的题好奇怪哦... C - Tesla 思路:先把跟停车位相邻的车停进去,然后开始转圈... #include<bits/stdc++.h> #define LL long long ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round #368 (Div. 2)
直达–>Codeforces Round #368 (Div. 2) A Brain’s Photos 给你一个NxM的矩阵,一个字母代表一种颜色,如果有”C”,”M”,”Y”三种中任意一种就输 ...
- cf之路,1,Codeforces Round #345 (Div. 2)
cf之路,1,Codeforces Round #345 (Div. 2) ps:昨天第一次参加cf比赛,比赛之前为了熟悉下cf比赛题目的难度.所以做了round#345连试试水的深浅..... ...
- Codeforces Round #279 (Div. 2) ABCDE
Codeforces Round #279 (Div. 2) 做得我都变绿了! Problems # Name A Team Olympiad standard input/outpu ...
- Codeforces Round #262 (Div. 2) 1003
Codeforces Round #262 (Div. 2) 1003 C. Present time limit per test 2 seconds memory limit per test 2 ...
随机推荐
- 如何搭一个vue项目
1.yarn global add @vue/cli (vue/cli是webpack的二次开发) 2.vue create 自定义项目名称 3.选择Manually select featu ...
- java 一些容易忽视的小点-类和对象
构造器 通过new关键字调用 构造器虽然有返回值,但是不能定义返回值类型(返回值的类型肯定是本类),不能在构造器里使用return返回某个值. 构造器是有权限的,也就是可以添加public,也可以添加 ...
- java将字符串根据空格进行分割,使用split方法
public class D { public static void main(String[] args) { String b = "Hello Java World"; S ...
- spoj220
题解: 后缀数组 把所有串连接起来 二分答案 代码: #include<cstdio> #include<cstring> #include<algorithm> ...
- Hibernate基础知识
Hibernate Hibernate的作用: 1. Hibernate解决ORM(对象关系映射)的问题,大大减少了持久层的代码量 2. hql方言,解决了可移植性问题 ...
- 深入研究sqlalchemy连接池
简介: 相对于最新的MySQL5.6,MariaDB在性能.功能.管理.NoSQL扩展方面包含了更丰富的特性.比如微秒的支持.线程池.子查询优化.组提交.进度报告等. 本文就主要探索MariaDB当中 ...
- C++面向对象实现封装线程池
body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...
- do while
do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次.因为它是先执行代码,后判断条件,如果条件为真,继续循环.
- L1-056 猜数字
一群人坐在一起,每人猜一个 100 以内的数,谁的数字最接近大家平均数的一半就赢.本题就要求你找出其中的赢家. 输入格式: 输入在第一行给出一个正整数N(≤104).随后 N 行,每行给出一个玩 ...
- 用MyEclipse开发REST Web Service
MyEclipse 在线订购年终抄底促销!火爆开抢>> MyEclipse最新版下载 使用MyEclipse开发RESTWeb服务来放大您的Web应用程序.在本教程示例中,您将创建一个简单 ...