Sequence two

1 //这题用set再好不过了 因为set不会插入重复元素并且能对插入的元素进行排序,很好的满足了题目要求的两个排序规则
2 #include<cstdio>
3 #include<cstdlib>
4 #include<iostream>
5 #include<map>
6 #include<set>
7 using namespace std;
8 #define Max 1010
9 int n,p,cnt,len,flag;
10 int a[Max],ans[Max];
11 struct Info
12 {
13 int g;//但前元素能连出去的
14 int value;
15 bool operator < (const Info &a)const//按value值从小到大使其满足字典序
16 {
17 return a.value>value;
18 }
19 };
20 set<Info> s[1100];
21 void DFS(int deep,int sta)
22 {
23 int i,j;
24 if(flag)return;
25 if(deep>len)return;
26 if(n-sta+deep<len)return;
27 if(deep==len)
28 {
29 cnt++;
30 if(cnt==p)
31 flag=1;
32 for(i=1;i<=len;i++)
33 {
34 printf("%d",ans[i]);
35 if(i==len)printf("\n");
36 else printf(" ");
37 }
38 return;
39 }
40 set<Info>::iterator it;
41 for(it=s[sta].begin();it!=s[sta].end();it++)
42 {
43 int next=(*it).g;
44 ans[deep+1]=a[next];
45 DFS(deep+1,next);
46 }
47 }
48 int main()
49 {
50 int i,j,h;
51 Info info;
52 while(scanf("%d%d",&n,&p)!=EOF)
53 {
54 s[0].clear();
55 for(i=1;i<=n;i++)
56 {
57 scanf("%d",&a[i]);
58 info.value=a[i];
59 info.g=i;
60 s[0].insert(info);
61 }
62 for(i=1;i<=n;i++)
63 {
64 s[i].clear();
65 for(j=i+1;j<=n;j++)
66 {
67 if(a[j]>=a[i])
68 {
69 info.value=a[j];
70 info.g=j;
71 s[i].insert(info);
72 }
73 }
74 }
75 cnt=0;flag=0;ans[0]=-1;
76 for(i=1;i<=n;i++)
77 {
78 len=i;
79 DFS(0,0);
80 if(flag)break;
81 }
82 printf("\n");
83 }
84 return 0;
85 }
86 /*
87 4 100
88 1 3 3 3
89 3 5
90 1 3 2
91 3 6
92 1 3 2
93 4 100
94 1 2 3 2
95 */
从论坛买的代码,自己水平解决不了啊aaaa!!!
posted @ 2011-07-12 14:15  georgechen_ena  阅读(99)  评论(0)    收藏  举报