4.4学习心得

第一道简单的例题
Jesus, what a great movie! Thousands of people are rushing to the cinema. However, this is really a tuff time for Joe who sells the film tickets. He is wandering when could he go back home as early as possible.
A good approach, reducing the total time of tickets selling, is let adjacent people buy tickets together. As the restriction of the Ticket Seller Machine, Joe can sell a single ticket or two adjacent tickets at a time.
Since you are the great JESUS, you know exactly how much time needed for every person to buy a single ticket or two tickets for him/her. Could you so kind to tell poor Joe at what time could he go back home as early as possible? If so, I guess Joe would full of appreciation for your help.
Input
There are N(1<=N<=10) different scenarios, each scenario consists of 3 lines:

  1. An integer K(1<=K<=2000) representing the total number of people;
  2. K integer numbers(0s<=Si<=25s) representing the time consumed to buy a ticket for each person;
  3. (K-1) integer numbers(0s<=Di<=50s) representing the time needed for two adjacent people to buy two tickets together.
    Output
    For every scenario, please tell Joe at what time could he go back home as early as possible. Every day Joe started his work at 08:00:00 am. The format of time is HH:MM:SS am|pm.
    Sample Input
    2
    2
    20 25
    40
    1
    8
    Sample Output
    08:00:40 am
    08:00:08 am
    这题就是很典型的动态规划,思路就是在第i个选择时,选two还是one,只有一个选择标准就是在到i的时间时,哪种方案用的时间少,状态转移方程就可以直接写出来

dp[i]=min(dp[i-1]+on[i],dp[i-2]+tw[i];

因为时间不是很长,所以最后的输出加都加am就好了。

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int dp[10000];
int on[10000],tw[10000];

int min(int a,int b){
	return a>b? b:a;
}
int main(){

	int k;
	int i,j,t,n;


	cin>>t;

	while(t--){
		cin>>n;
		memset(dp,0,sizeof(dp));
			memset(on,0,sizeof(on));
				memset(tw,0,sizeof(tw));

		for(i=1;i<=n;i++)
			cin>>on[i];
		for(i=2;i<=n;i++)
			cin>>tw[i];
       dp[0]=0;
	   dp[1]=on[1];
		for(i=2;i<=n;i++)
			dp[i]=min(dp[i-1]+on[i],dp[i-2]+tw[i]);

		 int h = dp[n]/3600+ 8;
         int m = dp[n]/60%60;
         int s = dp[n]%60;

        
        printf("%02d:%02d:%02d am\n",h,m,s);

	}

}

另外我还想整理一下这道递推的题
例题2: 在一个3 X N的长方形方格中,铺满1X2的骨牌(骨牌个数不限制),给定N,求方案数。

独立去想,因为3N要铺满,首先N要为一个偶数,会想到以两个单位为一次递推,所以增加两排会增加多少种方案,独立的两排,方案数就是32,一共3种
不独立的话,那就有可能是伸出来两块,(伸出来一块是不可能的)。伸出来两块肯定是连在一起的,这就有两种情况,这两种情况又是和少三排,多一块的情况相同,那么前面的思考就是有错误的。和奇数排也是有关系的。那么这样也是和多出来几块是有关系的,所以dp要弄二维数组,dp[i][3];

剩下的工作就是几何上的拼装了,N排的方案数:少一排,多一块(只有一种拼装方式),少两排,多两块(也是一种,另一种和前面重复了)还有就是少两排(三块横着拼起来)那么N排多一块的方案就和少一排,多两块的方案书是一样的,多2块有可能是直接多一个骨牌,或两块不同的骨牌拼起、

f[i][0] = f[i-2][0] + f[i-1][1] + f[i-2][2]  
  f[i][1] = f[i-1][2]
  f[i][2] = f[i][0] + f[i-1][1]
  边界条件     f[0][0] = f[1][1] = f[0][2] = 1

整个过程是很复杂的,不过想清楚了之后就很明了了

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值