POJ1833 & POJ3187 & POJ3785 next_permutation应用
要是没有next_permutation这个函数,这些题认为还不算特别水,只是也不一定,那样可能就会有对应的模板了。
反正正是由于next_permutation这个函数。这些题包含之前的POJ1226,都变得简单起来。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 17486 | Accepted: 6970 |
Description
大家知道,给出正整数n,则1到n这n个数能够构成n。种排列,把这些排列依照从小到大的顺序(字典顺序)列出,如n=3时,列出1 2 3,1 3 2,2 1 3,2 3 1,3 1 2,3 2 1六个排列。
任务描写叙述:
给出某个排列。求出这个排列的下k个排列,假设遇到最后一个排列,则下1排列为第1个排列,即排列1 2 3…n。
比方:n = 3,k=2 给出排列2 3 1,则它的下1个排列为3 1 2,下2个排列为3 2 1,因此答案为3 2 1。
Input
Output
Sample Input
3
3 1
2 3 1
3 1
3 2 1
10 2
1 2 3 4 5 6 7 8 9 10
Sample Output
3 1 2
1 2 3
1 2 3 4 5 6 7 9 8 10
直接用next_permutation这个函数就可以。
代码:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
using namespace std; int num[1030]; int main()
{
int Test,N,Q,i;
cin>>Test;
while(Test--)
{
scanf_s("%d%d",&N,&Q);
for(i=0;i<N;i++)
scanf_s("%d",&num[i]);
for(i=1;i<=Q;i++)
next_permutation(num,num+N);
for(i=0;i<N;i++)
printf("%d ",num[i]);
printf("\n");
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5072 | Accepted: 2923 |
Description
one instance of the game (when N=4) might go like this:
3 1 2 4 4 3 6 7 9 16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities.
Write a program to help FJ play the game and keep up with the cows.
Input
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
There are other possible sequences, such as 3 2 1 4, but 3 1 2 4 is the lexicographically smallest.
题意是要输出N个数,这N个数是从1到N这些数的一个顺序。这种顺序依照杨辉三角的模式相加起来等于sum,输出相等时的第一个字典顺序。
一看到N是大于1小于10的我就想暴力了。。
。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int main()
{
int i,n,sum,a[12];
cin>>n>>sum; for(i=1;i<=10;i++)
a[i]=i;
if(n==1)
{
cout<<1<<endl;
}
else if(n==2)
{
cout<<1<<" "<<2<<endl;
}
else if(n==3)
{
while(1*a[1]+2*a[2]+1*a[3]!=sum)
{
next_permutation(a+1,a+3+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<endl;
}
else if(n==4)
{
while(1*a[1]+3*a[2]+3*a[3]+1*a[4]!=sum)
{
next_permutation(a+1,a+4+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<endl;
}
else if(n==5)
{
while(1*a[1]+4*a[2]+6*a[3]+4*a[4]+1*a[5]!=sum)
{
next_permutation(a+1,a+5+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<endl;
}
else if(n==6)
{
while(1*a[1]+5*a[2]+10*a[3]+10*a[4]+5*a[5]+1*a[6]!=sum)
{
next_permutation(a+1,a+n+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<endl;
}
else if(n==7)
{
while(1*a[1]+6*a[2]+15*a[3]+20*a[4]+15*a[5]+6*a[6]+1*a[7]!=sum)
{
next_permutation(a+1,a+n+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<endl;
}
else if(n==8)
{
while(1*a[1]+7*a[2]+21*a[3]+35*a[4]+35*a[5]+21*a[6]+7*a[7]+1*a[8]!=sum)
{
next_permutation(a+1,a+n+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<endl;
}
else if(n==9)
{
while(1*a[1]+8*a[2]+28*a[3]+56*a[4]+70*a[5]+56*a[6]+28*a[7]+8*a[8]+1*a[9]!=sum)
{
next_permutation(a+1,a+n+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<endl;
}
else if(n==10)
{
while(1*a[1]+9*a[2]+36*a[3]+84*a[4]+126*a[5]+126*a[6]+84*a[7]+36*a[8]+9*a[9]+1*a[10]!=sum)
{
next_permutation(a+1,a+n+1);
}
cout<<a[1]<<" "<<a[2]<<" "<<a[3]<<" "<<a[4]<<" "<<a[5]<<" "<<a[6]<<" "<<a[7]<<" "<<a[8]<<" "<<a[9]<<" "<<a[10]<<endl;
} return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 979 | Accepted: 717 |
Description
123 -> 132
279134399742 -> 279134423799
It is possible that no permutation of the input digits has a larger value. For example, 987.
Input
value.
Output
set number, a single space and the next larger permutation of the input digits.
Sample Input
3
1 123
2 279134399742
3 987
Sample Output
1 132
2 279134423799
3 BIGGEST
还是直接使用next_permutation。这个函数是有返回值的,返回值是0时表示已经没有下一个字典顺序了,它要变成第一个字典顺序。返回值是1时表示还有字典顺序的下一个顺序,所以利用函数的这个性质就OK了。
代码:
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std; int main()
{
int Test,num;
char s[100]; cin>>Test;
while(Test--)
{
cin>>num>>s;
cout<<num<<" "; int n=next_permutation(s,s+strlen(s)); if(n==0)
cout<<"BIGGEST"<<endl;
else
cout<<s<<endl;
}
return 0;
}
POJ1833 & POJ3187 & POJ3785 next_permutation应用的更多相关文章
- POJ1833 & POJ3187 & POJ3785
要是没有next_permutation这个函数,这些题觉得还不算特别水,不过也不一定,那样可能就会有相应的模板了.反正正是因为next_permutation这个函数,这些题包括之前的POJ1226 ...
- 《挑战程序设计竞赛》2.1 穷竭搜索 POJ2718 POJ3187 POJ3050 AOJ0525
POJ2718 Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6509 Acce ...
- POJ-3187 Backward Digit Sums---枚举全排列
题目链接: https://vjudge.net/problem/POJ-3187 题目大意: 输入n,sum,求1~n的数,如何排列之后,相邻两列相加,直到得出最后的结果等于sum,输出1~n的排列 ...
- STL::next_permutation();
next_permutation()可以按字典序生成所给区间的全排列. 在STL中,除了next_permutation()外,还有一个函数prev_permutation(),两者都是用来计算排列组 ...
- 关于全排列 next_permutation() 函数的用法
这是一个c++函数,包含在头文件<algorithm>里面,下面是基本格式. 1 int a[]; 2 do{ 3 4 }while(next_permutation(a,a+n)); 下 ...
- About next_permutation
哈哈没错这个又是我们C++党的语言优势之一,用这个函数可以求当前排序的下一个排序,也就是说可以方便的求全排列,用这个函数需要用到algorithm这个头文件. 与这个函数相反的是prev_permut ...
- (DFS、全排列)POJ-3187 Backward Digit Sums
题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难 ...
- STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置, 直接求到第一个不按升序排列的序列. #include <iostream> #include & ...
- 【STL】next_permutation的原理和使用
1.碰到next_permutation(permutation:序列的意思) 今天在TC上碰到一道简单题(SRM531 - Division Two - Level One),是求给定数组不按升序排 ...
随机推荐
- 找出 alter system kill session ‘sid,serial#’ kill 掉的数据库会话对应进程
当我们使用alter system kill session ‘sid,serial#’ 在数据库中kill掉某个会话的时候,如果你观察仔细会发现v$session.paddr发生了改变,从而是的不能 ...
- pthread 的 api 分类
pthreads defines a set of C programming language types, functions and constants. It is implemented w ...
- 人工智能,你到底是天使or魔鬼?
人工智能的概念早在60多年前就被提出,但又一度沉寂.随着谷歌人工智能程序AlphaGo(阿尔法狗)战胜围棋世界冠军李世石,再次为世人瞩目.然而,与无限风光一起相伴而来的,还有关于人工智能的种种争议! ...
- mysql 查询格式化时间
select DATE_FORMAT(addtime,'$m %d %Y') from tablename 输出:01 28 2019 数据库时间格式:2019-01-28 15:01:20
- 搭建appium环境
1.下载jdk1.8 配置环境变量 JAVA_HOME---------->你的jdk路径 path---------------------->%JAVA_HOME%\bin;%JAV ...
- selenium自动化(二).........................................Demo篇
二 编写简单代码 简单代码一: demo1.py 1.from selenium import webdriver driver = webdriver.Chrome() driver.get(& ...
- Linux一些简单命令
1.安装gvim:sudo apt-get install vim-gtk vim和gvim相同,只是后者比前者多了一个界面,此界面可以用来保存.新建.查找等. 三种模式,insert(i),norm ...
- 51nod 1079 中国剩余定理模板
中国剩余定理就是同余方程组除数为质数的特殊情况 我直接用同余方程组解了. 记得exgcd后x要更新 还有先更新b1再更新m1,顺序不能错!!(不然会影响到b1的更新) #include<cstd ...
- 洛谷 P4147 玉蟾宫 (最大子矩形问题)
这道题用到了悬线法,非常牛逼,可以看这个论文. https://blog.csdn.net/twtsa/article/details/8120269 #include<cstdio> # ...
- 蓝的成长记——追逐DBA(18):小机上WAS集群故障,由一次更换IP引起
原创作品.出自 "深蓝的blog" 博客,欢迎转载,转载时请务必注明出处.否则追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong ...