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,开心。

  1/* ***********************************************
  2Author :111qqz
  3Created Time :2016年05月18日 星期三 19时37分03秒
  4File Name :code/hdu/4122.cpp
  5************************************************ */
  6
  7#include <cstdio>
  8#include <cstring>
  9#include <iostream>
 10#include <algorithm>
 11#include <vector>
 12#include <queue>
 13#include <set>
 14#include <map>
 15#include <string>
 16#include <cmath>
 17#include <cstdlib>
 18#include <ctime>
 19#define fst first
 20#define sec second
 21#define lson l,m,rt<<1
 22#define rson m+1,r,rt<<1|1
 23#define ms(a,x) memset(a,x,sizeof(a))
 24typedef long long LL;
 25#define pi pair < int ,int >
 26#define MP make_pair
 27
 28using namespace std;
 29const double eps = 1E-8;
 30const int dx4[4]={1,0,0,-1};
 31const int dy4[4]={0,-1,1,0};
 32const int inf = 0x3f3f3f3f;
 33const int N=1E5+7;
 34const int M[15]={-1,31,28,31,30,31,30,31,31,30,31,30,31}; //平年每月天数,1 indexed.
 35int n,m;
 36int T,S;
 37pi o[N];
 38int a[N];
 39int dp[N][18];
 40int toval(string mon)
 41{
 42    if (mon=="Jan") return 1;
 43    if (mon=="Feb") return 2;
 44    if (mon=="Mar") return 3;
 45    if (mon=="Apr") return 4;
 46    if (mon=="May") return 5;
 47    if (mon=="Jun") return 6;
 48    if (mon=="Jul") return 7;
 49    if (mon=="Aug") return 8;
 50    if (mon=="Sep") return 9;
 51    if (mon=="Oct") return 10;
 52    if (mon=="Nov") return 11;
 53    if (mon=="Dec") return 12;
 54    return -1; //不然总报 waring...
 55}
 56bool leap( int year)
 57{
 58    if (year0==0) return true;
 59    if (year%4==0&&year0!=0) return true;
 60    return false;
 61}
 62int tohour(int mon, int day,int year,int hour)
 63{
 64    int curday = 0 ;
 65    for ( int i = 2000 ; i <year ;  i++ )
 66    {
 67	if (leap(i)) curday+=366;
 68	else curday+=365;
 69    }
 70    for ( int i = 1 ; i < mon ; i ++) curday+=M[i];
 71    if (leap(year)&&mon>2) curday++;
 72
 73    curday = curday + day-1;
 74
 75    return curday*24+hour+1;
 76}
 77
 78int _min(int l,int r)
 79{
 80  //  cout<<"ll:"<<l<<" rr:"<<r<<endl;
 81    if (a[l]<a[r]) return l;
 82    else return r;
 83}
 84void init_rmq()
 85{
 86    for ( int i = 1 ; i <= m ; i++) dp[i][0] = i;
 87
 88    for ( int j = 1 ; (1<<j)<= m ; j++)
 89	for ( int i = 1 ; i+ (1<<j)-1 <= m ; i++)
 90	    dp[i][j] = _min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]);
 91}
 92
 93int rmq_min(int l,int r)
 94{
 95  //  cout<<"l:"<<l<<" r:"<<r<<endl;
 96    int k = 0 ;
 97    l = max(l,1);
 98    while (1<<(k+1)<=r-l+1) k++;
 99  //  cout<<"k:"<<k<<endl;
100    return _min(dp[l][k],dp[r-(1<<k)+1][k]);
101}
102int main()
103{
104	#ifndef  ONLINE_JUDGE 
105	freopen("code/in.txt","r",stdin);
106  #endif
107	while (scanf("%d %d",&n,&m)!=EOF)
108	{
109	    if (n==0&&m==0) break;
110	    for ( int i = 1 ; i <= n ;  i++)
111	    {
112		string month;
113		int day,year,hour,num;
114		cin>>month;
115		scanf("%d %d %d %d",&day,&year,&hour,&num);
116		int val_month = toval(month);
117	//	cout<<"month:"<<val_month<<" day:"<<day<<" year:"<<year<<" hour:"<<hour<<endl;
118		o[i] = make_pair(tohour(val_month,day,year,hour),num);
119	    }
120//	    for ( int i = 1 ; i <= n ; i++) printf("%d %d\n",o[i].fst,o[i].sec);
121	    scanf("%d %d",&T,&S);
122	    for ( int i = 1 ; i <= m ; i++) scanf("%d",&a[i]);
123
124	    int cnt = 0 ;
125	    for ( int i = m ; i >=1 ; i--)
126	    {
127		a[i]+=cnt*S;
128		cnt++;
129	    }
130
131//	    for ( int i = 1 ; i <= m ; i++) printf("%d\n",a[i]);
132
133	    init_rmq();
134	    LL ans = 0LL;
135	    for ( int i = 1 ; i <= n ; i++)
136	    {
137		int maketime = rmq_min(o[i].fst-T,o[i].fst);
138		//cout<<"maketime:"<<maketime<<endl;
139		int cost = a[maketime]-(m-o[i].fst)*S;
140		//cout<<"cost:"<<cost<<endl;
141		ans = ans + LL(cost)*LL(o[i].sec);
142	    }
143
144	    printf("%lld\n",ans);
145
146
147
148	}
149
150  #ifndef ONLINE_JUDGE  
151  fclose(stdin);
152  #endif
153    return 0;
154}kaixin