hdu 2610 && hdu 2611(bfs)
hdu 2610题意:从数组中找出不超过P个序列,每个序列为不下降序列,输出按1)序列长度;2)每个数出现的先后
做法:bfs,题目要求完全符合bfs的性质。判重很关键,我用了比较水的set来判重。(用set<string>来判重wa,改用set<vector<int> >ac而且比前者快。)
343MS
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-04-22-11.50 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream> 10 #include <cmath> 11 #include <cstring> 12 #include <algorithm> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include <queue> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 25 #define REP(i,n) for(int i=0;i<n;i++) 26 #define REPD(i,n) for(int i=n-1;i>=0;i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 35 36 37 int n,p; 38 int a[1011]; 39 set<vector<int> >S; 40 41 struct node{ 42 vector<int> ss; 43 int tail,end; 44 }; 45 46 void bfs(){ 47 S.clear(); 48 queue<node>Q; 49 node now; 50 int cnt = 0; 51 FOR(i,1,n){ 52 now.ss.clear(); 53 now.ss.PB(a[i]); 54 if(S.count(now.ss))continue; 55 S.insert(now.ss); 56 now.end = i; 57 now.tail = a[i]; 58 Q.push(now); 59 cnt ++; 60 cout<<a[i]<<endl; 61 if(cnt>=p)return; 62 } 63 while(!Q.empty()){ 64 now = Q.front(); 65 Q.pop(); 66 FOR(i,now.end+1,n){ 67 if(now.tail > a[i])continue; 68 node next; 69 next.ss = now.ss; 70 next.ss.PB(a[i]); 71 if(S.count(next.ss))continue; 72 next.end = i; 73 next.tail = a[i]; 74 S.insert(next.ss); 75 int sz = next.ss.size(); 76 REP(i,sz)printf("%d%c",next.ss[i],(i!=sz-1)?' ':'\n'); 77 cnt++; 78 if(cnt>=p)return; 79 Q.push(next); 80 } 81 } 82 } 83 84 int main(){ 85 //freopen("in","r",stdin); 86 //freopen("out","w",stdout); 87 while(~scanf("%d%d",&n,&p)){ 88 FOR(i,1,n)scanf("%d",a+i); 89 bfs(); 90 puts(""); 91 } 92 return 0; 93 }
hdu 2611 题意与上题的差异是输出规则2)按字典序输出
做法:我同样是用bfs和同样的判重方法。每个节点增加一个变量记录下标,先按数列的值的值排序,相同则按下标排(因为缺少了后者wa了几次)。在用bfs扩展的时候,要同时符合下标值和数值不小于前一个节点才扩展。
625MS
1 /* 2 *Author: Zhaofa Fang 3 *Created time: 2013-04-22-11.50 4 *Language: C++ 5 */ 6 #include <cstdio> 7 #include <cstdlib> 8 #include <sstream> 9 #include <iostream> 10 #include <cmath> 11 #include <cstring> 12 #include <algorithm> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 #include <queue> 17 #include <map> 18 #include <set> 19 using namespace std; 20 21 typedef long long ll; 22 #define DEBUG(x) cout<< #x << ':' << x << endl 23 #define FOR(i,s,t) for(int i = (s);i <= (t);i++) 24 #define FORD(i,s,t) for(int i = (s);i >= (t);i--) 25 #define REP(i,n) for(int i=0;i<n;i++) 26 #define REPD(i,n) for(int i=n-1;i>=0;i--) 27 #define PII pair<int,int> 28 #define PB push_back 29 #define MP make_pair 30 #define ft first 31 #define sd second 32 #define lowbit(x) (x&(-x)) 33 #define INF (1<<30) 34 35 36 37 int n,p; 38 set<vector<int> >S; 39 40 struct seq{ 41 int x,pos; 42 }a[111]; 43 bool cmp(seq x,seq y){ 44 if(x.x != y.x)return x.x<y.x; 45 return x.pos<y.pos; 46 } 47 struct node{ 48 vector<int> ss; 49 int tail,end,endpos; 50 }; 51 52 void bfs(){ 53 S.clear(); 54 queue<node>Q; 55 node now; 56 int cnt = 0; 57 FOR(i,1,n){ 58 now.ss.clear(); 59 now.ss.PB(a[i].x); 60 if(S.count(now.ss))continue; 61 S.insert(now.ss); 62 now.end = i; 63 now.tail = a[i].x; 64 now.endpos = a[i].pos; 65 Q.push(now); 66 cnt ++; 67 printf("%d\n",a[i]); 68 if(cnt>=p)return; 69 } 70 while(!Q.empty()){ 71 now = Q.front(); 72 Q.pop(); 73 FOR(i,now.end+1,n){ 74 if(now.tail > a[i].x)continue; 75 if(now.endpos > a[i].pos)continue; 76 node next; 77 next.ss = now.ss; 78 next.ss.PB(a[i].x); 79 if(S.count(next.ss))continue; 80 next.end = i; 81 next.tail = a[i].x; 82 next.endpos = a[i].pos; 83 S.insert(next.ss); 84 int sz = next.ss.size(); 85 REP(i,sz)printf("%d%c",next.ss[i],(i!=sz-1)?' ':'\n'); 86 cnt++; 87 if(cnt>=p)return; 88 Q.push(next); 89 } 90 } 91 } 92 93 94 int main(){ 95 //freopen("in","r",stdin); 96 //freopen("out","w",stdout); 97 while(~scanf("%d%d",&n,&p)){ 98 FOR(i,1,n){ 99 scanf("%d",&a[i].x); 100 a[i].pos = i; 101 } 102 sort(a+1,a+n+1,cmp); 103 bfs(); 104 puts(""); 105 } 106 return 0; 107 }
by Farmer