HDU 2610 (自己完全找不到思路) Sequence one

搜索虐我千百遍,我待搜索。。。好吧,我还木有初恋

题意:

我开始理解题意就理解偏了,Orz

题中有n个元素构成的序列,求出前p个非递减子序列。子序列是先按长度排序的,然后按原序列先后位置排序的。

这里的非递减是指子序列中从左到右元素大小的值不减,对,就是这我理解错了。

如果p>所有符合要求的子序列的个数,那么输出所有的子序列

这道题完全是参照这个大神的博客的代码A过的

http://www.cnblogs.com/newpanderking/archive/2012/10/11/2719941.html

思路:

按子序列的长度从1到n-1去搜,在搜长为len的子序列时,也是从左到右的元素逐个搜索,当搜索到的元素的个数满足len时,便输出一个子序列。

判重:

①当我们搜索子序列的第一个元素的时候,只需要和该元素之前的比较,如果有重复的话不再搜索

②当我们搜索子序列的第i个元素的时候,将要比较的元素j与 (搜到的子序列中第i-1个元素, 元素j)这个开区间的元素比较,如果有重复的话不再搜索

剪枝:

flag作为一个标记,每次搜索长度为i的子序列的时候将其赋值为false,找到一个长为i的子序列就赋值为true。

试想,如果长为i的子序列都不曾找到,那么一定不会存在长为i+1的子序列的。

再次膜拜一下高冷的代码君:

 1 #define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6
 7 struct Tem
 8 {
 9     int n, pos;
10 }tem[1010];
11 int n, p, len, cnt, num[1010];
12 bool flag;
13
14 bool check(int s, int e)
15 {
16     for(int i = s + 1; i < e; ++i)
17         if(num[i] == num[e])
18             return false;
19     return true;
20 }
21
22 void OutPut(int len)
23 {
24     for(int i = 0; i < len - 1; ++i)
25         printf("%d ", tem[i].n);
26     printf("%d\n", tem[len - 1].n);
27 }
28
29 void DFS(int dep, int pos)
30 {
31     if(cnt >= p)
32         return;
33     if(dep == len)
34     {
35         ++cnt;
36         flag = true;
37         OutPut(len);
38         return;
39     }
40     for(int i = pos; i < n; ++i)
41     {
42         if(dep == 0 || tem[dep - 1].n <= num[i])
43         {
44             if(dep == 0 && !check(-1, i))
45                 continue;
46             if(dep != 0 && !check(tem[dep - 1].pos, i))
47                 continue;
48             tem[dep].n = num[i];
49             tem[dep].pos = i;
50             DFS(dep + 1, i + 1);
51         }
52     }
53     return;
54 }
55
56 int main(void)
57 {
58     #ifdef LOCAL
59         freopen("2610in.txt", "r", stdin);
60     #endif
61
62     while(scanf("%d%d", &n, &p) == 2)
63     {
64         for(int i = 0; i < n; ++i)
65             scanf("%d", &num[i]);
66         cnt = 0;
67         for(int i = 1; i < n; ++i)
68         {
69             flag = false;
70             len = i;
71             DFS(0, 0);
72             if(cnt >= p || !flag)
73                 break;
74         }
75         printf("\n");
76     }
77     return 0;
78 }

代码君

HDU 2610 (自己完全找不到思路) Sequence one

时间: 2024-08-25 11:36:34

HDU 2610 (自己完全找不到思路) Sequence one的相关文章

hdu 4952 Number Transformation (找规律)

题目链接 题意:给你个x,k次操作,对于第i次操作是:要找个nx,使得nx是>=x的最小值,且能整除i,求k次操作后的数 分析: 经过打表找规律,会发现最后的x/i,这个倍数会趋于一个固定的值,求出这个固定的值和K相乘就可以了, 为什么会趋于固定的值呢,因为最后虽然i在不断增长,但是x也是在增长的,每次的倍数会回退一个发现 有余数,然后再加上一个,所以趋于稳定. 官方题解: 1 #include <iostream> 2 #include <cstdio> 3 #includ

HDU 4925 Apple Tree 找呀找规律

间隔着取_(:зゝ∠)_ #include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long ll; int n, m; int init(int i, int j) { int cnt = 1; if(i-1 >= 1) cnt *= 2; if(i+1 <= n) cnt *= 2; if(j-1 >= 1) cnt *=

hdu 4861 Couple doubi (找规律 )

题目链接 可以瞎搞一下,找找规律 题意:两个人进行游戏,桌上有k个球,第i个球的值为1i+2i+?+(p−1)i%p,两个人轮流取,如果DouBiNan的值大的话就输出YES,否则输出NO. 分析:解题报告 1 #include <cstdio> 2 #include <iostream> 3 4 using namespace std; 5 int main() 6 { 7 int k, p; 8 while(cin>>k>>p) 9 { 10 if(k/

HDU 4927 Series (找规律+JAVA)

题目链接:HDU 4927 Series 题意:给出一串N个元素的序列,作为第一串序列,第二串序列是第二串序列相邻元素的查值(即Bi=Ai+1-Ai)...第三串....一直到第N-1串是序列中只有一个数. 刚开始想到模拟一发,WA了一把,推出公式,发现是二项展开的系数(正负交替).组合数,果断要大数,苦逼JAVA不会.和一起队友摸索着,又T了一发,再想到组合数的递推.终于A了 C(a-1,b)=C(a,b)*a/(b-a+1) AC代码: import java.math.BigInteger

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

HDU 4861 Couple doubi(找规律|费马定理)

Couple doubi Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4861 Description DouBiXp has a girlfriend named DouBiNan.One day they felt very boring and decided to play some games. The rule of th

hdu 5703 Desert(找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5703 Desert Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 177    Accepted Submission(s): 141 Problem Description A tourist gets lost in the dese

【转载】 HDU 动态规划46题【只提供思路与状态转移方程】

1.Robberies 连接 :http://acm.hdu.edu.cn/showproblem.php?pid=2955 背包;第一次做的时候把概率当做背包(放大100000倍化为整数):在此范围内最多能抢多少钱  最脑残的是把总的概率以为是抢N家银行的概率之和- 把状态转移方程写成了f[j]=max{f[j],f[j-q[i].v]+q[i].money}(f[j]表示在概率j之下能抢的大洋); 正确的方程是:f[j]=max(f[j],f[j-q[i].money]*q[i].v)  其

HDU 4990 Reading comprehension (找规律+矩阵快速幂)

题目链接:HDU 4990 Reading comprehension 题目给的一个程序其实就是一个公式:当N=1时 f[n]=1,当n>1时,n为奇数f[n]=2*f[n-1]+1,n为偶数f[n]=2*f[n-1]. 先不取模,计算前十个找规律.得到一个递推公式:f[n]=2*f[n-2]+f[n-1]+1 然后快速幂解决之. 给出一个神奇的网站(找数列通项):http://oeis.org/ AC代码: #include<stdio.h> #include<string.h&