HDU 4122 Alice's mooncake shop monotone queue optimization DP

Source: Internet
Author: User

Alice ' s mooncake shop

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

http://acm.hdu.edu.cn/showproblem.php?pid=4122

Description the Mid-Autumn Festival, also known as the Moon Festival or Zhongqiu Festival is a popular harvest Festival CE Lebrated 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 are in September or early October in the Gregorian calendar. It's a date that parallels the autumnal equinox of the solar calendar, when the moon was at its fullest and roundest.

The traditional food of this festival is the mooncake. Chinese family members and friends would gather to admire the bright mid-autumn harvest Moon, and eat mooncakes under the M Oon together. In Chinese, "round" (round) also means something like "faultless" or "reuion", so the roundest moon, and the round mooncakes ma Ke 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 if the time is K-O ' clock sharp (k = 0,1,2 .... ) She can make mooncakes, and We assume that making cakes takes no time. Due to the fluctuation of the "the price" of the ingredients, the cost of a mooncake varies from hour to hour. She can make mooncakes if 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 are S and the storage life of a mooncake are T hours. She now asks you-to-work-out a plan-to-minimize the cost to fulfill the orders.

Input

The input contains no more than test cases.
For each test case:
The first line includes the 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. Following format:

Month Date year H R

It means a certain date, a customer orders R mooncakes at H o ' clock. "Month" is in the format of abbreviation, so it could was "Jan", "Feb", "Mar", "APR", "may", "June", "Jul", "", "Sep", "O CT "," Nov "or" Dec ". H and R is all integers.
All the orders is sorted by the time in increasing order.
The next line contains T and S meaning so the storage life of a mooncake are T hours and the cost to store a mooncake for An hour is S.
Finally, M lines follow. Among those M lines, the i-th line (i-starts from 1) contains an integer indicating the cost-to-make a mooncake during the I th hour. The cost was no more than 10000. Jan 1st 0 o ' clock belongs to the 1st hour, Jan 1st 1 o ' clock belongs to the 2nd hour, ...

(0<n <= 2500; 0 < m,t <=100000; 0<=s <= 200; r<=10000; 0&LT;=H&LT;24)

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

Output

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

Sample Input

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

Sample Output

HINT

Test instructions

Alice opened a mooncake shop with 2500 orders, including an hour (the start of the 1 hour on January 1, 2000 at 0) and the number of mooncakes needed. Then give you the first 100,000 hours of information, including the first I hour to do 1 loaves spent cost[i]. Then give you the shelf life of the mooncake T (stating that order I can only buy [order[i].hour-T ,order[i].hour ] the cake produced by this interval) and save 1 hours of spending s, allowing you to ask for the smallest cost to meet all orders.

Exercises

The first thing to do is to convert the time of the order to the hour starting from 0 o'clock on January 1, 2000, for up to 100,000 hours, so the maximum is 2012. Then maintain a minimum value for the monotonic queue.

Specific implementation:

First we have a monotone queue for the first order, a LQ, and then a 2nd order, we divide the LQ into 2 parts. A: Subscript in order[2].hour-T, order[1].hour the range (a set may be empty) B: Subscript in order[1].hour , order[2].hour the range. For a, because the 2nd order may also use the elements within the a set, we first have to add all elements of a set to the cost of 1.2 order time S*(order[2].hour- order[1].hour) (for saving), and then add the elements of the B set into the monotone queue, the last queue header value is the 2nd order to get a minimum cost of a cake, Multiply the number of orders. Each subsequent order is also processed. Be careful, the answer is long long .

http://altynai.me/2011/11/hdu-4121-4123/

Code

#include <iostream>#include<stdio.h>#include<vector>#include<deque>#include<queue>#include<cstring>using namespacestd;intn,m;Long LongAns =0;stringmon[ -]={"","Jan","Feb","Mar","APR"," May","June","Jul"," the","Sep","Oct","Nov","Dec"};intday[ -]={0, to, -, to, -, to, -, to, to, -, to, -, to};stringM;intD,y,h,num;Long Longtime[2505];d eque<pair<int,int> >Qq;queue<pair<int,int> >Q;intMain () { while(SCANF ("%d%d", &n,&m)! =EOF) {        inttot =0; if(n==0&&m==0) Break;  while(!Qq.empty ())        Qq.pop_back ();  while(!Q.empty ())        Q.pop (); memset (Time,0,sizeof(time));  for(intI=0; i<n;i++) {cin>>M; scanf ("%d%d%d%d",&d,&y,&h,&num);  for(intj= -; j<y;j++)            {                if((j%4==0&& J% -) || J% -==0) D + =366; ElseD + =365; }            intK;  for(k=1; k<= A; k++)                if(mon[k]==M) Break;  for(intj=1; j<k;j++)            {                if(j==2&& (y%4==0&& y% -) || Y% -==0)) d+= in; ElseD+=Day[j]; } d--; Q.push (Make_pair (num,d* -+h)); }        intt,s; scanf ("%d%d",&t,&S); Long LongAns =0;  for(intI=0; i<m;i++)        {            intx; scanf ("%d",&x);  while(! Qq.empty () &&x<=qq.back (). First + (I-qq.back (). Second) *S) Qq.pop_back ();            Qq.push_back (Make_pair (x,i));  while(! Q.empty () &&i==Q.front (). Second) {                 while(! Qq.empty () && Qq.front (). Second + T <i) Qq.pop_front (); Ans+ = (Qq.front (). First + (I-qq.front (). Second) * S) *Q.front (). First;            Q.pop (); }} printf ("%lld\n", ans); }}

HDU 4122 Alice's mooncake shop monotone queue optimization DP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.