4的最后一道题……我被它搞疯了……
Sequence two |
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) |
Total Submission(s): 43 Accepted Submission(s): 17 |
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 |
#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
using namespace std;
int n,p;
struct Node
{
int pos;
int v;
};
int cont;
Node a[110];
int res[10001];
int deep;
bool mcmp(const Node& x,const Node& y)
{
if(x.v != y.v)
return x.v < y.v;
else
return x.pos < y.pos;
};
void init()
{
int i;
int tv;
cont = 0;
Node tnode;
for(i = 1;i <= n;i ++)
{
scanf("%d",&tv);
a[i].pos = i;// 把位置作为结构题的一项
a[i].v = tv;
}
sort(a+1,a+1+n,mcmp);//按照值的大小重排序
}
bool dfs(int dep,int sp,int pp)//deepth ,search position , previous position
{
int i;
int prev;
bool f = false;
if(dep == deep+1)
{
cont ++;
for(i = 1;i < dep;i ++)
printf(i == deep ? "%d\n" : "%d ",res[i]);
if(cont == p) return true;
return false;
}
if(sp > n ) return false;
for(i = sp;i <= n;i ++)
{
if(a[i].pos > pp)
{
if(!f) { f = true; prev = a[i].v;} // 判重
else if(prev == a[i].v) continue;//判重
prev = a[i].v;
res[dep] = a[i].v;
if(dfs(dep+1,i+1,a[i].pos) ) return true;
}
}
return false;
}
void work()
{
int i,j;
for(i = 1;i <= n;i ++)
{
deep = i;
if(dfs(1,1,0)) return ;
}
}
int main()
{
while(scanf("%d%d",&n,&p) != EOF)
{
init();
work();
printf("\n");
}
return 0;
}
然后我基本照抄了一遍……就是错啊……T^T……抄都抄错是有多二啊……
#include <iostream>
#include <algorithm>
using namespace std;
int n,p;
struct nnode
{
int pos,v;//the position,value
}a[101];
int res[10001];
int deep;
int ct;//the whole deep
bool cmp(nnode x,nnode y)
{
if(x.v!=y.v)
return x.v<y.v;
else
return x.pos<y.pos;
}
bool dfs(int dep,int sp,int pp)//deep,search position,previous position
{
int i;
int prev;//previous position
bool f=false;
if(dep==deep+1)//find an array
{
ct++;//count
for(i=1;i<deep;i++)//cout
{
cout<<res[i];
}
cout<<endl;
if(ct==p)return true;//meet the p's need,finish job
return false;
}
if(sp>n)return false;//there is no number can be used
for(i=sp;i<=n;i++)
{
if(a[i].pos>pp)
{
if(!f)//if it is the first number in this array
{
f=true;
prev=a[i].v;
}
else
if(prev==a[i].v)
continue;
prev=a[i].v;
res[dep]=a[i].v;
if(dfs(dep+1,i+1,a[i].pos))
return true;
}
}
return false;
}
int main()
{
while(cin>>n>>p)
{
int i;
ct=0;//initiate
for(i=1;i<=n;i++)
{
cin>>a[i].v;
a[i].pos=i;
}
sort(a+1,a+n+1,cmp);
for(i=1;i<=n;i++)
{
deep=i;//the length of the array
if(dfs(1,1,0))//return true means searched all
break;
}
cout<<endl;
}
}
希望能啥时候找到错误……吐血……去做java作业了……
明天可以开始刷5了……哈哈~。。虽然说为了做题而做题不好…………不过做了总比没做好哈哈~加油加油~