Quantcast
Channel: CSDN博客推荐文章
Viewing all articles
Browse latest Browse all 35570

hdu4122之单调队列

$
0
0

Alice's mooncake shop

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1980    Accepted Submission(s): 485


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
题意:有n个订单和可以在m小时内制作月饼

接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼

接下来一行t,s表示制作的月饼可以保质t天,每保质一天需要花费s的价值

接下来m行表示从第0小时开始在该时间制作月饼的花费的价值

求完成所有订单消耗的最小价值

分析:粗略一想感觉很像优先队列,把每小时制作月饼到交付i订单的花费的价值求出来再用优先队列,求在不过期的条件下的最小值累加;//i=0,1,...n-1

但是细想下这根本完成不了,因为优先队列排序必须排序优先级的值是固定的(或者说入队的点的信息是固定的),而这里制作月饼的时间相对订单时间的时间差在改变,所以花费价值也会改变,所以无法用优先队列,从而想到了用单调队列,单调队列能很好的维护去见内的最大值/最小值

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<iomanip>
#define INF 99999999
using namespace std;

const int MAX=2500+10;
char mon[5];
int M[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

struct Node{
	int num,t;//对于订单来说num表示月饼数量,t表示订单时间;对于制作月饼来说num表示耗费的价值,t表示制作时间 
}order[MAX],q[MAX*4];//q表示单调递增队列

int MON(char *m){
	if(strcmp(m,"Jan") == 0)return 1;
	if(strcmp(m,"Feb") == 0)return 2;
	if(strcmp(m,"Mar") == 0)return 3;
	if(strcmp(m,"Apr") == 0)return 4;
	if(strcmp(m,"May") == 0)return 5;
	if(strcmp(m,"Jun") == 0)return 6;
	if(strcmp(m,"Jul") == 0)return 7;
	if(strcmp(m,"Aug") == 0)return 8;
	if(strcmp(m,"Sep") == 0)return 9;
	if(strcmp(m,"Oct") == 0)return 10;
	if(strcmp(m,"Nov") == 0)return 11;
	return 12;
}

bool LeapYear(int &year){
	return year%4 == 0 && year%100 || year%400 == 0;
}

int Time(int &year,int Mon,int &d,int &h){
	int t=0;
	for(int i=2000;i<year;++i){
		if(LeapYear(i))t+=366;
		else t+=365;
	}
	bool flag=LeapYear(year);
	for(int i=1;i<Mon;++i){
		if(flag && i == 2)t+=29;
		else t+=M[i];
	}
	t+=d-1;
	return t*24+h;
}

int main(){
	int n,m,t,s,r,h,d,year,a;
	while(~scanf("%d%d",&n,&m),n+m){
		for(int i=0;i<n;++i){
			scanf("%s%d%d%d%d",mon,&d,&year,&h,&r);
			order[i].num=r,order[i].t=Time(year,MON(mon),d,h);//转化成小时 
		}
		int top=0,tail=0,p=0;
		__int64 sum=0;
		scanf("%d%d",&t,&s);
		for(int i=0;i<m;++i){
			scanf("%d",&a);
			while(top<tail && q[tail-1].num+(i-q[tail-1].t)*s>=a)--tail;
			q[tail].num=a,q[tail++].t=i;
			while(p<n && i == order[p].t){
				while(q[top].t+t<i)++top;
				sum+=(q[top].num+(i-q[top].t)*s)*order[p++].num;
			}
		}
		printf("%I64d\n",sum);
	}
	return 0;
} 


作者:xingyeyongheng 发表于2013-9-6 23:58:15 原文链接
阅读:143 评论:0 查看评论

Viewing all articles
Browse latest Browse all 35570

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>