2015
04-16

# 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

#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;
}


1. a是根先忽略掉，递归子树。剩下前缀bejkcfghid和后缀jkebfghicd，分拆的原则的是每个子树前缀和后缀的节点个数是一样的，根节点出现在前缀的第一个，后缀的最后一个。根节点b出现后缀的第四个位置，则第一部分为四个节点，前缀bejk，后缀jkeb，剩下的c出现在后缀的倒数第2个，就划分为cfghi和 fghic，第3部分就为c、c