洛谷 P1088 火星人 (全排列)
直接调用next_permutation即可,向前的话可以调用prev_permutation
#include<cstdio>
#include<cctype>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
const int MAXN = 11234;
int a[MAXN], n, m;
void read(int& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
int main()
{
read(n); read(m);
REP(i, 0, n) read(a[i]);
while(m--) next_permutation(a, a + n);
printf("%d", a[0]); REP(i, 1, n) printf(" %d", a[i]);
puts("");
return 0;
}
还可以不用STL做,原理见https://blog.csdn.net/HyJoker/article/details/50899362
#include<cstdio>
#include<cctype>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
#define _for(i, a, b) for(int i = (a); i <= (b); i++)
using namespace std;
const int MAXN = 11234;
int a[MAXN], n, m;
void read(int& x)
{
int f = 1; x = 0; char ch = getchar();
while(!isdigit(ch)) { if(ch == '-1') f = -1; ch = getchar(); }
while(isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); }
x *= f;
}
void next()
{
int j, k;
for(j = n - 2; j >= 0 && a[j] > a[j+1]; j--);
for(k = n - 1; k >= 0 && a[k] < a[j]; k--);
swap(a[k], a[j]);
reverse(a + j + 1, a + n);
}
int main()
{
read(n); read(m);
REP(i, 0, n) read(a[i]);
while(m--) next();
printf("%d", a[0]); REP(i, 1, n) printf(" %d", a[i]);
puts("");
return 0;
}
洛谷 P1088 火星人 (全排列)的更多相关文章
- 洛谷——P1088 火星人
P1088 火星人 题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常 ...
- 洛谷P1088——火星人(全排列+数学模拟)
题目描述 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法.这种交流方法是这样的,首先,火星人把一个非常大的数字告诉人类科学 ...
- 洛谷 P1088 火星人
https://www.luogu.org/problemnew/show/P1088 这个题一开始是很蒙的 感觉很麻烦,每次都要交换balabala..... 后来才知道有这么一个神奇的stl 真是 ...
- 洛谷P1088 火星人【STL】【思维】
题目:https://www.luogu.org/problemnew/show/P1088 题意: 给定一个n个数的排列,要求得到这之后的第m个排列. 思路: next_permutation的简单 ...
- 洛谷P1088 火星人 [STL]
题目传送门 火星人 格式难调,题面就不放了. 分析: 这道题目不得不又让人感叹,还是$STL$大法好!!! $C++$的$algorithm$库中自带有$next\_permutation()$和$p ...
- 洛谷P1088 火星人
//其实就是全排列 //我们从外星人给的那串数字往下搜索 //一直往下拓展m次 //最后输出结果 //虽然看起来很暴力,但是题目上说了m非常小 #include<bits/stdc++.h> ...
- P1088 火星人——全排列函数
P1088 火星人 algorithm里面有一个next_permutation(a,a+n); #include<cstdio> #include<cstring> #inc ...
- [洛谷U72177]火星人plus
题目大意:给你一个$1\sim n(n\leqslant 10^5)$的排列,设$a$为它在$1\sim n$的全排列中的排名,求在$1\sim n$的全排列中第$a+m$个排列. 题解:康托展开以及 ...
- 洛谷试炼场-简单数学问题-P1088 火星人
洛谷试炼场-简单数学问题 A--P1088 火星人 Description 人类终于登上了火星的土地并且见到了神秘的火星人.人类和火星人都无法理解对方的语言,但是我们的科学家发明了一种用数字交流的方法 ...
随机推荐
- maven的pom.xml配置json依赖
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</art ...
- html css 样式中100%width 仍有白边解决办法
把 <body >改成<body style="margin=0%">
- node——try-catch与异步操作
//try-catch,用于捕获异常 //try-catch在node中只能捕获同步的异常,不能捕获异步异常 var fs=require('fs'); /*fs.writeFile('./abc.t ...
- 计算机网络Intro
1. 计算机网络体系结构 1.1 简介 定义 计算机网络的各层 + 其协议的集合 作用 定义该计算机网络的所能完成的功能 1.2 结构介绍 计算机网络体系结构分为3种:OSI体系结构.TCP / IP ...
- Extjs获取input值的几种方法
记录一下: ExtJs获取文本框中值的几种方式 EXTHTML 1.Html文本框 如: 获取值的方式为: var tValue = Ext.getDom('test').value; 或者 var ...
- JavaScript中的常用的数组操作方法
JavaScript中的常用的数组操作方法 一.concat() concat() 方法用于连接两个或多个数组.该方法不会改变现有的数组,仅会返回被连接数组的一个副本. var arr1 = [1,2 ...
- [terry笔记]python购物程序
如下是一个购物程序: 先输入工资,显示商品列表,购买,quit退出,最后格式化输出所买的商品. count = 0 while True: #做一个循环判断,如果输入的不是数字,基于提示,三次后退出 ...
- UVALive 2664 One-way traffic
One-way traffic Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Or ...
- C/C++ 名正则言顺
本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532701 名称所表达的含义极其丰富 ...
- WCF与WEB API区别