首页 > 其他分享 >题解:CodeForces 843A Sorting by Subsequences[模拟/排序]

题解:CodeForces 843A Sorting by Subsequences[模拟/排序]

时间:2024-07-14 14:34:54浏览次数:8  
标签:std Sorting sequence int 题解 CodeForces subsequence input out

CodeForces 843A

A. Sorting by Subsequences

time limit per test: 1 second
memory limit per test: 256 megabytes
inputstandard input
outputstandard output

You are given a sequence \(a_1, a_2, ..., a_n\) 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 will be sorted in increasing order.

Sorting integers in a subsequence is a process such that the numbers included in a subsequence are ordered in increasing order, and the numbers which are not included in a subsequence don't change their places.

Every element of the sequence must appear in exactly one subsequence.

Input

The first line of input data contains integer \(n (1 ≤ n ≤ 10^5)\) — the length of the sequence.

The second line of input data contains n different integers \(a_1, a_2, ..., a_n (-10^9 ≤ a_i ≤ 10^9)\) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.

Output

In the first line print the maximum number of subsequences \(k\) , which the original sequence can be split into while fulfilling the requirements.

In the next \(k\) lines print the description of subsequences in the following format: the number of elements in subsequence \(c_i (0 < c_i ≤ n)\), then \(c_i\) integers \(l_1, l_2, ..., l_{c_i} (1 ≤ l_j ≤ n)\) — indices of these elements in the original sequence.

Indices could be printed in any order. Every index from \(1\) to \(n\) must appear in output exactly once.

If there are several possible answers, print any of them.

Examples

input 1

6
3 2 1 6 5 4

output 1

4
2 1 3
1 2
2 4 6
1 5

input 2

6
83 -75 -49 11 37 62

output 2

1
6 1 2 3 4 5 6

我的题解

想象一下,如果要把一个数替换成应该放在这个位置上的数的话,
那么替换之后另一个位置上的数不一定会对应,那么再进行替换
以此类推,会拉出一长串的数
这就是对这串数排序的最短的序列
在遍历的时候加一个计数
然后其他的就按顺序输出就好了

我的代码

#include <bits/stdc++.h>
#define int long long

int t,ans;
const int N = 1e5+10;
int a[N];
int b[N];

std::map<int,std::pair<int,std::vector<int> > > mp;
std::map<int,int> index;

void solve()
{
    int n;
    std::cin >> n;
    for(int i = 1 ; i <= n ; i ++)
    {
        std::cin >> a[i];
        b[i] = a[i];
        index[a[i]] = i;
    }

    std::sort(b+1,b+n+1);

    int i;

    for(i = 1 ; i <= n; i ++)
    {
        if(mp[i].first < 0) continue;
        else
        {
            std::vector<int> out;
            out.push_back(i);
            int t = i;
            mp[i].first++;
            while(b[t] != a[i])
            {
                t = index[b[t]];
                out.push_back(t);
                mp[t].first = -1;
            }

            std::sort(out.begin(),out.end());
            mp[i].second = out;

			ans ++;
        }
    }
	
	
	std::cout << ans << "\n";
	for(int i = 1 ; i <= n ; i ++)
	{
		if(mp[i].first!=-1)
		{
			std::cout << mp[i].second.size() << " ";
			for(int j = 0 ; j < mp[i].second.size() ; j ++)
				std::cout << mp[i].second[j] << " ";
			std::cout << "\n";
		}
	}
}

signed main()
{
    solve();
    return 0;
}



有什么出现纰漏的地方还请大家在评论区指出!谢谢!

标签:std,Sorting,sequence,int,题解,CodeForces,subsequence,input,out
From: https://www.cnblogs.com/jiejiejiang2004/p/18301514

相关文章

  • 题解:CodeForces 618C Constellation[贪心/模拟]
    CodeForces618CC.Constellationtimelimitpertest:2secondsmemorylimitpertest:256megabytesinputstandardinputoutputstandardoutputCatNokuhasobtainedamapofthenightsky.Onthismap,hefoundaconstellationwithnstarsnumberedfrom......
  • 题解:CodeForces 835 D Palindromic characteristics[区间dp/马拉车Manacher]
    CodeForces835DD.Palindromiccharacteristicstimelimitpertest:3secondsmemorylimitpertest:256megabytes*inputstandardinputoutputstandardoutputPalindromiccharacteristicsofstring\(s\)withlength\(|s|\)isasequenceof\(|s|\)in......
  • 题解:CodeForces 1019 A Elections[贪心/三分]
    CodeForces1019AA.Electionstimelimitpertest:2secondsmemorylimitpertest:256megabytesinput:standardinputoutput:standardoutputAsyouknow,majorityofstudentsandteachersofSummerInformaticsSchoolliveinBerlandforthemostparto......
  • [CF1941E] Rudolf and k Bridges 的题解
    题目大意在第\((i,j)\)个格子修建一个桥墩需要\(a_{i,j}+1\)的花费而且要求\((i,0)\)与\((i,m)\)必须修建桥墩并且桥墩之间的距离不得大于\(d\)。现在需要求见\(k\)个连续的桥,求最小代价。其中\(1\lek\len\le100,3\lem\le2\cdot10,1\led\lem\)。思路因为......
  • [ABC206E] Divide Both 的题解
    题目大意求出从\(l\)至\(r\)中满足以下条件的\((x,y)\)的个数。\(\gcd(x,y)\ne1\)且\(\gcd(x,y)\nex\)且\(\gcd(x,y)\ney\)。其中\(1\lel\ler\le10^6\)。思路正难则反,所以可以求出所有互质或者是相互倍数的\((x,y)\)的对数,在将其减去所有的方案数就是......
  • [CF1178D] Prime Graph 的题解
    题目大意构造一个简单无向图,是所有的有度的点的度都是质数而且总共的边的数量的个数是质数。思路因为需要让所有的入度都为质数,所以我们可以找到两个相邻的质数\(2,3\),因为这样即使增加了一条边那么这个节点的度也是质数。先将这个图构成一个巨大的环,接着如果所有的边数并不......
  • [CF1538F] Interesting Function 的题解
    题目大意给定两个正整数\(l,r\),将\(l\)不断加\(1\)直到\(l=r\),求出这一过程中\(l\)发生变化的位数总数。\(1\lel<r\le10^9\)。思路假设从\(l\)处理到\(r\)变化的次数为\(f(l,r)\)。因为直接求解出\(f(l,r)\)十分困难,所以可以通过求出\(f(0,l)\)......
  • 题解:CodeForces 1511 C Yet Another Card Deck[暴力/模拟]
    CodeForces1511CC.YetAnotherCardDeckDescriptionYouhaveacarddeckof\(n\)cards,numberedfromtoptobottom,i. e.thetopcardhasindex\(1\)andbottomcard —index\(n\).Eachcardhasitscolor:the\(i\)-thcardhascolor\(a_i\......
  • 「ABC217F」Make Pair 的题解
    题目大意一共\(2N\)个学生站成一排,其中有\(M\)对朋友关系。老师每次从队列中挑出两个相邻的学生作为同桌。为了关系和睦,每次选出的两个学生必须是朋友关系。选出的两个学生离开队列,空出来的位置左右合拢。请问老师有多少种方式选完所有学生?对于两种选人的方案,即使同桌关系相......
  • [COCI2015-2016#6] PAROVI 的题解
    题意选择一些\(n\)一下互质的二元组\(\{a,b\}\),求对于任意\(x\in\big[2,n\big]\)都不满足\(a,b<x\)和\(a,b\gex\)的个数。简化题意因为无解的情况只发生在所有的\(\{a,b\}\)之间没有多余的位置用于放置\(x\),所以题意可以抽象成这样:选择一些区间互质的区间\([a......