HDU-4122-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): 4817    Accepted Submission(s): 1300


 

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

  
   

题意:Alice开了家月饼店,现有2500笔订单,订单包括某小时(2000年1月1日0点算第1个小时的开始)和需要的月饼数量。然后给你前100000小时的信息,包括第i个小时做1个饼的花费cost[i]。然后给你月饼的保质期T(说明订单i只能买[order[i].hour-T ,order[i].hour ]这个区间生产的饼)和保存1小时的花费S,让你求最小的花费满足所有订单。

题解:

首先要把订单的时间转化成自2000年1月1日0点开始的第几小时,由于最多100000小时,所以最大到2012年的样子。然后维护一个最小值的单调队列。就是把天数转化为时间这个方法可以学习

具体实现:

首先我们已经获得第一个Order的单调队列,记为LQ,然后是处理第2个订单,我们把LQ分成2个部分。A:下标在order[2].hour-T, order[1].hour范围(A集合可能为空) B:下标在order[1].hour , order[2].hour的范围。对于A,由于第2个订单也可能使用到A集合里面的元素,所以我们要先对A集合所有元素加上1.2订单时间差的花费S*(order[2].hour- order[1].hour)(用于保存),然后再把B集合的元素加到单调队列里面,最后队列头的值即为第2个订单得到一个饼的最小花费,乘以订单数即可。之后的每个订单同样处理即可。注意,答案要long long。代码解释写的也都很详细了。

代码:

/*
 *        ┏┓    ┏┓
 *        ┏┛┗━━━━━━━┛┗━━━┓
 *        ┃       ┃  
 *        ┃   ━    ┃
 *        ┃ >   < ┃
 *        ┃       ┃
 *        ┃... ⌒ ...  ┃
 *        ┃       ┃
 *        ┗━┓   ┏━┛
 *          ┃   ┃ Code is far away from bug with the animal protecting          
 *          ┃   ┃   神兽保佑,代码无bug
 *          ┃   ┃           
 *          ┃   ┃        
 *          ┃   ┃
 *          ┃   ┃           
 *          ┃   ┗━━━┓
 *          ┃       ┣┓
 *          ┃       ┏┛
 *          ┗┓┓┏━┳┓┏┛
 *           ┃┫┫ ┃┫┫
 *           ┗┻┛ ┗┻┛
 */
// warm heart, wagging tail,and a smile just for you!
//
//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                           O\  =  /O
//                        ____/`---'\____
//                      .'  \|     |//  `.
//                     /  \|||  :  |||//  \
//                    /  _||||| -:- |||||-  \
//                    |   | \\  -  /// |   |
//                    | \_|  ''\---/''  |   |
//                    \  .-\__  `-`  ___/-. /
//                  ___`. .'  /--.--\  `. . __
//               ."" '<  `.___\_<|>_/___.'  >'"".
//              | | :  `- \`.;`\ _ /`;.`/ - ` : | |
//              \  \ `-.   \_ __\ /__ _/   .-` /  /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//                     佛祖保佑      永无BUG
#include<bits/stdc++.h>
#define LL long long
#define N 2505
#define M 100005
using namespace std;
int n,m,d,y,h,num,k,T,S;
int Date[15]={0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},a[M],pos[M],sum[M];
string Mon[15]={"","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov" , "Dec"};
string s;
LL ans;
struct ljh
{
	int t,ned;
	//需要到的第几个小时,需要的量
	ljh(){};
	ljh(int tt,int need)
	{
		t=tt;
		ned=need;
	}
	bool operator < (const ljh& b)const
	{
		return t>b.t;
	}
};
int main()
{
	while(scanf("%d%d",&n,&m)&&n!=0&&m!=0)
	{
		ans=0;
		priority_queue<ljh>q;
		while(!q.empty())q.pop();
		deque< pair<int,int> >Q;//STL双端队列
		while(!Q.empty())Q.pop_back();
		for(int i=1;i<=n;i++)
		{
		cin>>s;
		scanf("%d%d%d%d",&d,&y,&h,&num);
		for(int j=2000;j<y;j++)
			if((j%4==0&&j%100!=0)||j%400==0)d+=366;
			else d+=365;//之前有某一年是闰年
		for(int j=1;j<=12;j++)
			if(Mon[j]==s)
			{
				k=j;
				break;
			}
		for(int j=1;j<k;j++)
			if(j==2&&((y%4==0&&y%100!=0)||y%400==0))d+=29;
		//计算该年之前月份的天数,该年是否是闰年
			else d+=Date[j];
		d--;//因为我本身那一天并没有过满,并且已经有了当天的第几点,所以需要减去本身那一天
		q.push(ljh(d*24+h,num));
		}
		scanf("%d%d",&T,&S);
		//保存的时间价格
		for(int i=0;i<m;i++)
		//题目说的是第几个小时,而我之前的n个需求是第几点的,换算成第i点的价格
		{
			int x;
			scanf("%d",&x);
			while(!Q.empty()&&x<=Q.back().first+(i-Q.back().second)*S)
				Q.pop_back();
			//现在做一个价格相对于之前做了放几个小时的价格比较
			Q.push_back(make_pair(x,i));
			// ljh now=q.top();
			while(!q.empty()&&q.top().t==i)
			{
				while(!Q.empty()&&i-Q.front().second>T)Q.pop_front();
				//现在的时间减去之前做的时间,看是否过期了
				ans+=(Q.front().first+(i-Q.front().second)*S)*q.top().ned;
				//(做的时候的价格+(存放的时间)*存放价格)*需求的量
				q.pop();//可能有多个需求在同一时间
			}
		}
		printf("%lld\n",ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值