POJ 4002 Alice's mooncake shop(DP,单调队列)

http://poj.org/problem?id=4002

Alice's mooncake shop
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 520 Accepted: 159

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.

Source



题意:给出客户买月饼的时间和数量,每个时间点做月饼的成本,月饼的保质期和存储成本,求出最小成本。

求出每个时间点的最小成本即可,成本为min{制作成本+存储成本×存储时间},由于时间在变化,成本也在变化,但他们的“相对成本”之间的大小关系并没有发生变化,我们可以先预处理出“相对”于最后一天的成本(第i小时的成本为cost[i]+(M-i)*S),然后维护一个单调不减的队列,保证队首元素为最优即可。


#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mm 100007

using namespace std;

int q[mm],cost[mm],dp[mm];
int _f,_r;
int t[2501],r[2501];
int T,S,N,M;
int year,date,hour;
char _month[12][5]={ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
int dates[12]={31,29,31,30,31,30,31,31,30,31,30,31};
char month[5];

inline bool leap(int _y)
{
    return  (_y%400==0 || _y%100!=0 && _y%4==0);
}

inline int cal_hours(int _y,itn _m,int _d,int _h)
{
    int res=0;
    for (int i=2000;i<_y;i++)
    {
        if (leap(i))    res+=366;   else    res+=365;
    }

    if (leap(_y))   dates[1]=29;    else    dates[1]=28;

    for (int i=0;i<_m;i++)  res+=dates[i];

    res+=_d;
    res--;

    res*=24;

    return res+_h;
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("/home/fcbruce/文档/code/t","r",stdin);
    #endif // ONLINE_JUDGE

    while (scanf("%d %d",&N,&M),N||M)
    {
        for (int i=0;i<N;i++)
        {
            scanf("%s %d %d %d %d",month,&date,&year,&hour,&r[i]);
            int _m;
            for (int i=0;i<12;i++)
                if (strcmp(month,_month[i])==0) {_m=i;break;}
            t[i]=cal_hours(year,_m,date,hour);

           // printf("%d\n",t[i]);
        }

        scanf("%d %d",&T,&S);
        for (int i=0;i<M;i++)
        {
            scanf("%d",cost+i);
            cost[i]+=S*(M-i);
        }

        _f=0;_r=-1;
        for (int i=0;i<M;i++)
        {
            while (_f<=_r && cost[q[_r]]>cost[i])   _r--;
            q[++_r]=i;

            while (q[_f]<i-T+1) _f++;

            dp[i]=cost[q[_f]]-S*(M-i);
        }

        long long ans=0;
        for (int i=0;i<N;i++)
        {
            ans+=dp[t[i]]*r[i];
        }

        //printf("%lld\n",ans);
        cout<<ans<<endl;

    }


    return 0;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
题目描述 给出一个$n\times m$的矩阵,每个位置上有一个非负整数,代表这个位置的海拔高度。一开始时,有一个人站在其中一个位置上。这个人可以向上、下、左、右四个方向移动,但是只能移动到海拔高度比当前位置低或者相等的位置上。一次移动只能移动一个单位长度。定义一个位置为“山顶”,当且仅当从这个位置开始移动,可以一直走到海拔高度比它低的位置上。请问,这个矩阵中最多有多少个“山顶”? 输入格式 第一行两个整数,分别表示$n$和$m$。 接下来$n$行,每行$m$个整数,表示整个矩阵。 输出格式 输出一个整数,表示最多有多少个“山顶”。 样例输入 4 4 3 2 1 4 2 3 4 3 5 6 7 8 4 5 6 7 样例输出 5 算法1 (递归dp) $O(nm)$ 对于这道题,我们可以使用递归DP来解决,用$f(i,j)$表示以$(i,j)$为起点的路径最大长度,那么最后的答案就是所有$f(i,j)$中的最大值。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码 算法2 (动态规划) $O(nm)$ 动态规划的思路与递归DP类似,只不过转移方程和实现方式有所不同。 状态转移方程如下: $$ f(i,j)=\max f(x,y)+1(x,y)是(i,j)的下一个满足条件的位置 $$ 注意:这里的状态转移方程中的$x,y$是在枚举四个方向时得到的下一个位置,即: - 向上:$(i-1,j)$ - 向下:$(i+1,j)$ - 向左:$(i,j-1)$ - 向右:$(i,j+1)$ 实现过程中需要注意以下几点: - 每个点都需要搜一遍,因此需要用双重for循环来枚举每个起点; - 对于已经搜索过的点,需要用一个数组$vis$来记录,防止重复搜索; - 在进行状态转移时,需要判断移动后的点是否满足条件。 时间复杂度 状态数为$O(nm)$,每个状态转移的时间复杂度为$O(1)$,因此总时间复杂度为$O(nm)$。 参考文献 C++ 代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值