Codeforces 691D Swaps in Permutation】的更多相关文章

Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 691D Description You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a…
题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交换X Y位置上的数字,求可以得到的最大字典序的数列. 题目思路: [搜索][并查集] 这题可以用搜索或者并查集写,都能过. 把位置分成若干块,每一块里面的位置都是可以被这一块里另一个位置经过若干次调换的(类似强连通,位置可达). 然后把每一块位置里的 位置按从小到大排序,位置上的值按从大到小排序,依次填入位置…
这个题刚开始我以为是每个交换只能用一次,然后一共m次操作 结果这个题的意思是操作数目不限,每个交换也可以无限次 所以可以交换的两个位置连边,只要两个位置连通,就可以呼唤 然后连通块内排序就好了 #include <vector> #include <iostream> #include <queue> #include <cmath> #include <map> #include <cstring> #include <alg…
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a pair from the given positions and swap the numbers in that…
题目链接: D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you ca…
D. Swaps in Permutation 题目连接: http://www.codeforces.com/contest/691/problem/D Description You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a pair from the given positions and swap…
Swaps in Permutation You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choose a pair from the given positions and swap the numbers in that positions. What is the lexicographically maximal…
D. Swaps in Permutation time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output You are given a permutation of the numbers 1, 2, ..., n and m pairs of positions (aj, bj). At each step you can choo…
题目链接:http://codeforces.com/problemset/problem/691/D 给你n个数,各不相同,范围是1到n.然后是m行数a和b,表示下标为a的数和下标为b的数可以交换无数次.问你最后字典序最大的数列是什么. 将下面的a和b用并查集联系起来存到祖节点对应的数组中,然后从大到小排序数组,最后依次按照父节点的数组中的顺序输出. 也可以用dfs的方法解(略麻烦),形成一个环路的就在一个数组中... //并查集 #include <bits/stdc++.h> using…
题目链接:http://codeforces.com/contest/691/problem/D 题意: 题目给出一段序列,和m条关系,你可以无限次互相交换这m条关系 ,问这条序列字典序最大可以为多少 思路: 并查集维护这m条关系,用个vector存一下当前点所能到达的点,拍下序大的优先,输出的时候输出当前点所能找到的最大的数,已输出的标记下就好了. 实现代码: #include<bits/stdc++.h> using namespace std; ; int f[M],vis[M],a[M…
题目链接 分析:一些边把各个节点连接成了一颗颗树.因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置. 我用了极为暴力的方法,先dfs每棵树,再用了优先队列.我估计最大复杂度约在,理论上应该跑不过.因为再cf上做题,看见5s时限,强行上了.很侥幸,在4秒的时候过了= =. /*****************************************************/ //#pragma comment(linker, "/ST…
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty…
B. New Year Permutation time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output User ainta has a permutation p1, p2, ..., pn. As the New Year is coming, he wants to make his permutation as pretty…
题目链接:http://codeforces.com/problemset/problem/500/B 题目意思:给出一个含有 n 个数的排列:p1, p2, ..., pn-1, pn.紧接着是一个 n * n 的矩阵A,当且仅当 Aij = 1 时,pi 与 pj 可以交换数值.现在问如何交换数值,使得最后得到的排列字典序最小. 比赛的时候不会做,看了Tutorial 1 的解法,觉得别人做得太巧妙了,出题者也出得很好 ^_^ 可以看这个:http://codeforces.com/blog…
题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排列. 先说明什么时候得不到这样的一个排列,就是n = k的情况.因为任何一个数x放在第1个位置的gcd(x, 1) = 1的,所以要得出这样一个排列 k 最大只能为 n-1 .而k最小为0个,这个排…
Description Ivan has an array consisting of n elements. Each of the elements is an integer from 1 to n. Recently Ivan learned about permutations and their lexicographical order. Now he wants to change (replace) minimum number of elements in his array…
题目链接:http://codeforces.com/problemset/problem/785/E 其实可以CDQ分治... 我们只要用一个数据结构支持单点修改,区间查询比一个数大(小)的数字有多少个就可以了. 考虑分块,每段区间之内有排序或者二分查询比一个数大的树的个数的操作. 复杂度${O(qn \sqrt n log_{2}^{\sqrt n})}$ #include<iostream> #include<cstdio> #include<algorithm>…
[题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出交换顺序. [题解] 我们发现对于四个一组组内进行六次交换之后可以保证四个数的位置不变, 而对于每组相互之间可以一共进行十六次交换使得两组均不会发生变化 所以如果n能被4整除,那么我们可以4个分组达到目的, 当不能被4整除的时候,我们发现余数为2和3的时候都不能构造出可行解. 在余数为1的时候有一种…
[题目链接] http://codeforces.com/contest/785/problem/E [题目大意] 一个1到n顺序排列的数列,每次选择两个位置的数进行交换,求交换后的数列的逆序对数 [题解] 我们将序列进行分块.每块维护一个权值树状数组表示各个数字的情况, 每次交换两个数之后我们计算前后贡献值进行加减,计算答案. [代码] #include <cstdio> #include <algorithm> using namespace std; const int N=…
题意:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这个排列上的数与它所在位置的序号的最大公约数满足 > 1,并且这些数的个数恰好满足k个,输出这样的一个排列. 思路:只需要后k个数与下标一样,前n-k个数逆序输出就复合题意. #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<cstdlib&g…
题目链接 假设始终可以找到一种状态使得值为0, 那么两个1之间需要隔n-2个数, 两个2之间需要隔n-3个数, 两个3之间隔n-4个数. 我们发现两个三可以放到两个1之间, 同理两个5放到两个3之间....这样就构造好了. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmat…
只要找出当前没用过的数字中,大于或等于当前这一列的最大值就可以 #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<algorithm> using namespace std; ; int n; int x[maxn][maxn]; int ans[maxn]; int flag[maxn]; int main() { scanf("…
是一个简单构造题. 请观察公式: 绝对值里面的就是 |di-(n-i)|,即di与(n-i)的差值的绝对值. 事实上,对于任何n,我们都可以构造出来每一个i的di与(n-i)的差值为0. 换句话说,就是这个最小值一定可以构造出来是0. 假设输入是6:那么可以这样构造:1 3 5 5 3 1 2 4 6 4 2 6 假设输入是7:那么可以这样构造:1 3 5 7 5 3 1 2 4 6 6 4 2 7 从上面就能看出怎么构造了,n最后放空缺的两个位置,剩下的从小到大一个一个放,奇数放左边构造,偶数…
problem 题意 本题题意不太easy看懂. 给定一个序列,我们能够把这个序列变成一些循环置换的和.然而这样的置换的方法是不止一种的.我们定义一种standard cyclic representation,即每一个循环置换中最大的数都在第一个.把得到的循环置换的括号去掉.我们能够得到一个新的序列. 定义一个序列,使得它变成置换后再去掉括号得到的新序列和原序列同样,那么称这样的序列是稳定的.给定一个n(序列长度)和k.要求求出全部稳定序列按字典序排序后的第k大序列. 思路 首先我们能够证明.…
题意: 有一个\(1 \sim n\)的排列\(A\),有\(q\)个询问: 交换任意两个元素的位置,求交换之后排列的逆序数 分析: 像这种不太容易用线段树,树状数组维护的可以考虑分块 每\(\sqrt{n}\)个元素划分为一块,然后两端的块可以直接扫出逆序数的变化,中间的块可以用二分计算逆序数 在更新块的时候,可以二分查找要插入或删除的位置 每次询问的复杂度为\(O(\sqrt{n}log\sqrt{n})=O(\sqrt{n}logn)\) #include <cstdio> #inclu…
刷水题做几道入门贪心题预热... 找联通块里字典序最小的放到最前面即可.记得写传递闭包 #include <iostream> #include <cstdio> #include <cstring> using namespace std; const int N=350; int n,a[N],pos[N],g[N][N]; int main() { scanf("%d",&n); for(int i=1;i<=n;i++) sca…
题目大意: 给定一个n*m的矩阵 可以更改任意一个位置的值 也可以选择一整列全部往上移动一位,最上方的数移动到最下方 问最少操作多少次可以把这个矩阵移动成 1 2 3 ... m m+1 m+2 m+3 ... 2m ... (n-1)m+1 (n-1)m+2 (n-1)m+3 ... nm 解题思路: 如果一个数大于n*m,或者这个数不属于这一列((d-1)%m!=j)那么这个数只能进行改变值的操作存完后以列为单位分开求答案用cnt[i]记录如果这一列移动k次的话,有多少数的值不需要进行更改比…
A - Fashion in Berland 水 // #pragma comment(linker, "/STACK:102c000000,102c000000") #include <iostream> #include <cstdio> #include <cstring> #include <sstream> #include <string> #include <algorithm> #include &…
[codeforces 339]E. Three Swaps 试题描述 Xenia the horse breeder has n (n > 1) horses that stand in a row. Each horse has its own unique number. Initially, the i-th left horse has number i. That is, the sequence of numbers of horses in a row looks as foll…
Diverse Permutation Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 483C Description Permutationp is an ordered set of integers p1,   p2,   ...,   pn, consisting of n distinct positive i…