posts - 129,comments - 0,views - 37692
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.

思路:正解应该是dfs,但是我为了偷懒,想开一个巨大的数组bfs,没想到hdu卡内存啊 啊啊啊!!害得我调试了好久好久!该死!

复制代码
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <set>
  7 using namespace std;
  8 
  9 const int maxn=101;
 10 const int limit=101000;
 11 const int kkk=limit+100;
 12 struct qq
 13 {
 14     int a,b;
 15     bool friend operator < (const qq &c,const qq &d)
 16     {
 17         if (c.a==d.a)
 18             return c.b<d.b;
 19         return c.a<d.a;
 20     }
 21 }q[maxn];
 22 
 23 int n,p,tail,baktail,head,sum1,rec,bt,lh0,lh1,h,t;
 24 short int l[kkk][2];
 25 short int x[kkk][maxn];
 26 int cnt=0,sum[maxn],s[maxn][maxn],pos[maxn][maxn];
 27 bool flag,flag2,flag3;
 28 
 29 void close()
 30 {
 31 fclose(stdin);
 32 fclose(stdout);
 33 exit(0);
 34 }
 35 
 36 void print()
 37 {
 38     for (int i=h+1;i<=t;i++)
 39     {
 40         for (int j=1;j<l[i][1];j++)
 41         {
 42             printf("%d ",q[x[i][j]].a);
 43         }
 44         printf("%d\n",q[x[i][l[i][1]]].a);
 45     }
 46 }
 47 
 48 bool judge()
 49 {
 50 p--;
 51     if (p<=0) 
 52     {
 53         return true;
 54     }
 55     return false;
 56 }
 57 
 58 
 59 
 60 void work()
 61 {
 62 head=0;tail=0;flag=false,flag2=true,flag3=false,cnt=0;
 63 memset(l,0,sizeof(l));
 64 memset(x,0,sizeof(x));
 65 h=0;
 66 t=0;
 67 for (int i=1;i<=sum[0];i++)
 68 {
 69     t++;
 70     if (judge()) 
 71     {
 72         print();
 73         return;
 74     }
 75     x[i][1]=s[0][i];
 76     l[i][0]=pos[0][i];
 77     l[i][1]=1;
 78 }
 79 print();
 80 h=0;
 81 p++;
 82        while (h!=t)
 83         {
 84              bt=t;
 85              while (h!=bt)
 86              {
 87                  h=h % limit +1;
 88                  lh0=l[h][0];
 89                  lh1=l[h][1];
 90                  for (int i=1;i<=sum[lh0];i++)
 91                  {
 92                      if (judge()) 
 93                      {
 94                          return;
 95                      }
 96                      t=t % limit +1;
 97                      memcpy(x[t],x[h],(lh1+1)*sizeof(int));
 98                      l[t][1]=lh1+1;  //多了一个数
 99                      l[t][0]=pos[lh0][i];
100                      x[t][l[t][1]]=s[lh0][i];
101                       for (int j=1;j<l[t][1];j++)
102                      {
103                        printf("%d ",q[x[t][j]].a);
104                     }
105                      printf("%d\n",q[x[t][l[t][1]]].a);
106                  }
107              }
108          }
109 print();
110 }
111 
112 void init()
113 {
114 freopen("seq.in","r",stdin);
115 freopen("seq.out","w",stdout);
116   while (scanf("%d%d",&n,&p)!=EOF)
117   {
118       memset(q,0,sizeof(q));
119       for (int i=1;i<=n;i++)
120       {
121           scanf("%d",&q[i].a);
122           q[i].b=i;
123       }
124       sort(q+1,q+n+1);
125       memset(s,0,sizeof(s));
126       memset(pos,0,sizeof(pos));
127      memset(sum,0,sizeof(sum));
128       for (int i=0;i<=n;i++)
129       {
130       rec=-1000;
131           for (int j=i+1;j<=n;j++)
132           {
133               if (q[i].b<q[j].b && rec!=q[j].a)
134               {
135                   flag=false;
136                   if (flag) continue;
137                   sum[i]++;
138                   s[i][sum[i]]=j;
139                   pos[i][sum[i]]=j;
140                   rec=q[j].a;
141                }
142           }
143       }
144       work();
145       printf("\n");
146   }
147 }
148 
149 int main ()
150 {
151 init();
152 close();
153 return 0;
154 }
复制代码

 

posted on   cssystem  阅读(175)  评论(0)    收藏  举报

点击右上角即可分享
微信分享提示