zoukankan      html  css  js  c++  java
  • hdu Sequence one

    Sequence one

    Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 158    Accepted Submission(s): 49
     
    Problem Description
    Search is important in the acm algorithm. When you want to solve a problem by using the search method, try to cut is very important. Now give you a number sequence, include n (<=1000) integers, each integer not bigger than 2^31, you want to find the first P subsequences that is not decrease (if total subsequence W is smaller than P, than just give the first W subsequences). The order of subsequences is that: first order the length of the subsequence. Second order the sequence of each integer’s position in the initial sequence. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {3}; {2}; {1,3}; {1,2}. {1,3} is first than {1,2} because the sequence of each integer’s position in the initial sequence are {1,2} and {1,3}. {1,2} is smaller than {1,3}. If you also can not understand , please see the sample carefully.
     
    Input
    The input contains multiple test cases. Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000).
     
    Output
    For each test case output the sequences according to the problem description. And at the end of each case follow a empty line.
     
    Sample Input
    3 5
    1 3 2
    3 6
    1 3 2
    4 100
    1 2 3 2
     
    Sample Output
    1
    3
    2
    1 3
    1 2
    
    1
    3
    2
    1 3
    1 2
    
    1
    2
    3
    1 2
    1 3
    2 3
    2 2
    1 2 3
    1 2 2
    Hint
    Hint : You must make sure each subsequence in the subsequences is unique.
     
    Author
    yifenfei
     
    Source
    奋斗的年代
     
    Recommend
    yifenfei

    分析:通过枚举子串长度分别dfs知道没有输出或已达到要求个数,并且保证每次进入dfs采用的数都不同。

    #include<cstdio>
    #include<cstring>
    int cnt;
    int a[1010];
    int ans[1010];
    int tot;
    int n, k;
    int flag;
    
    int fun(int x, int y) {
        int i;
        for (i = x; i < y; ++i)
            if (a[i] == a[y])
                return 0;
        return 1;
    }
    
    void dfs(int now, int num) {
        int i;
        if (num == tot) {
            ++cnt;
            for (i = 0; i < tot - 1; ++i)
                printf("%d ", ans[i]);
            printf("%d\n", ans[tot - 1]);  
            if (cnt == k) 
            flag = 1;
            return;
        }
    
        if (num == 0) {
       //     printf("**");
            for (i = 0; i < n; ++i) {
              //  printf("%d ",i);
                if (fun(0, i)) {  
               //    printf("%d ",i); 
                   ans[num]=a[i];
                    dfs(i, 1);
                    if (flag)
                        return;
                }
            }
        } else {
            for (i = now + 1; i < n; ++i) {
                if (a[i] >= a[now] && fun(now + 1, i)) {
                    ans[num]=a[i];
                    dfs(i, num + 1);
                    if (flag)
                        return;
                }
            }
        }
    }
    
    int main() {
        int i, t;
        while (scanf("%d%d", &n, &k) != EOF) {
            for (i = 0; i < n; ++i)
                scanf("%d", &a[i]);
            cnt = 0;
            for (tot = 1; cnt < k; ++tot) {
              //  printf("tot=%d\n",tot);
                flag = 0;
                t = cnt;
                dfs(0, 0);
                if(t==cnt)
                    break;
                if (flag)
                    break;
            }
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    shell 数组遍历加引号和不加引号的区别?
    shell map数据结构的实现
    PyCharm 项目打开窗口设置为当前还是新开一个怎么办?
    python 模拟ssh 登录远程服务器
    python 字节码死磕
    docker相关内容
    Windows7安装 docker-compose的过程
    史上最简单的Docker入门教程
    MySQL触发器使用详解
    存储过程
  • 原文地址:https://www.cnblogs.com/baidongtan/p/2666259.html
Copyright © 2011-2022 走看看