由于每个数字只出现一次,离散化一下,置换求个循环节就好了。

/** @Date    : 2017-08-25 01:39:39
* @FileName: C.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair<int ,int>
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 2e5+20;
const double eps = 1e-8; int a[N];
int pos[N];
vector<int>q[N];
int vis[N];
int main()
{
int n;
while(cin >> n)
{
MMF(vis);
MMF(pos);
map<int,int>s;
for(int i = 1; i <= n; i++)
scanf("%d", a + i), pos[i] = a[i];
sort(a + 1, a + n + 1);
for(int i = 1; i <= n; i++)
s[a[i]] = i;
for(int i = 1; i <= n; i++)
a[i] = s[pos[i]]; int c = 0;
for(int i = 1; i <= n; i++)
{
if(!vis[a[i]] && a[i] != i)
{
int cnt = 2;
int np = a[i];
vis[i] = 1;
while(a[np] != i || !vis[i])
{
cnt++;
vis[np] = 1;
q[c].PB(np);
np = a[np];
}
vis[np] = 1;
q[c].PB(np);
q[c].PB(i);
c++;
}
else if(a[i] == i)
{
q[c++].PB(a[i]);
vis[i] = 1;
}
}
printf("%d\n", c);
for(int i = 0; i < c; i++)
{
printf("%d", q[i].size());
while(!q[i].empty())
{ printf(" %d", q[i].back());
q[i].pop_back();
}
printf("\n");
}
}
return 0;
}

CF844 C 置换 水的更多相关文章

  1. GLSL实现Simple Displace Mapping 水仿真流体绘制 【转】

    http://blog.csdn.net/a3070173/archive/2008/11/20/3342062.aspx Dislace Mapping其实就是在顶点着色器中 对顶点进行置换偏移,经 ...

  2. 2017年浙江理工大学程序设计竞赛校赛 题解&源码(A.水, D. 简单贪心 ,E.数论,I 暴力)

    Problem A: 回文 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 1719  Solved: 528 Description 小王想知道一个字 ...

  3. Atcoder 水题选做

    为什么是水题选做呢?因为我只会水题啊 ( 为什么是$Atcoder$呢?因为暑假学长来讲课的时候讲了三件事:不要用洛谷,不要用dev-c++,不要用单步调试.$bzoj$太难了,$Topcoder$整 ...

  4. HDOJ 2317. Nasty Hacks 模拟水题

    Nasty Hacks Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  5. Openjudge 1.13-21:最大质因子序列(每日两水)

    总时间限制:  1000ms 内存限制:  65536kB 描述 任意输入两个正整数m, n (1 < m < n <= 5000),依次输出m到n之间每个数的最大质因子(包括m和n ...

  6. [LeetCode] LFU Cache 最近最不常用页面置换缓存器

    Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...

  7. [LeetCode] Longest Repeating Character Replacement 最长重复字符置换

    Given a string that consists of only uppercase English letters, you can replace any letter in the st ...

  8. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

  9. 如何装最多的水? — leetcode 11. Container With Most Water

    炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...

随机推荐

  1. Python:列表操作总结

    一.创建一个列表 只要把逗号分隔的不同数据项使用方括号括起来即可 list1=['physics','chemistry',1997,2000] list2=[1,2,3,4,5,6,7] [注]:1 ...

  2. 【week10】psp

    项目 内容 开始时间 结束时间 中断时间 净时间 2016/11/19(星期六) 写博客 吉林一日游规格说明书 10:30 15:10 20 260 2016/11/20(星期日) 看论文 磷酸化+三 ...

  3. Spring配置声明

    <...     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"        xmlns:p="htt ...

  4. 淘宝免费ip地址查询导致服务堵死的坑

    1.业务中因为想根据用户ip来做一些友好的提示,所以在网上找了个免费的ip查询地址 http://ip.taobao.com/service/getIpInfo.php?ip= 虽然说淘宝的这个地址会 ...

  5. Spring Security 入门详解

    序:本文主要参考 spring实战 对里面的知识做一个梳理 1.Spring Security介绍 Spring Security是基于spring的应用程序提供声明式安全保护的安全性框架,它提供了完 ...

  6. 利用vs10和opencv识别图片类型身份证的号码

    遇到的问题: 1 持续灰色图像框 waitkey()要在imshow()之前调用. 2 CvRect 和Rect CvXXX是C语言的接口,cv::XXX是C++语言的接口.两者混在一起容易出错 3 ...

  7. BZOJ 1923 外星千足虫(bitset优化线性基)

    题意:给出m次n个千足虫的足数信息,确定在第几次测试后可以确定每个千足虫的来历. 我们可以观察到每个测试结果具有异或后依然成立的性质,于是实际上我们只需要从头到尾确定有n个线性相关的向量是在哪一个测试 ...

  8. 【Java】list转换json的中文乱码问题

    添加如图红框内容

  9. Splay 的区间操作

    学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...

  10. python实现RSA加解密

    # coding=utf-8 """ @author:Eleven created on:2018年10月30日 """ import bi ...