HDU 4122 Alice's mooncake shop (RMQ)

Alice's mooncake shop

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


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.
 

 

Source
 

 

 

只需要静态查询最小值就可以了。

 

有一些细节要处理。

  1 /* ***********************************************
  2 Author        :kuangbin
  3 Created Time  :2013-11-8 9:59:19
  4 File Name     :E:\2013ACM\专题强化训练\区域赛\2011福州\B.cpp
  5 ************************************************ */
  6 
  7 #include <stdio.h>
  8 #include <string.h>
  9 #include <iostream>
 10 #include <algorithm>
 11 #include <vector>
 12 #include <queue>
 13 #include <set>
 14 #include <map>
 15 #include <string>
 16 #include <math.h>
 17 #include <stdlib.h>
 18 #include <time.h>
 19 using namespace std;
 20 
 21 
 22 int getmonth(char s[])
 23 {
 24     if(strcmp(s,"Jan") == 0) return 1;
 25     if(strcmp(s,"Feb") == 0) return 2;
 26     if(strcmp(s,"Mar") == 0) return 3;
 27     if(strcmp(s,"Apr") == 0) return 4;
 28     if(strcmp(s,"May") == 0) return 5;
 29     if(strcmp(s,"Jun") == 0) return 6;
 30     if(strcmp(s,"Jul") == 0) return 7;
 31     if(strcmp(s,"Aug") == 0) return 8;
 32     if(strcmp(s,"Sep") == 0) return 9;
 33     if(strcmp(s,"Oct") == 0) return 10;
 34     if(strcmp(s,"Nov") == 0) return 11;
 35     if(strcmp(s,"Dec") == 0) return 12;
 36 }
 37 int days[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
 38 bool isleap(int y)
 39 {
 40     if(y % 400 == 0 || (y % 100 != 0 && y%4 == 0))return true;
 41     else return false;
 42 }
 43 struct Node
 44 {
 45     char mon[10];
 46     int d,y,h;
 47     int R;
 48     int index;
 49     void input()
 50     {
 51         scanf("%s%d%d%d%d",mon,&d,&y,&h,&R);
 52         if(y < 2000)
 53         {
 54             index = -1;
 55             return;
 56         }
 57         index = 0;
 58         for(int i = 2000;i < y;i++)
 59         {
 60             if(isleap(i)) index += 366*24;
 61             else index += 365*24;
 62         }
 63         for(int i = 1;i < getmonth(mon);i++)
 64             index += days[i]*24;
 65         if(isleap(y) && getmonth(mon) > 2)index += 24;
 66         index += (d-1)*24;
 67         index += h+1;
 68     }
 69 };
 70 
 71 const int MAXN = 100010;
 72 long long dp[MAXN][20];
 73 long long b[MAXN];
 74 int mm[MAXN];
 75 void initRMQ(int n)
 76 {
 77     mm[0] = -1;
 78     for(int i = 1;i <= n;i++)
 79     {
 80         mm[i] = ((i&(i-1)) == 0)?mm[i-1]+1:mm[i-1];
 81         dp[i][0] = b[i];
 82     }
 83     for(int j = 1;j <= mm[n];j++)
 84         for(int i = 1;i + (1<<j) - 1 <= n;i++)
 85             dp[i][j] = min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
 86 }
 87 long long rmq(int x,int y)
 88 {
 89     int k = mm[y-x+1];
 90     return min(dp[x][k],dp[y - (1<<k) + 1][k]);
 91 }
 92 
 93 Node node[3000];
 94 
 95 int main()
 96 {
 97     //freopen("in.txt","r",stdin);
 98     //freopen("out.txt","w",stdout);
 99     int n,m;
100     int T,S;
101     while(scanf("%d%d",&n,&m) == 2)
102     {
103         if(n == 0 && m == 0)break;
104         for(int i = 0;i < n;i++)
105             node[i].input();
106         scanf("%d%d",&T,&S);
107         for(int i = 1;i <= m;i++)
108         {
109             scanf("%I64d",&b[i]);
110             b[i] += (m-i)*S;
111         }
112         initRMQ(m);
113         long long ans = 0;
114         for(int i = 0;i < n;i++)
115         {
116             if(node[i].index < 0 || node[i].index > m)continue;
117             long long tmp = rmq(max(1,node[i].index - T),node[i].index);
118             tmp -= (m - node[i].index)*S;
119             ans += tmp * node[i].R;
120         }
121         cout<<ans<<endl;
122     }
123     return 0;
124 }

 

 

 

 

 

 

posted on 2013-11-08 12:22  kuangbin  阅读(724)  评论(0编辑  收藏  举报

导航

JAVASCRIPT: