hdu 4657 Find Permutation
思路:用一个数组index[]存放a的下标,初始化令a[i]=c[i]=index[i]=i;
假设当前处理的i,初始时令cur=i;j为大于i的任意值。每次操作找a[l]=c[cur]-b[cur]。
若cur==l则结束;否则交换a[l]和a[cur]的值以及下标。若l>i,那么结束;否则令cur=l,
然后交换c[cur]和c[j],继续处理知道该轮结束。
代码如下:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<ctime>
#include<cstring>
#include<vector>
#define ll __int64
#define pi acos(-1.0)
#define MAX 100005
#define mod 1000003
using namespace std;
int a[MAX],b[MAX],c[MAX],index[MAX];
int main(){
int t,n,i,j;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;i++) scanf("%d",&b[i]);
for(i=;i<n;i++) a[i]=c[i]=index[i]=i;
for(i=;i<n;i++){
int cur=i;
while((a[cur]+b[cur])%n!=c[cur]){
int l = index[(c[cur]-b[cur]+n)%n];
swap(a[l],a[cur]);
swap(index[a[l]],index[a[cur]]);
if(l>i) break;
cur=l;
swap(c[i+],c[cur]);
}
}
for(i=;i<n;i++){
printf("%d",a[i]);
if(i!=n-) printf(" ");
else printf("\n");
}
for(i=;i<n;i++){
printf("%d",c[i]);
if(i!=n-) printf(" ");
else printf("\n");
}
}
return ;
}
hdu 4657 Find Permutation的更多相关文章
- HDU 6044 - Limited Permutation | 2017 Multi-University Training Contest 1
研究一下建树 : /* HDU 6044 - Limited Permutation [ 读入优化,笛卡尔树 ] | 2017 Multi-University Training Contest 1 ...
- HDU 6044 Limited Permutation(搜索+读入优化)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6044 [题目大意] 给出两个序列li,ri,现在要求构造排列p,使得对于区间[li,ri]来说, ...
- HDU 6044 Limited Permutation 读入挂+组合数学
Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...
- 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路
Tree and Permutation Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 【HDU - 4345 】Permutation(DP)
BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...
- hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】
题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...
- HDU 5753 Permutation Bo (推导 or 打表找规律)
Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...
- HDU 5868 Different Circle Permutation(burnside 引理)
HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...
- hdu 5225 Tom and permutation(回溯)
题目链接:hdu 5225 Tom and permutation #include <cstdio> #include <cstring> #include <algo ...
随机推荐
- 在swift中使用MJRefresh
cocoapod导入的,并且桥接已经完成,但是就是不提示方法,醉了,
- jqGrid API 全
JQGrid是一个在jquery基础上做的一个表格控件,以ajax的方式和服务器端通信. JQGrid Demo 是一个在线的演示项目.在这里,可以知道jqgrid可以做什么事情. 下面是转自其他人b ...
- zz Release memory in Linux (Unused or Cached)
In computer science, Memory Leakage occurs when a computer software or program consumes required mem ...
- ArcSDE for oracle10g安装后post的时候出现错误
The Post Installation Setup can not locate required Oracle files in your path.Check your Oracle inst ...
- linux之Gcc使用
本篇文章摘自 http://blog.csdn.net/ljzcom/article/details/7213101 对于Gcc自带的很多命令行,只有具体编程里面用到了或者看到别人真真用到的,用的目的 ...
- lex&yacc8--wehter use in C++
bintree.h:12:1: error: unknown type name ‘using’ using namespace std; ============== bintree.h:28:1: ...
- Layout.xml中控件的ID命名方式
控件 缩写 LayoutView lv RelativeView rv TextView tv Button btn ImageButton imgBtn ImageView mgView 或则 iv ...
- 隐藏index.php - ThinkPHP完全开发手册 - 3.1
为了更好的实现SEO优化,我们需要隐藏URL地址中的index.php,由于不同的服务器环境配置方法区别较大,apache环境下面的配置我们可以参考5.9 URL重写来实现,就不再多说了,这里大概 ...
- 第五章 体验Qt Creator的神奇魅力
第五章 体验Qt Creator的神奇魅力 Qt Creator是官方提供的一个轻量级IDE(集成开发环境),功能强大,是程序员快速开发程序的有力助手.下面我们用它实现一个helloworld工程. ...
- 代码分享:php判断数组是否有序
发布:脚本学堂/PHP编程 编辑:JB02 2013-12-17 14:59:02 [大 中 小] 转自:http://www.jbxue.com/article/14723.html如何判断 ...