hdoj2610

Sequence one

Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

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
#include<stdio.h>
#include<memory.h>
#define M 1001

typedef struct{
	long x;
	int p;
}NODE;

NODE node[M];
NODE result[M];

int n;
int m;

int count;

int flag;

int equal(int s,int e)
{
	for(int i=s+1;i<e;i++){
		if(node[i].x==node[e].x)
			return 1;
	}
	return 0;
}

void dfs(int s,int num,int step)
{
	if(count>=m)
		return ;
	if(num==step){
		flag=0;
		count++;
		for(int i=0;i<num-1;i++)
			printf("%d ",result[i].x);
		printf("%d\n",result[num-1].x);
		return;
	}
	for(int i=s;i<n;i++){
		if((num==0||node[i].x>=result[num-1].x)&&!((num==0&&equal(-1,i))||(num!=0&&equal(result[num-1].p,i)))){
			result[num].p=i;
			result[num].x=node[i].x;
			dfs(i+1,num+1,step);
		}
	}
	return;
}

int main(void)
{
	while(scanf("%d %d",&n,&m)!=EOF){
		for(int i=0;i<n;i++){
			scanf("%d",&node[i].x);
			node[i].p=i;
		}
		count=0;
		for(i=1;i<n;i++){
			flag=1;
			memset(result,0,sizeof(result));
			dfs(0,0,i);
			if(flag||count>=m)
				break;
		}
		printf("\n");
	}
	return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值