我是靠谱客的博主 清爽哈密瓜,这篇文章主要介绍Educational Codeforces Round 14 D. Swaps in Permutation,现在分享给大家,希望可以做个参考。

题目链接

分析:一些边把各个节点连接成了一颗颗树。因为每棵树上的边可以走任意次,所以不难想出要字典序最大,就是每棵树中数字大的放在树中节点编号比较小的位置。

我用了极为暴力的方法,先dfs每棵树,再用了优先队列。我估计最大复杂度约在O(Nlog(N)),理论上应该跑不过。因为再cf上做题,看见5s时限,强行上了。很侥幸,在4秒的时候过了= =。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*****************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <map> #include <set> #include <ctime> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define offcin ios::sync_with_stdio(false) #define sigma_size 26 #define lson l,m,v<<1 #define rson m+1,r,v<<1|1 #define slch v<<1 #define srch v<<1|1 #define sgetmid int m = (l+r)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define bits(a) __builtin_popcount(a) #define mk make_pair #define pb push_back #define fi first #define se second const int INF = 0x3f3f3f3f; const LL INFF = 1e18; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const int maxmat = 10; const ull BASE = 31; /*****************************************************/ const int maxn = 1e6 + 5; std::vector<int> G[maxn]; int N, M; int p[maxn]; bool vis[maxn]; struct Node { int val; bool operator <(const Node &rhs) const { return val > rhs.val; } }; priority_queue<Node> q; priority_queue<int> ans; void dfs(int u, int fa) { if (vis[u]) return; vis[u] = true; ans.push(p[u]); q.push((Node){u}); for (int i = 0; i < G[u].size(); i ++) { int v = G[u][i]; if (v == fa) continue; dfs(v, u); } } int main(int argc, char const *argv[]) { cin>>N>>M; mem(vis, false); for (int i = 1; i <= N; i ++) scanf("%d", p + i); for (int i = 0; i < M; i ++) { int u, v; scanf("%d%d", &u, &v); G[u].pb(v); G[v].pb(u); } for (int i = 1; i <= N; i ++) { if (!vis[i]) dfs(i, -1); while (!ans.empty()) { int tmp = ans.top(); ans.pop(); Node x = q.top(); q.pop(); int id = x.val; p[id] = tmp; } } for (int i = 1; i <= N; i ++) cout<<p[i]<<" "; return 0; }

正解没有那么暴力啊。正解是通过并查集来区分树。因为并查集的原因,遍历点的时候一定是从小到大的,省去了对位置的排序,复杂度又降了一个常数。这里只需要对各个树中的值排序就行了。复杂度比我的方法小了太多。用vector就可以了。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*****************************************************/ //#pragma comment(linker, "/STACK:1024000000,1024000000") #include <map> #include <set> #include <ctime> #include <stack> #include <queue> #include <cmath> #include <string> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <sstream> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define offcin ios::sync_with_stdio(false) #define sigma_size 26 #define lson l,m,v<<1 #define rson m+1,r,v<<1|1 #define slch v<<1 #define srch v<<1|1 #define sgetmid int m = (l+r)>>1 #define LL long long #define ull unsigned long long #define mem(x,v) memset(x,v,sizeof(x)) #define lowbit(x) (x&-x) #define bits(a) __builtin_popcount(a) #define mk make_pair #define pb push_back #define fi first #define se second const int INF = 0x3f3f3f3f; const LL INFF = 1e18; const double pi = acos(-1.0); const double inf = 1e18; const double eps = 1e-9; const LL mod = 1e9+7; const int maxmat = 10; const ull BASE = 31; /*****************************************************/ const int maxn = 1e6 + 5; int par[maxn], p[maxn]; std::vector<int> pos[maxn]; std::vector<int> num[maxn]; int N, M; void init() { for (int i = 1; i <= N; i ++) par[i] = i; } int findpar(int x) { return par[x] = (par[x] == x ? x : findpar(par[x])); } void unite(int x, int y) { x = findpar(x), y = findpar(y); if (x == y) return; par[x] = y; } int main(int argc, char const *argv[]) { cin>>N>>M; init(); for (int i = 1; i <= N; i ++) scanf("%d", p + i); for (int i = 0; i < M; i ++) { int u, v; scanf("%d%d", &u, &v); unite(u, v); } for (int i = 1; i <= N; i ++) { int x = findpar(i); pos[x].pb(i); num[x].pb(-p[i]); } for (int i = 1; i <= N; i ++) { sort(num[i].begin(), num[i].end()); for (int j = 0; j < pos[i].size(); j ++) { int id = pos[i][j]; p[id] = -num[i][j]; } } for (int i = 1; i <= N; i ++) cout<<p[i]<<" "; return 0; }

转载于:https://www.cnblogs.com/hahatianx/p/5943447.html

最后

以上就是清爽哈密瓜最近收集整理的关于Educational Codeforces Round 14 D. Swaps in Permutation的全部内容,更多相关Educational内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(73)

评论列表共有 0 条评论

立即
投稿
返回
顶部