关闭

hdu4122 hdoj4122

标签: acm 单调队列 杭电
411人阅读 评论(0) 收藏 举报
分类:
Problem Description
The Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest festival celebrated by Chinese people, dating back over 3,000 years to moon worship in China's Shang Dynasty. The Zhongqiu Festival is held on the 15th day of the eighth month in the Chinese calendar, which is in September or early October in the Gregorian calendar. It is a date that parallels the autumnal equinox of the solar calendar, when the moon is at its fullest and roundest. 

The traditional food of this festival is the mooncake. Chinese family members and friends will gather to admire the bright mid-autumn harvest moon, and eat mooncakes under the moon together. In Chinese, “round”(圆) also means something like “faultless” or “reuion”, so the roundest moon, and the round mooncakes make the Zhongqiu Festival a day of family reunion.

Alice has opened up a 24-hour mooncake shop. She always gets a lot of orders. Only when the time is K o’clock sharp( K = 0,1,2 …. 23) she can make mooncakes, and We assume that making cakes takes no time. Due to the fluctuation of the price of the ingredients, the cost of a mooncake varies from hour to hour. She can make mooncakes when the order comes,or she can make mooncakes earlier than needed and store them in a fridge. The cost to store a mooncake for an hour is S and the storage life of a mooncake is T hours. She now asks you for help to work out a plan to minimize the cost to fulfill the orders.
 

Input
The input contains no more than 10 test cases. 
For each test case:
The first line includes two integers N and M. N is the total number of orders. M is the number of hours the shop opens. 
The next N lines describe all the orders. Each line is in the following format:

month date year H R

It means that on a certain date, a customer orders R mooncakes at H o’clock. “month” is in the format of abbreviation, so it could be "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov" or "Dec". H and R are all integers. 
All the orders are sorted by the time in increasing order. 
The next line contains T and S meaning that the storage life of a mooncake is T hours and the cost to store a mooncake for an hour is S.
Finally, M lines follow. Among those M lines, the ith line( i starts from 1) contains a integer indicating the cost to make a mooncake during the ith hour . The cost is no more than 10000. Jan 1st 2000 0 o'clock belongs to the 1st hour, Jan 1st 2000 1 o'clock belongs to the 2nd hour, …… and so on.

(0<N <= 2500; 0 < M,T <=100000; 0<=S <= 200; R<=10000 ; 0<=H<24)

The input ends with N = 0 and M = 0.
 

Output
You should output one line for each test case: the minimum cost. 
 

Sample Input
1 10 Jan 1 2000 9 10 5 2 20 20 20 10 10 8 7 9 5 10 0 0
 

Sample Output
70
Hint
“Jan 1 2000 9 10” means in Jan 1st 2000 at 9 o'clock , there's a consumer ordering 10 mooncakes. Maybe you should use 64-bit signed integers. The answer will fit into a 64-bit signed integer.
 



题意:
第一行给你有多少个人要买(n)和商店开放时间(即可做月饼的时间,m个小时,每小时都能做月饼!但只能在整点做月饼)
下面n行给你来买的人的日期(月,日,年,小时)和要多少个月饼!(商店在2000 Jan 1st 0 o'clock开放)
再下面一行是两个数据,S和T,S是做好的月饼可以保存的时间,T是每个月饼每保存一小时需要花费的钱
然后跟着m行,分别表示第i小时(m>=i>=1)做一个月饼所花费的钱
这里有个关键的地方:
可以在买的人到来前就做好月饼,然后直接给他,也可以现场做现场卖,做月饼是不用时间的。
输出花费的最少的钱!

分析:
刚开始想用贪心直接下去,但是可能月饼无法保存到买的人到来,也有可能是之前做的月饼保存后给买的人更省钱,有很多情况考虑。
但是这题我们看问题只要输出花费的最少的钱
可以想到用单调队列来维护最小值

code:
#include <stdio.h>
#include <string.h>
struct s{
	int time,num;//time是买月饼人到的时间,num是买的个数
}order[2505];//买月饼人的信息
struct p{
	int wastetime,t;//wastetime是现在这个整点做月饼的花费,t是当前时间
}yueb[2505];//做月饼的信息
int months[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};//非闰月和闰月
int leapyear(int year)
{
	return year%4==0&&year%100!=0||year%400==0;
}//判断闰年
int month(char *s)
{
	if(strcmp(s,"Jan")==0)
		return 1;		
	if(strcmp(s,"Feb")==0)
		return 2;
	if(strcmp(s,"Mar")==0)
		return 3;
	if(strcmp(s,"Apr")==0)
		return 4;
	if(strcmp(s,"May")==0)
		return 5;
	if(strcmp(s,"Jun")==0)
		return 6;
	if(strcmp(s,"Jul")==0)
		return 7;
	if(strcmp(s,"Aug")==0)
		return 8;
	if(strcmp(s,"Sep")==0)
		return 9;
	if(strcmp(s,"Oct")==0)
		return 10;
	if(strcmp(s,"Nov")==0)
		return 11;
	return 12;
}//判断在哪个月
int gettime(int m,int d,int y,int h)
{
	int i,t=0;
	for(i=2000;i<y;i++)
		if(leapyear(i))
			t+=366;
		else
			t+=365;
	int flag=leapyear(y);
	for(i=0;i<m-1;i++)
		t+=months[flag][i];
	t+=d-1;
	t=t*24+h+1;
	return t;
}//获取从2000 Jan 1st 0 o'clock到这个时间过了多少个小时
int main()
{
	int i,n,m,d,y,h,o,top,end,T,S,x,j;
	__int64 sum;
	char str[10];
	while(~scanf("%d%d",&n,&m)&&(n!=0||m!=0))
	{
		sum=0;
		for(i=0;i<n;i++){
			scanf("%s%d%d%d%d",str,&d,&y,&h,&o);
			order[i].time=gettime(month(str),d,y,h);
			order[i].num=o;
		}
		top=end=0;//队列的头尾指针
		j=0;//买的人的指针
		scanf("%d%d",&T,&S);
		for(i=1;i<=m;i++){
			scanf("%d",&x);
			while(top<end&&yueb[end-1].wastetime+(i-yueb[end-1].t)*S>=x)//如果队列头指针小于队列尾指针且最后那个月饼花费的总时间比当前的时间更长,那么舍弃这个最后的月饼
				end--;
			yueb[end].t=i;//将当前的花费时间放入队列尾,舍弃原始队列里花费时间比这个长的。
			yueb[end++].wastetime=x;
			while(j<n&&order[j].time==i){//如果买的人的指针不到n-1,且当前时间是买的人来的时间
				while(yueb[top].t+T<i)//如果队首的月饼放不到这个时间,那么top++
					top++;
				sum+=(yueb[top].wastetime+(i-yueb[top].t)*S)*order[j].num;//sum加上最少的花费
				j++;//买的人的指针向后移
			}
		}
		printf("%I64d\n",sum);//这里是关键!不能用%lld,杭电oj要用%I64%
	}
	return 0;
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:6444次
    • 积分:217
    • 等级:
    • 排名:千里之外
    • 原创:15篇
    • 转载:2篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论