HDOJ 题目1260 Tickets(动态规划)

本文探讨了在电影院售票场景中,如何通过合理安排相邻顾客同时购买两张票的方式,以最短时间完成所有顾客的购票过程。通过动态规划算法解决实际问题,详细解释了状态转移方程,并给出了具体实现代码及输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tickets

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1193    Accepted Submission(s): 561


Problem Description
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
 

Source
 

Recommend
JGShining   |   We have carefully selected several similar problems for you:   1160  1231  1074  1159  1264 
 

题意:有k个人要买票(你也在里面最后一位),每个人买票要花时间,或者两个相邻的人一起买会有一个时间, 问你最少花费多少时间才能买到票


思路:题意看的半懂的,结合sample差不多理解了,

以某个人节点,他要么单独买,要么和前面一个人或者后面一个人一起买,

和后面一个人一起买其实可以看成是后面一个人和他前面的一个人一起买,

因此相当于只有两种情况,要么自己一个人买,要么和前面一个人一起买

于是设计dp[i][0],dp[i][1]

0:代表自己一个人买时到他这个位置所需要的最少时间

1:代表自己和前面一个人一起买到他当前位置所需的最少时间


于是状态转移方程就出来了

dp[i][0] = max(dp[i-1][0], dp[i-1][1]) + t[i]; (i >= 1 && i <= n, t表示单独买所需的时间)

dp[i][1] = max(dp[i-2][0], dp[i-2][1]) + interval[i]; (i >= 2 && i <=n, interval表示和前面一个人一起买所需的时间)


最后的answer就是 max(dp[n][0], dp[n][1])了

这个结果是s,换算一下变成时分秒 然后加上早上8点整 大于12点就对12取模,下午是pm,上午是am

ac代码
#include<stdio.h>
#include<string.h>
int dp[2020][2],t1[2020],t2[2020];
int min(int a,int b)
{
	if(a>b)
		return b;
	else
		return a;
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n,i,ans,h,m,s;
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%d",&t1[i]);
		}
		for(i=2;i<=n;i++)
		{
			scanf("%d",&t2[i]);
		}
		memset(dp,0,sizeof(dp));
		t2[1]=t1[1];
		for(i=1;i<=n;i++)
		{
			dp[i][0]=min(dp[i-1][0],dp[i-1][1])+t1[i];
			if(i==1)
				dp[i][1]=t1[i];
			else
				dp[i][1]=min(dp[i-2][0],dp[i-2][1])+t2[i];
		}
		ans=min(dp[n][0],dp[n][1]);
		h=8+ans/3600;
		ans%=3600;
		m=ans/60;
		ans%=60;
		s=ans;
		if(h>12)
		{
			printf("%02d:%02d:%02d pm\n",h,m,s);
		}
		else
			printf("%02d:%02d:%02d am\n",h,m,s);
	}
}


资源下载链接为: https://pan.quark.cn/s/d9ef5828b597 在本文中,我们将探讨如何通过 Vue.js 实现一个带有动画效果的“回到顶部”功能。Vue.js 是一款用于构建用户界面的流行 JavaScript 框架,其组件化和响应式设计让实现这种交互功能变得十分便捷。 首先,我们来分析 HTML 代码。在这个示例中,存在一个 ID 为 back-to-top 的 div 元素,其中包含两个 span 标签,分别显示“回到”和“顶部”文字。该 div 元素绑定了 Vue.js 的 @click 事件处理器 backToTop,用于处理点击事件,同时还绑定了 v-show 指令来控制按钮的显示与隐藏。v-cloak 指令的作用是在 Vue 实例渲染完成之前隐藏该元素,避免出现闪烁现象。 CSS 部分(backTop.css)主要负责样式设计。它首先清除了一些默认的边距和填充,对 html 和 body 进行了全屏布局,并设置了相对定位。.back-to-top 类则定义了“回到顶部”按钮的样式,包括其位置、圆角、阴影、填充以及悬停时背景颜色的变化。此外,与 v-cloak 相关的 CSS 确保在 Vue 实例加载过程中隐藏该元素。每个 .page 类代表一个页面,每个页面的高度设置为 400px,用于模拟多页面的滚动效果。 接下来是 JavaScript 部分(backTop.js)。在这里,我们创建了一个 Vue 实例。实例的 el 属性指定 Vue 将挂载到的 DOM 元素(#back-to-top)。data 对象中包含三个属性:backTopShow 用于控制按钮的显示状态;backTopAllow 用于防止用户快速连续点击;backSeconds 定义了回到顶部所需的时间;showPx 则规定了滚动多少像素后显示“回到顶部”按钮。 在 V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值