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

第一行是一个正整数m。表示測试数据的个数,以下是m组測试数据。每组測试数据第一行是2个正整数n( 1 <= n < 1024 )和k(1<=k<=64),第二行有n个正整数,是1,2 … n的一个排列。

Output

对于每组输入数据,输出一行。n个数,中间用空格隔开,表示输入排列的下k个排列。

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;
}

Backward Digit Sums

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5072   Accepted: 2923

Description

FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example,
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

Line 1: Two space-separated integers: N and the final sum.

Output

Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.

Sample Input

4 16

Sample Output

3 1 2 4

Hint

Explanation of the sample:

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;
}

The Next Permutation

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 979   Accepted: 717

Description

For this problem, you will write a program that takes a (possibly long) string of decimal digits, and outputs the permutation of those decimal digits that has the next larger value (as a decimal number) than the input number. For example:

123 -> 132

279134399742 -> 279134423799

It is possible that no permutation of the input digits has a larger value. For example, 987.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set is a single line that contains the data set number, followed by a space, followed by up to 80 decimal digits which is the input
value.

Output

For each data set there is one line of output. If there is no larger permutation of the input digits, the output should be the data set number followed by a single space, followed by the string BIGGEST. If there is a solution, the output should be the data
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;
}
时间: 2024-10-10 07:03:24

POJ1833 &amp; POJ3187 &amp; POJ3785 next_permutation应用的相关文章

poj1833 排列

排列 Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15934   Accepted: 6413 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排列为第

全排列(next_permutation)

next_permutation函数既可用于非重排列也可用于重排列: #include <bits/stdc++.h>#define MAXN 200000+10#define ll long longusing namespace std; int a[MAXN]; int main(void){    int n;    cin >> n;    for(int i=0; i<n; i++)    cin >> a[i];    sort(a, a+n);  

next_permutation暴力搜索,POJ(3187)

题目链接:http://poj.org/problem?id=3187 解题报告: #include <stdio.h> #include <iostream> #include <algorithm> using namespace std; int main() { int n,sum; scanf("%d%d",&n,&sum); int a[15],b[15]; for(int i=0; i<n; i++) a[i]=i

next_permutation,POJ(1256)

题目链接:http://poj.org/problem?id=1256 解题报告: 1.sort函数是按照ASC11码排序,而这里是按照 'A'<'a'<'B'<'b'<...<'Z'<'z'排序. #include <iostream> #include <algorithm> #include <string> using namespace std; bool cmp(char a,char b) { char m=tolowe

STL next_permutation排列

概念 全排列的生成算法有很多种,有递归遍例,也有循环移位法等等.C++/STL中定义的next_permutation和prev_permutation函数则是非常灵活且高效的一种方法,它被广泛的应用于为指定序列生成不同的排列.本文将详细的介绍prev_permutation函数的内部算法. 按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为减序为止.prev_permutation函数与之相反,是生成给定序列的上一个较小的序列

next_permutation函数

转自此处 http://blog.sina.com.cn/s/blog_9f7ea4390101101u.html 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>下面是以前的笔记    与之完全相反的函数还有prev_permutation  (1) int 类型的next_permutation int main(){ int a[3];a[0]=1;a[1]=2;a[2]=3; do{cout<<a[0]<<" &

A - Next_permutation

首先介绍一下next_permutation函数的用途! 按照STL文档的描述,next_permutation函数将按字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止. prev_permutation函数与之相反,是生成给定序列的上一个较小的排列. 代码如下 #include<iostream> #include<algorithm> using namespace std; int main() { int a[] = {3,6,4,2}; do{ cout

《STL源码剖析》--next_permutation函数

STL中提供了2个计算排列组合关系的算法.分别是next_permucation和prev_permutaion. next_permutation是用来计算下一个(next)字典序排列的组合,而prev_permutation用来计算上一个(prev)字典序的排列组合. 字典排序是指排列组合中,按照大小由小到大的排序,例如123的排列组着,字典排序为123,132,213,231,312,321. 看一下next_permutation的实现原理: 从序列的后面向前找,找了两个相邻的元素p[n

STL全排列算法next_permutation和prev_permutation

全排列的问题取决于如何找到"下一个",然后就用同样的方法找到全部的排列 全排列这个问题其实和我们数数:11,12,13,14,15,16,17,18,19,20,21,一样的规律,只不过他的变化只要那固定的几个数自每次都出现,这就导致我们传统的连续整数会在那里丢失一部分:你如何对剩下的这些全排列中的数字看成是"连续的"一列数字,对于"第一个""最小的数字"找到它的"下一个",然后用同样的方法找到再"