843A - Sorting by Subsequences

843A - Sorting by Subsequences

还好之前了解过一点白书上的permutation!

我写的递归,其实很容易直接写成递推啊

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+10;
 4
 5 int nex[maxn];
 6 struct Node{
 7     int id,x;
 8     bool operator<(const Node &a)const {
 9         return x<a.x;
10     }
11 }p[maxn];
12 int vis[maxn];
13 vector<int> v[maxn];
14 void dfs(int u){
15     vis[u]=1;
16     v[res].push_back(u);
17     if(!vis[nex[u]]) dfs(nex[u]);
18 }
19 int main(){
20     int n;
21     while(scanf("%d",&n)!=EOF){
22         for(int i=0;i<=n;i++) v[i].clear();
23         for(int i=1;i<=n;i++){
24             scanf("%d",&p[i].x);
25             p[i].id=i;
26         }
27         sort(p+1,p+n+1);
28         for(int i=1;i<=n;i++){
29             nex[i]=p[i].id;
30         }
31         memset(vis,0,sizeof(vis));
32         for(int i=1;i<=n;i++) if(!vis[i]){
33             dfs(i);
34         }
35         printf("%d\n",res);
36         for(int i=0;i<res;i++){
37             printf("%d",v[i].size());
38             for(int j=0;j<v[i].size();j++) printf(" %d",v[i][j]);
39             puts("");
40         }
41     }
42 }

tourist~~

 1 #include <bits/stdc++.h>
 2
 3 using namespace std;
 4
 5 const int N = 1234567;
 6
 7 pair <int, int> a[N];
 8 int p[N];
 9 bool was[N];
10
11 int main() {
12   int n;
13   scanf("%d", &n);
14   for (int i = 0; i < n; i++) {
15     scanf("%d", &a[i].first);
16     a[i].second = i;
17   }
18   sort(a, a + n);
19   for (int i = 0; i < n; i++) {
20     p[i] = a[i].second;
21   }
22   vector < vector <int> > res;
23   for (int i = 0; i < n; i++) {
24     if (was[i]) {
25       continue;
26     }
27     int x = i;
28     vector <int> cycle;
29     while (!was[x]) {
30       was[x] = true;
31       cycle.push_back(x);
32       x = p[x];
33     }
34     res.push_back(cycle);
35   }
36   int cnt = res.size();
37   printf("%d\n", cnt);
38   for (int i = 0; i < cnt; i++) {
39     int cc = res[i].size();
40     printf("%d", cc);
41     for (int j = 0; j < cc; j++) {
42       printf(" %d", res[i][j] + 1);
43     }
44     printf("\n");
45   }
46   return 0;
47 }

tourist

时间: 2024-10-28 20:30:24

843A - Sorting by Subsequences的相关文章

CF 843 A. Sorting by Subsequences

A. Sorting by Subsequences You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order,

CodeForces - 844C Sorting by Subsequences (排序+思维)

You are given a sequence a1,?a2,?...,?an consisting of different integers. It is required to split this sequence into the maximum number of subsequences such that after sorting integers in each of them in increasing order, the total sequence also wil

cf 843 A Sorting by Subsequences [建图]

题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边一个出边的一张图 那么在这张图里的一个环,就代表着一个满足条件的子序列 所以只要把图建出来以后,统计图中的每一个环就可以了 Code: 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #includ

AIM Tech Round 4 (Div. 2) A B C

A. Diversity 题意:给出一个字符串,和n,问最多改变多少个字符,使其不同字符最少为n 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=1e5+10; 5 6 map<char ,int >ma; 7 int main(){ 8 string s; 9 cin>>s; 10 int n; 11 cin>>n; 12 int

AIM Tech Round 4 (Div. 2)(A,暴力,B,组合数,C,STL+排序)

A. Diversity time limit per test:1 second memory limit per test:256 megabytes input:standard input output:standard output Calculate the minimum number of characters you need to change in the string s, so that it contains at least k different letters,

HDU 5122 K.Bro Sorting(模拟——思维题详解)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubble sort will compare each pair of adjacent items and swap them if they are in the wrong o

[LeetCode] Increasing Subsequences 递增子序列

Given an integer array, your task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2 . Example: Input: [4, 6, 7, 7] Output: [[4, 6], [4, 7], [4, 6, 7], [4

Distinct Subsequences

https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be non

LeetCode115 Distinct Subsequences

Given a string S and a string T, count the number of distinct subsequences of T in S. (Hard) A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the re