luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节
luogu 1327 数列排序
题意
给定一个数列\(\{an\}\),这个数列满足\(ai≠aj(i≠j)\),现在要求你把这个数列从小到大排序,每次允许你交换其中任意一对数,请问最少需要几次交换?
思路
找循环节。答案即为 (循环节的长度\(-1\)) 对所有循环节求和。
如果只能交换相邻两个,那么就是求逆序对个数。因为交换相邻两个数字的效果是使逆序对个数\(-1\).
Code
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;
typedef long long LL;
int a[maxn], b[maxn];
bool vis[maxn];
map<int, int> mp;
int main() {
int n;
scanf("%d", &n);
for (int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
b[i] = a[i];
mp[a[i]] = i;
}
sort(b, b+n);
int ans = 0;
for (int i = 0; i < n; ++i) {
if (vis[i]) continue;
int j = i;
while (!vis[j]) {
vis[j] = true;
j = mp[b[j]];
++ans;
}
--ans;
}
printf("%d\n", ans);
return 0;
}
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J.Minimum Distance in a Star Graph
题意
给出一个排列,问对其进行多少次操作能得到递增的排列。
一次操作指交换第\(i\)个位置上的数和第\(1\)个位置上的数。
思路
找循环节。
对于包含第一个数字的循环节,交换次数为 (循环节长度\(-1\));\(otherwise\),交换次数为 (循环节长度\(+1\)).
Code
#include <bits/stdc++.h>
using namespace std;
#define maxn 1010
int n;
char s[100], t[100];
int pos[maxn];
bool vis[maxn];
void work() {
char ch;
memset(vis, 0, sizeof(vis));
memset(pos, 0, sizeof(pos));
scanf("%s%s", s, t);
for (int i = 0; i < n; ++i) pos[s[i]] = i;
int temp = 0, ans = 0; vis[0] = true;
while (t[temp] != s[0]) {
temp = pos[t[temp]];
++ans;
vis[temp] = true;
}
for (int i = 1; i < n; ++i) {
if (!vis[i]) {
vis[i] = true;
int temp = i, len = 0;
while (t[temp] != s[i]) {
temp = pos[t[temp]];
++len;
vis[temp] = true;
}
if (len) ans += len + 2;
}
}
printf("%d\n", ans);
}
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < 5; ++i) work();
}
return 0;
}
luogu 1327 数列排序 & 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 J题 循环节的更多相关文章
- HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)
HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: ...
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem【状态压缩】
2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 M. Frequent Subsets Problem 题意:给定N和α还有M个U={1,2,3,...N}的子集,求子集X个数,X满足:X是U ...
- Skiing 2017 ACM-ICPC 亚洲区(乌鲁木齐赛区)网络赛H题(拓扑序求有向图最长路)
参考博客(感谢博主):http://blog.csdn.net/yo_bc/article/details/77917288 题意: 给定一个有向无环图,求该图的最长路. 思路: 由于是有向无环图,所 ...
- 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)
摘要 本文主要列举并求解了2016 ACM/ICPC亚洲区青岛站现场赛的部分真题,着重介绍了各个题目的解题思路,结合详细的AC代码,意在熟悉青岛赛区的出题策略,以备战2018青岛站现场赛. HDU 5 ...
- 2016 ACM/ICPC亚洲区大连站-重现赛 解题报告
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=5979 按AC顺序: I - Convex Time limit 1000 ms Memory li ...
- 2017 ACM/ICPC Asia 南宁区 L The Heaviest Non-decreasing Subsequence Problem
2017-09-24 20:15:22 writer:pprp 题目链接:https://nanti.jisuanke.com/t/17319 题意:给你一串数,给你一个处理方法,确定出这串数的权值, ...
- 2013 ACM/ICPC 长沙网络赛J题
题意:一个数列,给出这个数列中的某些位置的数,给出所有相邻的三个数字的和,数列头和尾处给出相邻两个数字的和.有若干次询问,每次问某一位置的数字的最大值. 分析:设数列为a1-an.首先通过相邻三个数字 ...
- [刷题]ACM/ICPC 2016北京赛站网络赛 第1题 第3题
第一次玩ACM...有点小紧张小兴奋.这题目好难啊,只是网赛就这么难...只把最简单的两题做出来了. 题目1: 代码: //#define _ACM_ #include<iostream> ...
- [刷题]ACM ICPC 2016北京赛站网络赛 D - Pick Your Players
Description You are the manager of a small soccer team. After seeing the shameless behavior of your ...
随机推荐
- Spring学习笔记之Spring概述
概述 Spring是一个java应用最广的开源框架,它是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Deve ...
- 03_6_package和import语句
03_6_package和import语句 1. package和import语句 为便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重命名空 ...
- wdcp 使用说明总结(持续更新中。。。)
wdcp 使用说明总结(持续更新中...) 1.移动文件时,如果是上一层,直接填写../即可
- jQuery获取动态添加的元素,live和on的区别
今天给大家说一下如果用jQuery获取动态添加的元素,通常如果你在网页上利用jQuery添加一个元素,那么用平常的jQuery获取元素的方法无效的获取不到的.可以用以下的方法获取动态元素!假设我们现在 ...
- LeetCode(307) Range Sum Query - Mutable
题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclus ...
- poj-1011 sticks(搜索题)
George took sticks of the same length and cut them randomly until all parts became at most 50 units ...
- private virtual in c++
source from http://blog.csdn.net/steedhorse/article/details/333664 // Test.cpp #include <iostream ...
- HDU 5371 Manacher Hotaru's problem
求出一个连续子序列,这个子序列由三部分ABC构成,其中AB是回文串,A和C相同,也就是BC也是回文串. 求这样一个最长的子序列. Manacher算法是在所有两个相邻数字之间插入一个特殊的数字,比如- ...
- virtual 三种用法
virtual用法一 #include using namespace std;class A{public: virtual void display(){ cout<<& ...
- LINQ to SQL和Entity Framework对比与关联
LINQ to SQL和Entity Framework都是一种包含LINQ功能的对象关系映射技术.他们之间的本质区别在于EF对数据库架构和我们查询的类型实行了更好的解耦.使用EF,我们查询的对象不再 ...