POJ1833 & POJ3187 & POJ3785
要是没有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的更多相关文章
- POJ1833 & POJ3187 & POJ3785 next_permutation应用
要是没有next_permutation这个函数,这些题认为还不算特别水,只是也不一定,那样可能就会有对应的模板了. 反正正是由于next_permutation这个函数.这些题包含之前的POJ122 ...
- 《挑战程序设计竞赛》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的排列 ...
- (DFS、全排列)POJ-3187 Backward Digit Sums
题目地址 简要题意: 输入两个数n和m,分别表示给你1--n这些整数,将他们按一定顺序摆成一行,按照杨辉三角的计算方式进行求和,求使他们求到最后时结果等于m的排列中字典序最小的一种. 思路分析: 不难 ...
- POJ-3187 Backward Digit Sums (暴力枚举)
http://poj.org/problem?id=3187 给定一个个数n和sum,让你求原始序列,如果有多个输出字典序最小的. 暴力枚举题,枚举生成的每一个全排列,符合即退出. dfs版: #in ...
- poj1833 排列
...
- POJ3187 Backward Digit Sums
给出杨辉三角的顶点值,求底边各个数的值.直接DFS就好了 #include<iostream> #include<cstdio> #include<cstring> ...
- 【搜索】POJ-3187 枚举全排列
一.题目 Description FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to ...
- POJ3187 Backward Digit Sums 【暴搜】
Backward Digit Sums Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4487 Accepted: 25 ...
随机推荐
- 在Gridview控件中根据Field Name来取得对应列索引
下面方法,只能在Gridview的BoundField进行操作,而在TemplateField模版中去找的话,就无能为力了,因TemplateField模版没有DataField属性. public ...
- Android 4.1 设置默认开机动态壁纸
最新在对Android 4.1做一些定制性的工作,刚好遇到了设置第三方动态壁纸为默认启动壁纸的问题,遂做笔记如下. 需要修改的文件为: 找到SourceCode/framework/base/core ...
- class中static总结-静态成员函数和静态成员变量
C++规定const静态类成员可以直接初始化,其他非const的静态类成员需要在类声明以外初始化,我们一般选择在类的实现文件中初始化,初始化的方式是书写一遍类型的定义: //A.cpp ); //使用 ...
- IDEA报 : Lombok Requires Annotation Processing
Lombok Requires Annotation Processing Annotation processing seems to be disabled for the project &qu ...
- Day9 - F - Monkey and Banana HDU - 1069
一组研究人员正在设计一项实验,以测试猴子的智商.他们将挂香蕉在建筑物的屋顶,同时,提供一些砖块给这些猴子.如果猴子足够聪明,它应当能够通过合理的放置一些砖块建立一个塔,并爬上去吃他们最喜欢的香蕉. ...
- 【Luogu4448】 [AHOI2018初中组]球球的排列
题意 有 \(n\) 个球球,每个球球有一个属性值 .一个合法的排列满足不存在相邻两个球球的属性值乘积是完全平方数.求合法的排列数量对 \(10^9+7\) 取膜. \(n\le 300\) (本题数 ...
- 拷贝Maven工程依赖的jar包出来
参考:https://blog.csdn.net/fengsheng5210/article/details/80491731
- flutter如何使用配置文件pubspec.yaml(位于项目根目录)来管理第三方依赖包
官方文档 在软件开发中,很多时候有一些公共的库或SDK可能会被很多项目用到,因此,将这些代码单独抽到一个独立模块,然后哪个项目需要使用时再直接集成这个模块,便可大大提高开发效率.很多编程语言或开发工具 ...
- delphi对ZIP解压
Delphi 对GZIP解压 作者:admin 来源:未知 日期:2010/5/9 13:08:46 人气:获取失败 标签: QQ空间新浪微博腾讯微博腾讯朋友QQ收藏百度空间百度贴吧更多0 呵呵,终于 ...
- Python中eval与exec用法的区别
Python中eval,exec这两个函数有着相似的输入参数类型和执行功能,因此在用法上经常出现混淆,以至经常用错,程序易抛出错误.下面主要通过这两个函数的语法来阐述区别,并用例子来进一步说明. 首先 ...