CF844 C 置换 水
由于每个数字只出现一次,离散化一下,置换求个循环节就好了。
- /** @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 置换 水的更多相关文章
- GLSL实现Simple Displace Mapping 水仿真流体绘制 【转】
http://blog.csdn.net/a3070173/archive/2008/11/20/3342062.aspx Dislace Mapping其实就是在顶点着色器中 对顶点进行置换偏移,经 ...
- 2017年浙江理工大学程序设计竞赛校赛 题解&源码(A.水, D. 简单贪心 ,E.数论,I 暴力)
Problem A: 回文 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1719 Solved: 528 Description 小王想知道一个字 ...
- Atcoder 水题选做
为什么是水题选做呢?因为我只会水题啊 ( 为什么是$Atcoder$呢?因为暑假学长来讲课的时候讲了三件事:不要用洛谷,不要用dev-c++,不要用单步调试.$bzoj$太难了,$Topcoder$整 ...
- HDOJ 2317. Nasty Hacks 模拟水题
Nasty Hacks Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- Openjudge 1.13-21:最大质因子序列(每日两水)
总时间限制: 1000ms 内存限制: 65536kB 描述 任意输入两个正整数m, n (1 < m < n <= 5000),依次输出m到n之间每个数的最大质因子(包括m和n ...
- [LeetCode] LFU Cache 最近最不常用页面置换缓存器
Design and implement a data structure for Least Frequently Used (LFU) cache. It should support the f ...
- [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 如何装最多的水? — leetcode 11. Container With Most Water
炎炎夏日,还是呆在空调房里切切题吧. Container With Most Water,题意其实有点噱头,简化下就是,给一个数组,恩,就叫 height 吧,从中任选两项 i 和 j(i <= ...
随机推荐
- “我爱淘”第二冲刺阶段Scrum站立会议6
完成任务: 完成学院分类的点击查看书籍功能,可以点击书的条目查看书的详细信息. 计划任务: 将书的详细信息进行完善,并且可以点击收藏以及已预订等功能. 遇到问题: 分类功能,根据不同学院,自动将数据库 ...
- Struts2:Struts2在jsp中使用标签时值的获取
在OGNL的使用中,需要访问一系列的对象,这些对象放在OGNL的context中,context是一个Map结构,实际上它和ActionContext是相应的. 当用户发送请求时,struts会创建A ...
- 将 Spring 和 Hibernate 与 WebSphere Application Server 一起使用
本文摘要 如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSp ...
- css新增UI方案
一.文本新增样式 opacity 不透明度 h1{ margin: 100px auto; opacity: 0.5; } </style> </head> <body& ...
- 0325 实验一操作系统模拟cmd
实验一.命令解释程序的编写 专业:商软(2)班 姓名:韩麒麟 学号:201406114253 一. 实验目的 (1)掌握命令解释程序的原理: (2)掌握简单的DOS调用方法: (3)掌握C语言编程 ...
- (七)对Jmeter进行参数化的俩种方式
一.使用CSV Data Set Config: 1.添加CSV Data Set Config: 2.配置参数: Filename:文件名,指保存参数化数据的文件目录,可以相对或者绝对路径. Fil ...
- Spring注解原理
一.注解的基本概念和原理及其简单实用 注解(Annotation)提供了一种安全的类似注释的机制,为我们在代码中添加信息提供了一种形式化得方法,使我们可以在稍后某个时刻方便的使用这些数据(通过解析注解 ...
- 第201天:js---实现继承的5种方式
一.构造函数方式 //构造函数 function People(){ this.race = '汉族'; } People.prototype={ eat:function(){ console.lo ...
- git 恢复单个文件
首先查看该文件的历史版本信息:git log Default@2x.png 记录下需要恢复的commit版本号:如 9aa51d89799716aa68cff3f30c26f8815408e926 恢 ...
- CF398B Painting The Wall 概率期望
题意:有一个 $n * n$ 的网格,其中 $m$ 个格子上涂了色.每次随机选择一个格子涂色,允许重复涂,求让网格每一行每一列都至少有一个格子涂了色的操作次数期望.题解:,,这种一般都要倒推才行.设$ ...