SDAU课程练习2 1024

Sequence one

Time Limit : 6000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1   Accepted Submission(s) : 1
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 (&lt;=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
 

Author
yifenfei
 

Source
奋斗的年代

题目大意:

很奇怪的题目吧。给出第一个 3  是三个数,第二个 5  是输出前5 个递增序列。如果不够五个,就全部输出。
看样例:

3 5     1  3  2


建立一棵树
                                1            3               2
                             /     \                           
                          3        2                          
                            

然后就是  dfs  遍历输出了吧。其实我感觉过程更像是  bfs   不过感觉用  bfs  会爆栈。这个是没办法优化的。因为要都输出出来、
嗯  就是这样,思路就不写了吧。都在这里面那。还有就是这个题的剪枝。比如  3  这个,下面已经没有了。就不用再检索了、
也就是说,每次检索的时候都去检查一遍,如果这是第一层的话。就去检索从前一位置到第一个位置有没有出现过当前数字。如果出现过,那么肯定在前面已经被检索过了。如果不是第一个就从当前串的前一个检索到当前数字。如果出现过,说明也已经被检索过了、。。。

感想:

这个题还有下一个写了好几天,最后还是失败了,参考别人的代码写出来的。其实思路已经很清楚了。但就是写不出来。输出不够完整。
经历还不够。

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;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值