poj 1833 排列

根据一个序列,求下一个序列。如果一串数字是降序排列,则一定是组合成的最大的数字,只要这串数字中有地方是升序的,则不是组合成的最大数字。
比如:num[6] = 123654
从后向前,如果num[i] < num[i+1],则停止循环,在这里是3<6,则从num[i+1]后边的数字(5和4)里面找到一个数n,n满足条件n>3(num[i])&&n<6(num[i+1]),而且这个n是找到的满足条件里面的最小的(如果找不到这个n,则交换num[i]和num[i+1]),这时交换n和3(num[i]),则序列变成124563,然后把(num[i])4后边的数字从小到大排序,得到124356,就找到了下一个序列

第一遍用的cin和cout,超时了,后来换成scanf和printf就ac了,还是c的速度快

#include <cstdio>
#include <algorithm>
using namespace std;
int n,k;
int num[1025];

void process()
{
    int i,j;
    for(i = n-2; i >= 0; --i)
        if(num[i] < num[i+1])
            break;
    if(i == -1)
    {
        sort(num,num+n);
        return ;
    }
    int t = i+1;
    //如果从(t,n)找不到符合的,那么t就和i替换,所以从后向前循环比较好,因为t后边是降序排列的
    for(j = n-1; j >= i+2; --j)
        if(num[j] > num[i] && num[j] < num[t])
            break;

    int temp = num[j];
    num[j] = num[i];
    num[i] = temp;

    sort(num+t,num+n);

    return ;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&k);
        for(int i = 0; i < n; ++i)
            scanf("%d",&num[i]);
        for(int i = 0; i < k; ++i)
            process();
        for(int i = 0; i < n; ++i)
            printf("%d ",num[i]);
        printf("\n");
    }
    return 0;
}
时间: 2024-08-10 02:10:08

poj 1833 排列的相关文章

POJ 1833 排列(全排列 STL)

题目链接:http://poj.org/problem?id=1833 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

POJ 1833 排列【STL/next_permutation】

题目描述: 大家知道,给出正整数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,表示

POJ 1833 生成排列

题目链接:POJ 1833 /************************************ * author : Grant Yuan * time : 2014/10/19 16:38 * source : POJ 1833 * algorithm: STL+排列的生成 *************************************/ #include <iostream> #include <algorithm> #include <cstdio&

poj 1833

http://poj.org/problem?id=1833 next_permutation这个函数是用来全排列的,按字典的序进行排列,当排列无后继的最大值时,会执行字典升序排列,相当于排序: 当排列无后继的最大值时返回值为false,其他的为true: 也可以在其后加一个cmp函数 1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 5 using namespace std; 6

POJ 1833 排序

http://poj.org/problem?id=1833 题意: 给出一个排序,求出它之后的第k个排序. 思路: 排序原理: 1.如果全部为逆序时,说明已经全部排完了,此时回到1~n的排序. 2.从后往前找到第一对 ai<ai+1,然后从i+1~n寻找比ai大的最小的数并与之互换,之后再对i+1~n的数进行排序即可. 1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #includ

初学ACM - 组合数学基础题目PKU 1833

题目链接:http://poj.org/problem?id=1833 题意说的很清楚,就是找出当前排列后的第k个排列. 很容易的,就能利用STL的next_permulation()函数写出一个答案: #include <iostream> #include <cstdio> #include <algorithm> using namespace std; int data[1025]; int main(){     int n,k,m;     scanf(&q

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

转载:poj题目分类(侵删)

转载:from: POJ:http://blog.csdn.net/qq_28236309/article/details/47818407 按照ac的代码长度分类(主要参考最短代码和自己写的代码) 短代码:0.01K–0.50K:中短代码:0.51K–1.00K:中等代码量:1.01K–2.00K:长代码:2.01K以上. 短:1147.1163.1922.2211.2215.2229.2232.2234.2242.2245.2262.2301.2309.2313.2334.2346.2348

poj题库分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea