第五周学习体会

这个周主要学习了动态规划的有关知识,动态规划程序设计是解最优化问题的一种途径,一种方法,而不是一种特殊的算法,他针对不同类型的问题存在不同的解题方法。在做题的过程中我主要遇到了两个问题,首先我认为动态规划的有些题目有模板可以直接套,但是是由于刚接触动态规划这部分知识做题题量少,对于动态规划状态转移方程的获取存在问题,不能较快的根据题意获取算法。其次是现在动态规划的题目大多是英文题目,我有时候对部分题目的题意理解不清楚,甚至题目给出的样例的获得方法不清楚,这对以动态规划的状态转移方程的获取也出现了一定阻碍。

下面是解决动态规划的一般步骤:
1、判断问题是否具有最优子结构性质,若不具备则不能用动态规划。
2、把问题分成若干个子问题(分阶段)。
3、建立状态转移方程(递推公式)。
4、找出边界条件。
5、将已知边界值带入方程。
6、递推求解。

例题:
1.Milking Time
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.

Farmer John has a list of M (1 ≤ M ≤ 1,000) possibly overlapping intervals in which he is available for milking. Each interval i has a starting hour (0 ≤ starting_houri ≤ N), an ending hour (starting_houri < ending_houri ≤ N), and a corresponding efficiency (1 ≤ efficiencyi ≤ 1,000,000) which indicates how many gallons of milk that he can get out of Bessie in that interval. Farmer John starts and stops milking at the beginning of the starting hour and ending hour, respectively. When being milked, Bessie must be milked through an entire interval.

Even Bessie has her limitations, though. After being milked during any interval, she must rest R (1 ≤ R ≤ N) hours before she can start milking again. Given Farmer Johns list of intervals, determine the maximum amount of milk that Bessie can produce in the N hours.

Input

  • Line 1: Three space-separated integers: N, M, and R
  • Lines 2…M+1: Line i+1 describes FJ’s ith milking interval withthree space-separated integers: starting_houri , ending_houri , and efficiencyi

Output

  • Line 1: The maximum number of gallons of milk that Bessie can product in the N hours

Sample Input
12 4 2
1 2 8
10 12 19
3 6 24
7 10 31
Sample Output
43

首先输入开始时间结束时间和效率,然后只需要把结束时间加上工作后休息时间就可以满足题目要求进行休息,状态转移方程是只要前提满足a[i].start>=a[j].end,就可以将之前赋值的dp[i]和之前的dp[j]+effect[i]之间求取最大值,即可对上述问题做出解决。

ac代码如下:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
struct Node
{
	int start;
	int end;
	int effect;
}a[10001];
int dp[10001];
bool cmp(Node a,Node b)
{
	return a.start<b.start;
}
int main()
{
	int N,M,R,i,j;
	while(cin>>N>>M>>R)
	{
		for(i=0;i<M;i++)
		{
			cin>>a[i].start>>a[i].end>>a[i].effect;
			a[i].end+=R;
		}
		sort(a,a+M,cmp);
		dp[0]=a[0].effect;
		int sum=dp[0];
		for(i=1;i<M;++i)
		{
			dp[i]=a[i].effect;
			for(j=i-1;j>=0;j--)
			{
				if(a[i].start>=a[j].end)
				{
					dp[i]=max(dp[i],dp[j]+a[i].effect);
				}
			}
			sum=max(sum,dp[i]);
		}
		cout<<sum<<endl;
	}
	return 0;

2.Jumping Cows
Farmer John’s cows would like to jump over the moon, just like the cows in their favorite nursery rhyme. Unfortunately, cows can not jump.

The local witch doctor has mixed up P (1 <= P <= 150,000) potions to aid the cows in their quest to jump. These potions must be administered exactly in the order they were created, though some may be skipped.

Each potion has a ‘strength’ (1 <= strength <= 500) that enhances the cows’ jumping ability. Taking a potion during an odd time step increases the cows’ jump; taking a potion during an even time step decreases the jump. Before taking any potions the cows’ jumping ability is, of course, 0.

No potion can be taken twice, and once the cow has begun taking potions, one potion must be taken during each time step, starting at time 1. One or more potions may be skipped in each turn.

Determine which potions to take to get the highest jump.
Input

  • Line 1: A single integer, P

  • Lines 2…P+1: Each line contains a single integer that is the strength of a potion. Line 2 gives the strength of the first potion; line 3 gives the strength of the second potion; and so on.
    Output

  • Line 1: A single integer that is the maximum possible jump.
    Sample Input
    8
    7
    2
    1
    8
    4
    3
    5
    6
    Sample Output
    17

题意大致就是从P组数中任意取出n个数,i是这n个数的编号,若i为奇数则这个数加,若i为偶数则这个数减,求最后可能得到的最大和。这道题出错的原因是他需要通过while(cin>>P)来重复出入数据。

ac代码如下:

#include<iostream>
using namespace std;
int a[150001];
int main()
{
    int P;
    while(cin>>P)
    {
        for(int i=0;i<P;i++)
        cin>>a[i];
        int sum1=a[0];
        int sum2=0;
        for(int i=1;i<P;i++)
        {
            if(sum1>sum2+a[i]) sum2=sum1-a[i];
            if(sum1<sum2+a[i]) sum1=sum2+a[i];
        }
        if(sum1>sum2) cout<<sum1<<endl;
        else cout<<sum2<<endl;
    }
    return 0;
}

3.FatMouse
FatMouse believes that the fatter a mouse is, the faster it runs. To disprove this, you want to take the data on a collection of mice and put as large a subset of this data as possible into a sequence so that the weights are increasing, but the speeds are decreasing.
Input
Input contains data for a bunch of mice, one mouse per line, terminated by end of file.

The data for a particular mouse will consist of a pair of integers: the first representing its size in grams and the second representing its speed in centimeters per second. Both integers are between 1 and 10000. The data in each test case will contain information for at most 1000 mice.

Two mice may have the same weight, the same speed, or even the same weight and speed.
Output
Your program should output a sequence of lines of data; the first line should contain a number n; the remaining n lines should each contain a single positive integer (each one representing a mouse). If these n integers are m[1], m[2],…, m[n] then it must be the case that

W[m[1]] < W[m[2]] < … < W[m[n]]

and

S[m[1]] > S[m[2]] > … > S[m[n]]

In order for the answer to be correct, n should be as large as possible.
All inequalities are strict: weights must be strictly increasing, and speeds must be strictly decreasing. There may be many correct outputs for a given input, your program only needs to find one.
Sample Input
6008 1300
6000 2100
500 2000
1000 4000
1100 3000
6000 2000
8000 1400
6000 1200
2000 1900
Sample Output
4
4
5
9
7

这道题首先应该给输入的这些老鼠的数据排序,通过cmp函数先根据体重升序排列,如果体重相同的老鼠按照速度降序排列。我一开始一直出错的原因是将体重升序和速度降序分开写了,但是后来发现如果有多只老鼠体重相同就无法满足我的需要。

这是错误的代码:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 10001
int dp[N];
int m[N];
struct mouse
{
    int weight;
    int speed;
    int num;
};
bool cmp1(mouse a,mouse b)
{
    return a.weight<b.weight;
}
int main()
{
    mouse a[10001];
    int n=0;
    int ans=0;
    while(cin>>a[n].weight>>a[n].speed)
    {
        a[n].num=n+1;
        n++;
    }
    sort(a,a+n,cmp1);
    int temp;
    for(int i=0;i<n;i++)
    {
        if(a[i].weight==a[i+1].weight&&a[i].speed<a[i+1].speed)
        {
            temp=a[i].speed;
            a[i].speed=a[i+1].speed;
            a[i+1].speed=temp;
        }
    }
    for(int i=0;i<n;i++)
    {
        dp[i]=1;
    }
    for(int i=n-1;i>=0;i--)
    {
        for(int j=i;j<n;j++)
        {
            if(a[i].weight<a[j].weight&&a[i].speed>a[j].speed)
            {
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
        ans=max(ans,dp[i]);
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        if(ans==dp[i])
        {
            cout<<a[i].num<<endl;
            ans--;
        }
        if(ans==0) break;
    }
    return 0;
}

排完序之后如果数据满足前者体重小且速度大,就可以将dp[i]和dp[j]+1之前取最大值,将最大值赋给ans,在最后输出编号的时候,如果dp[i]于ans相等,就可以输出这个i时的a[i].num。

ac的代码:

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define N 10001
int dp[N];
int m[N];
struct mouse
{
    int weight;
    int speed;
    int num;
};
bool cmp(mouse A,mouse B)
{
     if(A.weight!=B.weight)
        return A.weight<B.weight;
     else
     return A.speed>B.speed;
 }
int main()
{
    mouse a[10001];
    int n=0;
    int ans=0;
    while(cin>>a[n].weight>>a[n].speed)
    {
        a[n].num=n+1;
        n++;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++)
    {
        dp[i]=1;
    }
    for(int i=n-1;i>=0;i--)
    {
        for(int j=i;j<n;j++)
        {
            if(a[i].weight<a[j].weight&&a[i].speed>a[j].speed)
            {
                dp[i]=max(dp[i],dp[j]+1);
            }
        }
        ans=max(ans,dp[i]);
    }
    cout<<ans<<endl;
    for(int i=0;i<n;i++)
    {
        if(ans==dp[i])
        {
            cout<<a[i].num<<endl;
            ans--;
        }
        if(ans==0) break;
    }
    return 0;
}

生死看淡,不服就干!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值