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.<br>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. <br>
Input
The input contains multiple test cases.<br>Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000). <br>
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<br>1 3 2<br>3 6<br>1 3 2<br>4 100<br>1 2 3 2<br>
Sample Output
1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>3<br>2<br>1 3<br>1 2<br><br>1<br>2<br>3<br>1 2<br>1 3<br>2 3<br>2 2<br>1 2 3<br>1 2 2<br><br><br><div style='font-family:Times New Roman;font-size:14px;background-color:F4FBFF;border:#B7CBFF 1px dashed;padding:6px'><div style='font-family:Arial;font-weight:bold;color:#7CA9ED;border-bottom:#B7CBFF 1px dashed'><i>Hint</i></div>Hint : You must make sure each subsequence in the subsequences is unique.</div>
Author
yifenfei
Source
奋斗的年代
题意:在给定的序列中找到固定个数的递增的子序列,如果子序列的总个数少于要求的个数,那么就把所有的子序列输出
思路:深搜,在网上有看到剪枝,不必要的返回就行
感想:。。。怎么还没做完
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct numberr
{
int number;
int postion;
};
numberr b[1005];
int a[1005];
int n,p,cou,dep;
int flag;
int isok(int ss,int ee)
{
for(int i=ss;i<ee;i++)
{
if(a[i]==a[ee])return 0;
}
return 1;
}
void dfs(int nowdep,int pos)
{
if(cou>=p)
return ;
if(nowdep==dep)
{
cou++;
flag=1;
for(int i=0;i<nowdep-1;i++)
{
cout<<b[i].number<<" ";
}
cout<<b[nowdep-1].number<<endl;
return ;
}
for(int i=pos;i<n;i++)
{
if((nowdep!=0&&b[nowdep-1].number<=a[i])||nowdep==0)
{
if(nowdep!=0)
{
if(!isok(b[nowdep-1].postion+1,i))
continue;
}
else
{
if(nowdep==0&&!isok(0,i))
continue;
}
b[nowdep].number=a[i];
b[nowdep].postion=i;
dfs(nowdep+1,i+1);
}
}
}
int main()
{
//freopen("r.txt","r",stdin);
while(cin>>n>>p)
{
for(int i=0;i<n;i++)
cin>>a[i];
cou=0;
for(int i=1;i<n;i++)
{
flag=0;
dep=i;
dfs(0,0);
if(cou>=p||(!flag))break;
}
cout<<endl;
}
return 0;
}