CF 843 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, 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.
The first line of input data contains integer n (1 ≤ n ≤ 105) — the length of the sequence.
The second line of input data contains n different integers a1, a2, ..., an ( - 109 ≤ ai ≤ 109) — the elements of the sequence. It is guaranteed that all elements of the sequence are distinct.
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 ci (0 < ci ≤ n), then ci integers l1, l2, ..., lci (1 ≤ lj ≤ 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.
6
3 2 1 6 5 4
4
2 1 3
1 2
2 4 6
1 5
6
83 -75 -49 11 37 62
1
6 1 2 3 4 5 6
In the first sample output:
After sorting the first subsequence we will get sequence 1 2 3 6 5 4.
Sorting the second subsequence changes nothing.
After sorting the third subsequence we will get sequence 1 2 3 4 5 6.
Sorting the last subsequence changes nothing.
把每一次交换涉及到的元素放到一个集合中。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <queue>
#include <stack>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <cassert>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>=y?x:y)
#define min(x,y) (x<=y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define ios() ios::sync_with_stdio(false)
#define INF 1044266558
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
int a[],b[],vis[];
int ans,n;
set<int>s;
set<int>::iterator it;
int main()
{
while(scanf("%d",&n)!=EOF)
{
fill(vis,vis+n,);
ans=;
for(int i=;i<n;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b,b+n);
for(int i=;i<n;i++) a[i]=lower_bound(b,b+n,a[i])-b;
for(int i=;i<n;i++)
{
if(!vis[i])
{
for(int j=a[i];!vis[j];j=a[j]) vis[j]=;
ans++;
}
}
printf("%d\n",ans);
for(int i=;i<n;i++)
{
if(vis[i])
{
s.clear();
for(int j=a[i];vis[j];j=a[j]) s.insert(j+),vis[j]=;
int pos=s.size();
printf("%d",pos);
for(it=s.begin();it!=s.end();it++)
printf(" %d",*it);
printf("\n");
}
}
}
return ;
}
CF 843 A. Sorting by Subsequences的更多相关文章
- cf 843 A Sorting by Subsequences [建图]
题面: 传送门 思路: 这道题乍一看有点难 但是实际上研究一番以后会发现,对于每一个位置只会有一个数要去那里,还有一个数要离开 那么只要把每个数和他将要去的那个位置连起来,构成了一个每个点只有一个入边 ...
- cf 843 D Dynamic Shortest Path [最短路+bfs]
题面: 传送门 思路: 真·动态最短路 但是因为每次只加1 所以可以每一次修改操作的时候使用距离分层的bfs,在O(n)的时间内解决修改 这里要用到一个小技巧: 把每条边(u,v)的边权表示为dis[ ...
- cf 843 B Interactive LowerBound [随机化]
题面: 传送门 思路: 这是一道交互题 比赛的时候我看到了直接跳过了...... 后来后面的题目卡住了就回来看这道题,发现其实比较水 实际上,从整个序列里面随机选1000个数出来询问,然后从里面找出比 ...
- 【AIM Tech Round 4 (Div. 2) C】Sorting by Subsequences
[链接]http://codeforces.com/contest/844/problem/C [题意] 水题,没有记录意义 [题解] 排序之后,记录每个数字原来在哪里就好. 可以形成环的. 环的个数 ...
- AIM Tech Round 4 (Div. 2)ABCD
A. Diversity time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- 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 o ...
- 【Codeforces AIM Tech Round 4 (Div. 2) C】
·将排序限制于子序列中,又可以说明什么呢? C. Sorting by Subsequences ·英文题,述大意: 输入一个长度为n的无重复元素的序列{a1,a2……an}(1<= ...
- CF 689D - Friends and Subsequences
689D - Friends and Subsequences 题意: 大致跟之前题目一样,用ST表维护a[]区间max,b[]区间min,找出多少对(l,r)使得maxa(l,r) == minb( ...
- CF#335 Sorting Railway Cars
Sorting Railway Cars time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- 高德地图和canvas画图结合应用(二)
上节讲述了如在在高德地图中添加canvas图层,这节就讲述下如何在canvas图层添加鼠标的事件. 在上节脚本的最后加入以下代码: var text; $('#container').on('clic ...
- tensorflow-gpu
1.Cuda 查看Cuda支持的GPU型号:https://developer.nvidia.com/cuda-gpus 下载cuda:https://developer.nvidia.com/cud ...
- NodeJS学习笔记 (25)逐行读取-readline(ok)
模块概览 readline是个非常实用的模块.如名字所示,主要用来实现逐行读取,比如读取用户输入,或者读取文件内容.常见使用场景有下面几种,本文会逐一举例说明. 文件逐行读取:比如说进行日志分析. 自 ...
- 洛谷 P2695 骑士的工作
P2695 骑士的工作 题目背景 你作为一个村的村长,保卫村庄是理所当然的了.今天,村庄里来了一只恶龙,他有n个头,恶龙到处杀人放火.你着急了.不过天无绝人之路,现在来了一个骑士团.里面有m位成员(往 ...
- Fragment的实际开发中总结(二)
在实际项目的开发过程Fragment的情况越来越多.大家肯定须要遇到过Fragment被销毁重建的情况. 结合自己在项目开发的一点总结和学习开源项目的代码.继续分享自己对Fragment的一点总结. ...
- Go 语言编程
[课程名称]Go 语言编程 [课程时间]2014年7月30日(周三) 20:50 - 22:00 [课程安排]20:50-21:00 通过邮件地址登录网络课堂 ...
- 大型网站架构之JAVA中间件
中间件就是在大型网站中,帮助各子模块间实现互相访问,消息共享或统一访问等功能的软件产品.常见的有: 远程服务框架中间件:主要解决各子模块之间互相访问的问题. 消息队列中间件:主要解决各子模之间消息共享 ...
- netsh http的使用
1.首先通过cmd进入 C:\Windows\System32\inetsrv>netshnetsh>http netsh http> 退出的时候,使用exit命令 2.显示监听的i ...
- string类自定义字符串替换函数replace
#include <iostream> #include <string> using namespace std; /* * 函数功能:将string字符串中的某些字符替换 ...
- jquery ajax中支持哪些返回类型以及js中判断一个类型常用的方法?
1 jquery ajax中支持哪些返回类型在JQuery中,AJAX有三种实现方式:$.ajax() , $.post , $.get(). 预期服务器返回的数据类型.如果不指定,jQuery 将自 ...