思路:用一个数组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的更多相关文章

  1. HDU 6044 - Limited Permutation | 2017 Multi-University Training Contest 1

    研究一下建树 : /* HDU 6044 - Limited Permutation [ 读入优化,笛卡尔树 ] | 2017 Multi-University Training Contest 1 ...

  2. HDU 6044 Limited Permutation(搜索+读入优化)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6044 [题目大意] 给出两个序列li,ri,现在要求构造排列p,使得对于区间[li,ri]来说, ...

  3. HDU 6044 Limited Permutation 读入挂+组合数学

    Limited Permutation Problem Description As to a permutation p1,p2,⋯,pn from 1 to n, it is uncomplica ...

  4. 2018中国大学生程序设计竞赛 - 网络选拔赛 hdu Tree and Permutation 找规律+求任意两点的最短路

    Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. 【HDU - 4345 】Permutation(DP)

    BUPT2017 wintertraining(15) #8F 题意 1到n的排列,经过几次置换(也是一个排列)回到原来的排列,就是循环了. 现在给n(<=1000),求循环周期的所有可能数. ...

  6. hdu 6044 : Limited Permutation (2017 多校第一场 1012) 【输入挂 组合数学】

    题目链接 参考博客: http://blog.csdn.net/jinglinxiao/article/details/76165353 http://blog.csdn.net/qq_3175920 ...

  7. HDU 5753 Permutation Bo (推导 or 打表找规律)

    Permutation Bo 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5753 Description There are two sequen ...

  8. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

  9. hdu 5225 Tom and permutation(回溯)

    题目链接:hdu 5225 Tom and permutation #include <cstdio> #include <cstring> #include <algo ...

随机推荐

  1. OC6_目录及文件的创建

    // // main.m // OC6_目录及文件的创建 // // Created by zhangxueming on 15/6/19. // Copyright (c) 2015年 zhangx ...

  2. (转)MSSQL 各个发行版本版本号以及Compact 版本号

    终于开始写博客了. 不要笑啊. 下面是MSSQL 的发行版本以及版本号.自己整理的. http://support.microsoft.com/kb/321185/zh-cn SQL Server 2 ...

  3. UvaLive7362 Fare(欧拉函数)

    题意:求1~n的素因子之和. 分析:欧拉函数 #include<cstdio> #include<cstring> #include<cctype> #includ ...

  4. 函数 sort,unique,stable_sort,count_if,谓词

    bool isShorter(const string &s1,const string &s2) { return s1.size() < s2.size(); } bool ...

  5. C++ 的隱式型別轉換

    先上一段代碼, 這段代碼竟然可以編譯過,我的老天! class Boo { Boo(int c){ cout << "I'm Boo"; } }; void do_so ...

  6. lex&yacc

    LEX: yytext 数组包含匹配模式的文本; 使词法分析程序工作的两条规则是:1. lex 模式只匹配输入字符或字符串一次.2. lex 执行当前输入的最长可能匹配的动作. 由 lex 产生的词法 ...

  7. 3月3日(2) Search Insert Position

    这题...有点简单吧,为什么只有34%的通过率? 题目意思简单说就是查找index,或者按升序插入的未知,WA一次,罪过,下次要特别注意程序里变量的变化,提交前用样例检查. 简单的我有点不好意思贴代码 ...

  8. iOS 非ARC基本内存管理系列 3-循环retain和@class

    1.@class 使用场景:对于循环依赖关系来说,比方A类引用B类,同时B类也引用A类: 可以看出Person和Card互相引用,此时如果使用#import编译报错!因此当使用@class在两个类中相 ...

  9. linux ubuntu 思源黑体安装

    下载地址: 全部:700多M  https://github.com/adobe-fonts/source-han-sans/releases/tag/1.001R 可选部分Github : http ...

  10. String.Format数字格式化输出 {0:N2} {0:D2} {0:C2

    //格式为sring输出 //   Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); //   Label2.Text = ...