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 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
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.
题意:
某些小时(按照日期给出)有月饼订单,同时给出数量。
你可以把月饼冰冻,每冰冻一小时,花费为s,最多冰冻t小时。
每个小时对于月饼的制造花费不同。
求完成所有订单的最小花费。
思路:
每个小时的最小月饼花费,要么是这个小时直接做,要么是之前做好冰冻过来。这个与数目没有关系,与订单没有关系。
可以维护每个小时月饼花费最小的单调队列,单调队列递增,队首为花费最小的做月饼时间以及对应花费。
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <iostream>
using namespace std;
const int maxn = 100005;
typedef long long ll;
queue<pair<ll,ll>>q1;
deque<pair<ll,ll>>q2;
map<ll,ll>mp1;
map<string,ll>mp2;
void init()
{
mp1[1] = 31;
mp1[2] = 28;
mp1[3] = 31;
mp1[4] = 30;
mp1[5] = 31;
mp1[6] = 30;
mp1[7] = 31;
mp1[8] = 31;
mp1[9] = 30;
mp1[10] = 31;
mp1[11] = 30;
mp1[12] = 31;
mp2["Jan"] = 1;
mp2["Feb"] = 2;
mp2["Mar"] = 3;
mp2["Apr"] = 4;
mp2["May"] = 5;
mp2["Jun"] = 6;
mp2["Jul"] = 7;
mp2["Aug"] = 8;
mp2["Sep"] = 9;
mp2["Oct"] = 10;
mp2["Nov"] = 11;
mp2["Dec"] = 12;
}
bool is_leap(ll year)
{
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return true;
return false;
}
int main()
{
init();
ll n,m;
while(~scanf("%lld%lld",&n,&m) && (n || m))
{
while(!q1.empty())q1.pop();
while(!q2.empty())q2.pop_back();
string str;
ll day,year,h,r;
for(ll i = 1;i <= n;i++)
{
cin >> str >> day >> year >> h >> r;
ll mon = mp2[str];
day--;
for(ll j = 2000;j < year;j++)
{
if(is_leap(j)) day += 366;
else day += 365;
}
if(is_leap(year)) mp1[2] = 29;
else mp1[2] = 28;
for(ll j = 1;j < mon;j++)
{
day += mp1[j];
}
day *= 24;
h++;
day += h;
q1.push(make_pair(day,r));
}
ll ans = 0;
ll t,s;scanf("%lld%lld",&t,&s);
for(ll i = 1;i <= m;i++)
{
ll x;scanf("%lld",&x);
while(!q2.empty() && q2.back().first + (i - q2.back().second) * s >= x)
{
q2.pop_back();
}
q2.push_back(make_pair(x,i));
while(!q1.empty() && q1.front().first == i)
{
while(i - q2.front().second > t)q2.pop_front();
ans += ((i - q2.front().second) * s + q2.front().first) * q1.front().second;
q1.pop();
}
}
printf("%lld\n",ans);
}
return 0;
}
RMQ写法
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
const int maxn = 100005;
typedef long long ll;
ll d[101100][20],a[maxn];
ll t,s;
ll n,m;
vector<pair<ll,ll> >v;
map<ll,ll>mp1;
map<string,ll>mp2;
void init()
{
mp1[1] = 31;
mp1[2] = 28;
mp1[3] = 31;
mp1[4] = 30;
mp1[5] = 31;
mp1[6] = 30;
mp1[7] = 31;
mp1[8] = 31;
mp1[9] = 30;
mp1[10] = 31;
mp1[11] = 30;
mp1[12] = 31;
mp2["Jan"] = 1;
mp2["Feb"] = 2;
mp2["Mar"] = 3;
mp2["Apr"] = 4;
mp2["May"] = 5;
mp2["Jun"] = 6;
mp2["Jul"] = 7;
mp2["Aug"] = 8;
mp2["Sep"] = 9;
mp2["Oct"] = 10;
mp2["Nov"] = 11;
mp2["Dec"] = 12;
}
bool is_leap(ll year)
{
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)return true;
return false;
}
ll Min(ll i,ll j)
{
if(i == j)return i;
if(i < j)
{
return a[i] + (j - i) * s <= a[j] ? i : j;
}
else if(i > j)
{
return a[j] + (i - j) * s <= a[i] ? j : i;
}
return false;
}
void init2()
{
for(int i = 1;i <= m;i++)d[i][0] = i;
for(int j = 1;(1 << j) <= m;j++)
{
for(int i = 1;i + (1 << j) - 1 <= m;i++)
{
d[i][j] = Min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]);
}
}
}
ll getmin(ll l,ll r)
{
ll k = (r - l + 1);
k = log2(k);
return Min(d[l][k],d[r - (1 << k) + 1][k]);
}
int main()
{
init();
while(~scanf("%lld%lld",&n,&m) && (n || m))
{
v.clear();
memset(d,0,sizeof(d));
string str;
ll day,year,h,r;
for(ll i = 1;i <= n;i++)
{
cin >> str >> day >> year >> h >> r;
ll mon = mp2[str];
day--;
for(ll j = 2000;j < year;j++)
{
if(is_leap(j)) day += 366;
else day += 365;
}
if(is_leap(year)) mp1[2] = 29;
else mp1[2] = 28;
for(ll j = 1;j < mon;j++)
{
day += mp1[j];
}
day *= 24;
h++;
day += h;
v.push_back(make_pair(day,r));
}
ll ans = 0;
scanf("%lld%lld",&t,&s);
for(ll i = 1;i <= m;i++)
{
scanf("%lld",&a[i]);
}
init2();
for(int i = 0;i < n;i++)
{
ll r = v[i].first;
ll minn = getmin(max(1ll,r - t),r);
ans += ((r - minn) * s + a[minn]) * v[i].second;
}
printf("%lld\n",ans);
}
return 0;
}