Alice's mooncake shop
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2795 Accepted Submission(s): 703
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.
![](https://i-blog.csdnimg.cn/blog_migrate/52824cfa4ec02012d617b97fad81eea1.jpeg)
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 i th line( i starts from 1) contains a integer indicating the cost to make a mooncake during the i th hour . The cost is no more than 10000. Jan 1st 2000 0 o'clock belongs to the 1 st hour, Jan 1st 2000 1 o'clock belongs to the 2 nd 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.
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 i th line( i starts from 1) contains a integer indicating the cost to make a mooncake during the i th hour . The cost is no more than 10000. Jan 1st 2000 0 o'clock belongs to the 1 st hour, Jan 1st 2000 1 o'clock belongs to the 2 nd 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
70Hint“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
解题思路:日期计算+简单线段树,先计算每个月饼的总制作成本=制作成本+存储成本,建立线段树,对于每个询问就是计算从询问时间与询问时间之前不变质的时间点这一段时间中的最小值加上到询问时间的保存费用(因为储存成本是算到制作时间的末尾,实际上如果询问时间是在制作时间的中间正好减掉多余的保存成本)
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#define Maxn 100005
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define Inf 0x7fffffff
using namespace std;
int cost[Maxn];
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
struct Order
{
string mon;
int day,year,hour,num;
}order[Maxn];
long long tree[Maxn<<2];
inline int leap(int year){
return (year%4==0&&year%100!=0)||year%400==0;
}
int get_time(string s,int y,int d,int h)
{
int m,ret=y*365+(y-1)/4-(y-1)/100+(y-1)/400;
days[1]+=leap(y);
if(s=="Jan")
m=1;
else if(s=="Feb")
m=2;
else if(s=="Mar")
m=3;
else if(s=="Apr")
m=4;
else if(s=="May")
m=5;
else if(s=="Jun")
m=6;
else if(s=="Jul")
m=7;
else if(s=="Aug")
m=8;
else if(s=="Sep")
m=9;
else if(s=="Oct")
m=10;
else if(s=="Nov")
m=11;
else
m=12;
for(int i=0;i<m-1;ret+=days[i++]);
days[1]=28;
return (ret+d)*24+h;
}
void push_up(int rt)
{
tree[rt]=min(tree[rt<<1],tree[rt<<1|1]);
}
void build(int l,int r,int rt)
{
if(l==r)
{
tree[rt]=cost[l];
return ;
}
int m=(l+r)>>1;
build(lson);
build(rson);
push_up(rt);
}
long long query(int L,int R,int l,int r,int rt)
{
if(L<=l&&R>=r)
return tree[rt];
int m=(l+r)>>1;
long long ans=Inf;
if(L<=m)
ans=min(ans,query(L,R,lson));
if(R>m)
ans=min(ans,query(L,R,rson));
return ans;
}
int main()
{
int i,m,n,t,c,a,b;
long long ans;
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
while(scanf("%d%d",&m,&n),m+n)
{
ans=0;
memset(cost,0,sizeof(cost));
for(i=1;i<=m;i++)
{
cin>>order[i].mon;
scanf("%d%d%d%d",&order[i].day,&order[i].year,&order[i].hour,&order[i].num);
}
scanf("%d%d",&t,&c);
for(i=1;i<=n;i++)
{
scanf("%d",&cost[i]);
cost[i]+=(n-i)*c;
}
build(1,n,1);
for(i=1;i<=m;i++)
{
b=get_time(order[i].mon,order[i].year,order[i].day,order[i].hour)-get_time("Jan",2000,1,0)+1;
a=b-t;
int L=max(1,a);int R=min(b,n);
//cout<<L<<" "<<R<<endl;
ans+=(query(L,R,1,n,1)+(b-n)*c)*order[i].num;
}
printf("%I64d\n",ans);
}
return 0;
}