hdu 4122 Alice's mooncake shop(rmq)

hdu2142题目链接 题意:有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接下来一行t,s表示制作的月饼可以保质t天,每保质一天需要花费s的价值 接下来m行表示从第0小时开始在该时间制作月饼的花费的价值 求完成所有订单消耗的最小价值

思路:一开始毫无头绪。。因为读错题了。。。之后发现做月饼只能在整点做,即使是提前,也只能提前整数个小时做。 然后发现冰箱的容量是没有限制的,所以每个订单单独考虑即可。

那么对于每一个订单,我们要找到订单当天以及之前T天,这T+1天中做月饼花费最少的那天做月饼。

但是如果对于每个订单,如果每次都更新相应的价值,找一次最小值,复杂度会炸。

这里我卡了一下。。。然后发现,可以只初始化一次。虽然在不同时间做月饼的花费会因为订单时间的不同而不同,但是每相邻的两个小时之间做月饼花费的差是固定的,也就是花费的相对大小是固定的。

因此对于每个订单,我在相应的区间内找到花费最小的时间的下标,然后恢复成实际的花费(因为花费是一个等差数列,很好恢复)

由于之后给的花费是开始后的第i小时。。。那么订单不妨也转化成小时的形式。。。

注意判断闰年。。

2A,开心。

/* ***********************************************
Author :111qqz
Created Time :2016年05月18日 星期三 19时37分03秒
File Name :code/hdu/4122.cpp
************************************************ */
 1#include <cstdio>
 2#include <cstring>
 3#include <iostream>
 4#include <algorithm>
 5#include <vector>
 6#include <queue>
 7#include <set>
 8#include <map>
 9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
 1using namespace std;
 2const double eps = 1E-8;
 3const int dx4[4]={1,0,0,-1};
 4const int dy4[4]={0,-1,1,0};
 5const int inf = 0x3f3f3f3f;
 6const int N=1E5+7;
 7const int M[15]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //平年每月天数,1 indexed.
 8int n,m;
 9int T,S;
10pi o[N];
11int a[N];
12int dp[N][18];
13int toval(string mon)
14{
15    if (mon=="Jan") return 1;
16    if (mon=="Feb") return 2;
17    if (mon=="Mar") return 3;
18    if (mon=="Apr") return 4;
19    if (mon=="May") return 5;
20    if (mon=="Jun") return 6;
21    if (mon=="Jul") return 7;
22    if (mon=="Aug") return 8;
23    if (mon=="Sep") return 9;
24    if (mon=="Oct") return 10;
25    if (mon=="Nov") return 11;
26    if (mon=="Dec") return 12;
27    return -1; //不然总报 waring...
28}
29bool leap( int year)
30{
31    if (year0==0) return true;
32    if (year%4==0&&year0!=0) return true;
33    return false;
34}
35int tohour(int mon, int day,int year,int hour)
36{
37    int curday = 0 ;
38    for ( int i = 2000 ; i <year ;  i++ )
39    {
40	if (leap(i)) curday+=366;
41	else curday+=365;
42    }
43    for ( int i = 1 ; i < mon ; i ++) curday+=M[i];
44    if (leap(year)&&mon>2) curday++;
    curday = curday + day-1;

    return curday*24+hour+1;
}
1int _min(int l,int r)
2{
3  //  cout<<"ll:"<<l<<" rr:"<<r<<endl;
4    if (a[l]<a[r]) return l;
5    else return r;
6}
7void init_rmq()
8{
9    for ( int i = 1 ; i <= m ; i++) dp[i][0] = i;
1    for ( int j = 1 ; (1<<j)<= m ; j++)
2	for ( int i = 1 ; i+ (1<<j)-1 <= m ; i++)
3	    dp[i][j] = _min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
4}
 1int rmq_min(int l,int r)
 2{
 3  //  cout<<"l:"<<l<<" r:"<<r<<endl;
 4    int k = 0 ;
 5    l = max(l,1);
 6    while (1<<(k+1)<=r-l+1) k++;
 7  //  cout<<"k:"<<k<<endl;
 8    return _min(dp[l][k],dp[r-(1<<k)+1][k]);
 9}
10int main()
11{
12	#ifndef  ONLINE_JUDGE 
13	freopen("code/in.txt","r",stdin);
14  #endif
15	while (scanf("%d %d",&n,&m)!=EOF)
16	{
17	    if (n==0&&m==0) break;
18	    for ( int i = 1 ; i <= n ;  i++)
19	    {
20		string month;
21		int day,year,hour,num;
22		cin>>month;
23		scanf("%d %d %d %d",&day,&year,&hour,&num);
24		int val_month = toval(month);
25	//	cout<<"month:"<<val_month<<" day:"<<day<<" year:"<<year<<" hour:"<<hour<<endl;
26		o[i] = make_pair(tohour(val_month,day,year,hour),num);
27	    }
28//	    for ( int i = 1 ; i <= n ; i++) printf("%d %d\n",o[i].fst,o[i].sec);
29	    scanf("%d %d",&T,&S);
30	    for ( int i = 1 ; i <= m ; i++) scanf("%d",&a[i]);
1	    int cnt = 0 ;
2	    for ( int i = m ; i >=1 ; i--)
3	    {
4		a[i]+=cnt*S;
5		cnt++;
6	    }
//	    for ( int i = 1 ; i <= m ; i++) printf("%d\n",a[i]);
 1	    init_rmq();
 2	    LL ans = 0LL;
 3	    for ( int i = 1 ; i <= n ; i++)
 4	    {
 5		int maketime = rmq_min(o[i].fst-T,o[i].fst);
 6		//cout<<"maketime:"<<maketime<<endl;
 7		int cost = a[maketime]-(m-o[i].fst)*S;
 8		//cout<<"cost:"<<cost<<endl;
 9		ans = ans + LL(cost)*LL(o[i].sec);
10	    }
	    printf("%lld\n",ans);
	    


	}
1  #ifndef ONLINE_JUDGE  
2  fclose(stdin);
3  #endif
4    return 0;
5}kaixin