HDU-4122 Alice's mooncake shop

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


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. 
HDU-4122
Alice's mooncake shop

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.
 
Source
/**
    题意:一家月饼店 从2000年1月1日开始 开m个小时,接受了n个订单,现在知道n个订单的时间,并且知道店每天可以再整点的时间做月饼,
        并且花费为temp[i] 当然月饼也可以提前做,但是月饼都有一个保质期保质期为T,并且如果提前做那么就必须冷藏,冷藏室每小时花费M
        现在有n个订单,要求完成所有的订单的最小的花费
    做法:RMQ 求当前之间的~当前时间-T的最小值;然后...... 
**/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define  inf 0x7fffffff
const int maxn = 100010;
char month[15][10] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int Day[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int num[3600];
int Time[3600];
inline int minn(int a, int b) {
    return a > b ? b : a;
}

///2000年1月1日0
bool  check(int year)
{
    if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
        return true;
    }
    return false;
}
///gettime(ch, dd, yy, hh)
int gettime(char ch[], int day, int year, int hh)
{
    int res =  0;
    int mm;
    for(int i = 0; i < 12; i++)
    {
        if(strcmp(month[i], ch) == 0)
        {
            mm = i + 1;
            break;
        }
        else {
            continue;
        }
    }
    // cout << "mm = " << mm << endl;
    for(int i = 2000; i < year; i++)
    {
        if(check(i)) {
            res += 366 * 24;
        }
        else {
            res += 365 * 24;
        }
    }
    for(int i = 1; i < mm; i++)
    {
        res += Day[i - 1] * 24;
        if(check(year) && i == 2) {
            res += 24;
        }
    }
    for(int i = 1; i < day; i++) {
        res += 24;
    }
    res += hh + 1;
    return res;
}
long long dp[maxn][20];
void makeRMQIndex(int n, long long b[]) //形成最小值下标的RMQ
{
    for(int i = 0; i < n; i++) {
        dp[i][0] = i;
    }
    for(int j = 1; (1 << j) <= n; j++)
        for(int i = 0; i + (1 << j) - 1 < n; i++) {
            dp[i][j] = b[dp[i][j - 1]] < b[dp[i + (1 << (j - 1))][j - 1]] ? dp[i][j - 1] : dp[i + (1 << (j - 1))][j - 1];
        }
}
int rmqIndex(int s, int v, long long b[])
{
    if(s < 0) {
        s = 0;
    }
    int k = (int)(log(v - s + 1) / log(2.0));
    return b[dp[s][k]] < b[dp[v - (1 << k) + 1][k]] ? dp[s][k] : dp[v - (1 << k) + 1][k];
}

int  temp[maxn];
long long  tmp[maxn];
int main()
{
    int n, m;
    while(~scanf("%d %d", &n, &m))
    {
        if(n == 0 && m == 0) {
            break;
        }
        char ch[20];
        int dd, yy, hh;
        for(int i = 0; i < n; i++)
        {
            scanf("%s %d %d %d %d", ch, &dd, &yy, &hh, &num[i]);
            Time[i] = gettime(ch, dd, yy, hh);
            // cout << Time[i] << endl;
        }
        int T, S;
        scanf("%d %d", &T, &S);
        for(int i = 0; i < m; i++)
        {
            scanf("%d", &temp[i]);
            tmp[i] = temp[i] + S * (m - i - 1);
        }
        makeRMQIndex(m, tmp);
        long long  sum = 0;
        long long  cet = 0;
        for(int i = 0; i < n; i++)
        {
            int tt = rmqIndex(Time[i] - T, Time[i] - 1, tmp);
            cet = temp[tt] + (Time[i] - 1 - tt) * S;
            sum += cet * num[i];
        }
        cout << sum << endl;
    }
    return 0;
}