hdu 2611 Sequence two

Sequence two

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 857    Accepted Submission(s): 310


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 (<=100) 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 subsequence by lexicographical. For example initial sequence 1 3 2 the total legal subsequences is 5. According to order is {1}; {2}; {3}; {1,2}; {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<=100, 1<p<=100000).
 

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 2 3 1 2 1 3 1 2 3 1 2 1 3 1 2 3 1 2 1 3 2 2 2 3 1 2 2 1 2 3
Hint
Hint : You must make sure each subsequence in the subsequences is unique.
 
#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<vector>
#include<cmath>
#define N 105
using namespace std;
int n, p;
int index[N];  //index[i]表示:当前加入到目前子序列中的元素;
int cnt; //用于存储当前所形成的的子序列的个数, 这样可以看看到没到p个;
int len; //用于表示当前正在形成的子序列所要达到的长度,长度达到要求就可以输出了;
bool flag;
struct node{
    int num;
    int pos;
    friend bool operator <(const node x, const node y){
        if(x.num == y.num){
            return x.pos < y.pos;
        }
        else
            return x.num<y.num;
    }
}a[N];

bool dfs(int l, int pos, int repos){
    int i;
    if(l == len){
        cnt++;
        for(i = 0; i < len-1; i++){
            printf("%d ", index[i]);
        }
        printf("%d\n", index[i]);
        if(cnt == p)
            return true;
        return false;
    }
    int temp;
    bool flag = false;
    for(int i = pos; i <= n; i++){
        if(a[i].pos > repos){
            if(!flag){
            flag = true;
            temp = a[i].num;
            }
            else if(temp == a[i].num){ //判重,如果当前的值以前已经搜索过了则跳过
                continue;
            }
            temp = a[i].num;
            index[l] = a[i].num;
            if(dfs(l+1, i+1, a[i].pos)) return true;
        }
    }
    return false;

}
int main(){
    while(scanf("%d%d", &n, &p) != EOF){
        for(int i = 1; i <= n; i++){
            scanf("%d", &a[i].num);
            a[i].pos = i;
        }
        sort(a+1, a+n+1);
        cnt = 0;
        for(int i = 1; i < n; i++){
            len = i;
            if(dfs(0, 1, 0))
                break;
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值