DP基础模板
基础dp
hdu2182:Frog
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 3060 Accepted Submission(s): 1363
Problem Description
A little frog named Fog is on his way home. The path’s length is N (1 <= N <= 100), and there are many insects along the way. Suppose the
original coordinate of Fog is 0. Fog can stay still or jump forward T units, A <= T <= B. Fog will eat up all the insects wherever he stays, but he will
get tired after K jumps and can not jump any more. The number of insects (always less than 10000) in each position of the path is given.
How many insects can Fog eat at most?
Note that Fog can only jump within the range [0, N), and whenever he jumps, his coordinate increases.
Input
The input consists of several test cases.
The first line contains an integer T indicating the number of test cases.
For each test case:
The first line contains four integers N, A, B(1 <= A <= B <= N), K (K >= 1).
The next line contains N integers, describing the number of insects in each position of the path.
Output
each test case:
Output one line containing an integer - the maximal number of insects that Fog can eat.
Sample Input
1
4 1 2 2
1 2 3 4
Sample Output
8
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t, n, k, a, b;
cin >> t;
int num[110];
while (t--)
{
cin >> n >> a >> b >> k;
int dp[110][110] = { 0 };//dp[i][j]表示经过了i次跳跃,青蛙来到了第j个位置吃到的最大昆虫数量
int ans = 0;
for (int i = 0; i < n; i++)
cin >> num[i];
dp[0][0] = num[0];
for (int i = 1; i <=k; i++)
{
for (int j = 1; j < n; j++)
{
for (int c = a; c <= b; c++)
{
if(j-c>=0&&dp[i-1][j-c]!=0)
dp[i][j] = max(dp[i][j], dp[i - 1][j - c] + num[j]);
ans = max(ans, dp[i][j]);
}
}
}
cout << ans << endl;
}
return 0;
}
01背包
Bone Collector
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 137305 Accepted Submission(s): 54382
Problem Description
Many years ago , in Teddy’s hometown there was a man who was called “Bone Collector”. This man like to collect varies of bones , such as dog’s , cow’s , also he went to the grave …
The bone collector had a big bag with a volume of V ,and along his trip of collecting there are a lot of bones , obviously , different bone has different value and different volume, now given the each bone’s value along his trip , can you calculate out the maximum of the total value the bone collector can get ?
Input
The first line contain a integer T , the number of cases.
Followed by T cases , each case three lines , the first line contain two integer N , V, (N <= 1000 , V <= 1000 )representing the number of bones and the volume of his bag. And the second line contain N integers representing the value of each bone. The third line contain N integers representing the volume of each bone.
Output
One integer per line representing the maximum of the total value (this number will be less than 231).
Sample Input
1
5 10
1 2 3 4 5
5 4 3 2 1
Sample Output
14
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct x {
int val, vol;
}bone[1010];
int dp[1001][1001] = { 0 };//dp[i][j]代表在背包容量为j的情况下装前面i件物品且尽可能要装第i件物品的最大价值
int main()
{
int t, n, v;
cin >> t;
while (t--)
{
cin >> n >> v;
for (int i = 1; i <= n; i++)
cin >> bone[i].val;
for (int i = 1; i <= n; i++)
cin >> bone[i].vol;
for(int i=1;i<=n;i++)
for (int j = 0; j <= v; j++)//从0开始,骨头体积可能为0
{
if (j - bone[i].vol >= 0)
dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - bone[i].vol] + bone[i].val);
else
dp[i][j] = dp[i - 1][j];
}
cout << dp[n][v] << endl;
memset(dp, 0, sizeof(dp));
}
return 0;
}
LCS
Common Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 69344 Accepted Submission(s): 32171
Problem Description
A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = <x1, x2, …, xm> another sequence Z = <z1, z2, …, zk> is a subsequence of X if there exists a strictly increasing sequence <i1, i2, …, ik> of indices of X such that for all j = 1,2,…,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
Sample Input
abcfbc abfcab
programming contest
abcd mnp
Sample Output
4
2
0
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
int dp[1010][1010];
int main()
{
string s1, s2;
while (cin >> s1 >> s2)
{
for(int i=1;i<=s1.size(); i++)
for (int j = 1; j <= s2.size(); j++)
{
if (s1[i - 1] == s2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
cout << dp[s1.size()][s2.size()] << endl;
memset(dp, 0, sizeof(dp));
}
return 0;
}
LIS
最少拦截系统
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 101377 Accepted Submission(s): 38092
Problem Description
某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭.由于该系统还在试用阶段,所以只有一套系统,因此有可能不能拦截所有的导弹.
怎么办呢?多搞几套系统呗!你说说倒蛮容易,成本呢?成本是个大问题啊.所以俺就到这里来求救了,请帮助计算一下最少需要多少套拦截系统.
Input
输入若干组数据.每组数据包括:导弹总个数(正整数),导弹依此飞来的高度(雷达给出的高度数据是不大于30000的正整数,用空格分隔)
Output
对应每组数据输出拦截所有导弹最少要配备多少套这种导弹拦截系统.
Sample Input
8 389 207 155 300 299 170 158 65
Sample Output
2
//LCS求LIS
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;
int dp[1010][1010];
int main()
{
int s1[1010], s2[1010];
int n;
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
cin >> s1[i];
s2[i] = s1[i];
}
sort(s1, s1 + n);
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
{
if (s1[i - 1] == s2[j - 1])
dp[i][j] = dp[i - 1][j - 1] + 1;
else
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
}
cout << dp[n][n] << endl;
memset(dp, 0, sizeof(dp));
}
return 0;
}
//暴力dp
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 10050;
int n, high[MAXN];
int LIS()
{
int ans = 1;
int dp[MAXN];
dp[1] = 1;
for (int i = 1; i <= n; i++)
{
int maxn = 0;
for (int j = 1; j < i; j++)
if (dp[j] > maxn && high[j] < high[i])
{
maxn = dp[j];
}
dp[i] = maxn + 1;//要放外边,不然结果会变成0
if (dp[i] > ans)
ans = dp[i];
}
return ans;
}
int main()
{
while (cin >> n)
{
for (int i = 1; i <= n; i++)
{
cin >> high[i];
}
cout << LIS() << endl;
}
return 0;
}
//贪心加二分
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN = 10050;
int n, high[MAXN];
int LIS() {
int len = 1;
int d[MAXN];
d[1] = high[1];
for (int i = 2; i <= n; i++)
if (high[i] > d[len])
d[++len] = high[i];
else
{
int j = lower_bound(d + 1, d + len + 1, high[i]) - d;
d[j] = high[i];
}
return len;
}
int main()
{
while (cin >> n)
{
for (int i = 1; i <= n; i++)
{
cin >> high[i];
}
cout << LIS() << endl;
}
return 0;
}
递推和记忆化搜索
The Triangle
Time Limit: 1000MS | Memory Limit: 10000K | |
---|---|---|
Total Submissions: 72925 | Accepted: 43537 |
Description
73 88 1 02 7 4 44 5 2 6 5(Figure 1)
Figure 1 shows a number triangle. Write a program that calculates the highest sum of numbers passed on a route that starts at the top and ends somewhere on the base. Each step can go either diagonally down to the left or diagonally down to the right.
Input
Your program is to read from standard input. The first line contains one integer N: the number of rows in the triangle. The following N lines describe the data of the triangle. The number of rows in the triangle is > 1 but <= 100. The numbers in the triangle, all integers, are between 0 and 99.
Output
Your program is to write to standard output. The highest sum is written as an integer.
Sample Input
5
7
3 8
8 1 0
2 7 4 4
4 5 2 6 5
Sample Output
30
#include<iostream>
#include<cstring>
using namespace std;
int maxy[105][105];
int num[105][105];
int main()
{
int n;
cin>>n;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= i; j ++)
cin>>num[i][j];
for(int i = 1; i <= n; i ++)
maxy[n][i] = num[n][i];//给最后一行赋值
for(int i = n - 1; i >= 1; i --)//从最后一行往回算,计算每一个点的最大值
for(int j = 1; j <= i; j ++)
maxy[i][j] = max(maxy[i + 1][j], maxy[i + 1][j + 1]) + num[i][j];//找出并记录每一个点的最大值
cout<<maxy[1][1]<<endl;//回溯的终点
return 0;
}