1260 Tickets【dp】

本文探讨了一种使用动态规划方法解决电影售票场景中,通过优化相邻顾客同时购买两张票的方式,减少总购票时间的问题。详细介绍了输入数据格式、算法实现及输出结果的处理方式,包括时间的计算和格式化输出。

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

Tickets

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


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

第一次见到这个题是在昨天的一次联合外校的联谊赛中,绞尽脑汁,才在比赛开始三小时之后提交.............之前没一点dp 的基础,如果说有的话,那就是lcs了...然后现在又做了一遍,结果一马虎,贡献了一个wa,呃呃呃.......下次要注意!比赛可不是闹着玩的....


动态规划,主要是找状态转移方程,把某个题的动态求最优解过程弄清楚,这道题也就没太大问题了,这道题的思路是:类似于lcs的情况(所以当时在赛场上直接按模式套用了....),不过只是一维的,比较简单,就是每一次都选择两者一起和单独耗费的时间最少的那一种,每次记录局部最优解,最后得到全局最优解......


#include<stdio.h>
#include<string.h>
#define min(a,b) (a<b?a:b)
int a[2005],b[2005],dp[2005],sum,n;
void ticket()//计算时间
{
	memset(dp,0,sizeof(dp));
	dp[1]=a[1];sum=0;//初始化...
	for(int i=2;i<=n;++i)
	{
		dp[i]=min(dp[i-1]+a[i],dp[i-2]+b[i]);//动态转移方程
	}
	sum=dp[n];
}
void time()//处理输出
{
	int h=8,m=0,s,kase=0;
	h+=sum/3600;sum%=3600;
	m+=sum/60;sum%=60;
	s=sum;//这里时间处理也是醉了....
	if(h>12)//下午了
	{
		kase=1;h%=12;
	}
	printf("%02d:%02d:%02d ",h,m,s);
	if(!kase)//输出不同
	{
		printf("am\n");
		return;
	}
	printf("pm\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
    	int i;
        scanf("%d",&n);
        for(int i=1;i<=n;++i)
        {
        	scanf("%d",a+i);
		}
		for(i=2;i<=n;++i)
		{
			scanf("%d",b+i);
		}
		ticket();//调用函数
		time();
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值