hdu 2610 Sequence one ( dfs+可行性剪枝 )
来源:互联网 发布:unity3d小游戏制作视频 编辑:程序博客网 时间:2024/05/20 21:58
Sequence one
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 483 Accepted Submission(s): 179
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 (<=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.
Now give you a number sequence, include n (<=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.
Input
The input contains multiple test cases.
Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000).
Each test case include, first two integers n, P. (1<n<=1000, 1<p<=10000).
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 51 3 23 61 3 24 1001 2 3 2
Sample Output
1321 31 21321 31 21231 21 32 32 21 2 31 2 2HintHint : You must make sure each subsequence in the subsequences is unique.
Author
yifenfei
题目分析:注意几点,判重,可以根据它是子序列的性质,只需判断当前元和上一个元素间在原始串中有没有相同的元,有则重复,如果是当前串的第一位,就判断和之前所有的元中有没有相同的,然后是当前短串找不到的情况下,是找不到更长的符合条件的串的,所以剪枝时要减去,还有剩余原始串长度如果小于剩余当前的串的长度,也是一定无解的,所以要跟据这些有解的必要条件进行剪枝,也就是可行性剪枝
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <map>#define MAX 1007using namespace std;int n,p;int s[MAX];int a[MAX];int cnt;bool check ( int s ,int e ){ for ( int i = s+1 ; i < e ; i++ ) if ( a[i] == a[e] ) return false; return true;}void dfs ( int cur , int now , int len ){ if ( n - cur < len - now ) return; if ( cnt >= p ) return; if ( now == len ) { cnt++; printf ( "%d" , s[0] ); for ( int i = 1 ; i < len ; i++ ) printf ( " %d" , s[i] ); puts ( "" ); return; } for ( int i = cur+1 ; i <= n ; i++ ) { if ( now > 0 && a[i] < s[now-1] ) continue; bool mark = false; for ( int j = cur+1 ; j < i ; j++ ) if ( a[j] == a[i] ) { mark = true; break; } //if ( now == 0 && !check ( 0 , i ) ) continue; //if ( now != 0 && !check ( cur , i ) ) continue; if ( mark ) continue; s[now] = a[i]; dfs ( i , now+1 , len ); if ( cnt == p ) return; }}int main ( ){ //bool flag = false; while ( ~scanf ( "%d%d" , &n , &p ) ) { //if ( flag ) puts (""); //else flag = true; for ( int i = 1 ; i <= n ; i++ ) scanf ( "%d" , &a[i] ); cnt = 0; for ( int i = 1 ; i < n ; i++ ) { int judge = cnt; dfs ( 0 , 0 , i ); if ( cnt == judge || cnt >= p ) break; } puts(""); }}
0 0
- hdu 2610 Sequence one ( dfs+可行性剪枝 )
- hdu 2610 Sequence one【搜索 dfs+剪枝】
- HDU 2610 Sequence one(dfs+剪枝)
- hdu 1455 Sticks(dfs+可行性剪枝)
- HDU 2610 Sequence one
- HDU 2610 Sequence one
- 【HDU】2610 Sequence one
- hdu2610Sequence one(dfs + 剪枝)
- Best Sequence----dfs+剪枝
- HDU 2611 Sequence two (dfs + 重判 + 剪枝)
- 【HDU4090】Gem and Prince-DFS+可行性剪枝
- hdu 4090(搜索+可行性剪枝)
- hdu 2610 dfs+判重剪枝
- DFS 剪枝 hdu 1010
- HDU 1010 DFS+剪枝
- hdu 1445 dfs剪枝
- hdu 1010 DFS + 剪枝
- hdu 1010 dfs+剪枝
- 职业女性确实处于劣势吗?记一次不甚严谨的考据 -- 向胡适之先生的遥远致敬
- Cocos2d-x_Box2D刚体使用PhysicsEditor工具生成形状
- 2011年哈尔滨工业大学计算机研究生机试真题
- WCF实例上下文与并发
- iOS开发--利用NSProxy实现消息转发-模块化的网络接口层设计
- hdu 2610 Sequence one ( dfs+可行性剪枝 )
- 动态规划-阿里笔试
- Spark集群资源调度
- 关于++i与i++以及静态成员变量与非静态成员变量新手疑惑的地方
- 汤米维斯提
- 最长回文子串的长度 南邮NOJ 1968
- 得到UIWebView内视频播放器弹出和关闭的通知
- Linux C++ 简单线程池
- SQLServer 服务器架构扩展之数据库扩展的一些步骤