HDU4122-Alice's mooncake shop(單調隊列)

Alice’s mooncake shop

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

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.
題目:HDU4122
題意:Alice開了一個月餅店,從頭2000年1月1日0點開始營業,現在有一些訂單,格式如Jan 1 2000 9 10,表示在2000年1月1日9點要10個月餅,給出一些時間做月餅的成本,並且一個月餅可以儲存T天,每天儲存的花費爲S,現問完成所有訂單的最小花費。
思路:在第n天需要交貨的訂單的最小成本顯然爲n-t天內min(cost[n-k]+k*s)(k<=t),cost[n-k]爲第n-k天製作月餅的材料成本,很顯然對於n我們只需要枚舉其n-t天的成本即可,複雜度爲n*k,當k很小時,不會超時,但是當k很大時,顯然會T了。
我們需要使用單調隊列維護一下這個cost最小值。
單調隊列
我們假設一個隊列滿足一個三個性質:
1.隊列中元素值的大小有序。
2.隊列中元素下標從小到大(有序)。
3.隊列中元素下標滿足一個範圍(n-t)。
對於本題,如果存在這樣一個隊列滿足上述條件,隊中元素的值的大小由小至大排序,那麼隊首的元素就是對應當前n最小的成本。代碼實現:

void get_min()
{
    head=0;
    tail=-1;
    for(int i=1; i<=m; i++)
    {
        while(tail>=head&&(queues[tail].x+(i-queues[tail].pos)*s)>=a[i].x)tail--;
        queues[++tail]=a[i];
        while(queues[head].pos<(i-t))head++;
        nmin[i]=queues[head];
    }
}

上述代碼,模擬一個隊列,head表示隊首座標,tail表示隊尾座標,m即使總的元素長度,t是區間長度,對於每個元素a[i]我們讓其從隊尾開始比較,如果隊尾元素比a[i]大,就將隊尾元素彈出,直到在隊中找到一個比a[i]小的元素(或者隊列爲空),將a[i]放在隊尾,然後從隊首開始,將下標不滿足條件的元素全部彈出。
完成操作後隊首元素即爲當前i對應的最小花費。
本題,先求出每個日期對應的最小花費,然後計算總花費即可,獲取時間時注意閏年的判斷。
AC代碼:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<vector>
#define met(s,k) memset(s,k,sizeof s)
#define scan(a) scanf("%d",&a)
#define scanl(a) scanf("%lld",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannl(a,b) scanf("%lld%lld",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define prin(a) printf("%d\n",a)
#define prinl(a) printf("%lld\n",a)
using namespace std;
typedef long long ll;
const int maxn=1e6+10;
const int inf=2100;
const double eps=1e-4;
int n,m,s,t;
int head,tail;
char chamonth[13][5]= {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
int mday[13]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
struct node
{
    int x,pos;
} a[maxn],queues[2*maxn],nmin[maxn];
struct
{
    char month[10];
    int year,day,mnum,clock;
} order[2505];
bool judge_year(int x)
{
    return x%4==0&&x%100||x%400==0;
}
void get_min()//單調隊列
{
    head=0;
    tail=-1;
    for(int i=1; i<=m; i++)
    {
        while(tail>=head&&(queues[tail].x+(i-queues[tail].pos)*s)>=a[i].x)tail--;
        queues[++tail]=a[i];
        while(queues[head].pos<(i-t))head++;
        nmin[i]=queues[head];
    }
}
int get_month(int x)
{
    for(int i=0; i<11; i++)
        if(strcmp(chamonth[i],order[x].month)==0)return i+1;
    return 12;
}
int get_time(int x)
{
    int hour=0;
    for(int i=2000; i<order[x].year; i++)
    {
        if(judge_year(i))hour+=366*24;
        else hour+=365*24;
    }
    int nmonth=get_month(x);
    for(int i=1; i<nmonth; i++)
    {
        hour+=24*mday[i];
        if(i==2)if(judge_year(order[x].year))hour+=24;
    }
    hour+=(order[x].day-1)*24+order[x].clock+1;
    return hour;
}
int main()
{
    while(scann(n,m),n+m)
    {
        for(int i=1; i<=n; i++)
        {
            scanf("%s%d%d%d%d",order[i].month,&order[i].day,&order[i].year,&order[i].clock,&order[i].mnum);
        }
        scann(t,s);
        for(int i=1; i<=m; i++)scan(a[i].x),a[i].pos=i;
        get_min();
        ll ans=0;
        for(int i=1; i<=n; i++)
        {
            int time=get_time(i);//獲取訂單時間
            if(time>m)continue;
            ans+=(nmin[time].x+(time-nmin[time].pos)*s)*order[i].mnum;
        }
        prinl(ans);
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章