USACO 3.2 kimbits DP

自己YY了个DP:设f[n][l]为n位数中包含不超过l个1的总个数

f[n][l]=f[n-1][l]+f[n-1][l-1]

然后用_search()从高位向低位扫描即可,tmp记录当前已记下多少个数了(这些数肯定都比第I个小)

一开始f数组的初值YY错了,看了nocow改过来就好了

 1 /*
 2 PROB:kimbits
 3 LANG:C++
 4 */
 5 #include <stdio.h>
 6 #include <cstring>
 7 int N,L,tmp;
 8 long long I;
 9 int a[1000];
10 int f[1000][1000];
11
12 void _search(int n,int l)
13 {
14     if ((n<=0)||(l<=0)) return;
15     int t1=f[n-1][l],t2=f[n-1][l-1];
16     if (tmp+t1>=I)
17     {
18         a[n]=0;
19         _search(n-1,l);
20     }
21     else
22     {
23         tmp=tmp+t1;
24         a[n]=1;
25         _search(n-1,l-1);
26     }
27 }
28
29 int main()
30 {
31     freopen("kimbits.in","r",stdin);
32     freopen("kimbits.out","w",stdout);
33
34     scanf("%d%d%lld",&N,&L,&I);
35
36     memset(f,0,sizeof(f));
37     for (int i=0;i<=N;i++)
38     {
39         f[i][0]=1;
40         f[0][i]=1;
41     }
42
43     for (int i=1;i<=N;i++)
44         for (int j=1;j<=L;j++)
45             if (j>i)
46                 f[i][j]=f[i][i];
47             else
48                 f[i][j]=f[i-1][j]+f[i-1][j-1];
49           // XXXXX =  0XXXX  +  1XXXX
50 /*
51     for (int i=1;i<=N;i++)
52     {
53         for (int j=1;j<=N;j++)
54             printf("%d ",f[i][j]);
55         printf("\n");
56     }
57 */
58     tmp=0;
59     _search(N,L);
60
61     for (int i=N;i>=1;i--)
62         printf("%d",a[i]);
63     printf("\n");
64
65     return 0;
66 }

时间: 2024-10-10 17:13:35

USACO 3.2 kimbits DP的相关文章

USACO Cow Pedigrees 【Dp】

一道经典Dp不过现在还不是能特别理解. 定义dp[i][j] 表示由i个节点,j 层高度的累计方法数 状态转移方程为: 用i个点组成深度最多为j的二叉树的方法树等于组成左子树的方法数 乘于组成右子树的方法数再累计. 暂贴代码: /* ID: wushuai2 PROG: nocows LANG: C++ */ //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler #include <stdio.h>

POJ3280 Cheapest Palindrome 【DP】

Cheapest Palindrome Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6013   Accepted: 2933 Description Keeping track of all the cows can be a tricky task so Farmer John has installed a system to automate it. He has installed on each cow a

poj 2385 Apple Catching(dp)

Description It is a little known fact that cows love apples. Farmer John has two apple trees (which are conveniently numbered 1 and 2) in his field, each full of apples. Bessie cannot reach the apples when they are on the tree, so she must wait for t

poj 3616 Milking Time(dp)

Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible. Fa

POJ3616——Milking Time

Milking Time Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4964   Accepted: 2076 Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤ N ≤

USACO prefix TrieTree + DP

/* ID:kevin_s1 PROG:prefix LANG:C++ */ #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <vector> #include <map> #include <set> #include <algorithm> #include <cstdlib>

usaco 3.2 Stringsobits 数位dp

StringsobitsKim Schrijvers Consider an ordered set S of strings of N (1 <= N <= 31) bits. Bits, of course, are either 0 or 1. This set of strings is interesting because it is ordered and contains all possible strings of length N that have L (1 <=

USACO nocows DP

这题没想出来,直接参考了nocow,太弱了= =. 基本思想是动态规划,因为树是递归结构,所以可以递归分成子问题处理.一个树可以看成根加左子树加右子树,所以根据乘法原理,N个节点放成k层的结构等于i个节点放成k - 1层乘以N - i - 1个节点放在k - 1层的积. 令dp[i][j] 为i个节点放j层的最多可能数量,则dp[i][j] = sum{dp[k][j - 1] * dp[i - k - 1][j - 1] + 9901} % 9901 做动规还是没感觉,好忧伤 /* ID:ke

【USACO 2.2】Subset Sums (DP)

N (1 <= N <= 39),问有多少种把1到N划分为两个集合的方法使得两个集合的和相等. 如果总和为奇数,那么就是0种划分方案.否则用dp做. dp[i][j]表示前 i 个数划分到一个集合里,和为j的方法数. dp[i][j]=dp[i-1][j]+dp[i][j-i] n 为 39 时,1 到 39 的和为 780,枚举 j 的时候枚举到 s/2,最后输出dp[n][s/2]/2. http://train.usaco.org/usacoprob2?a=z5hb7MFUmsX&