天天看點

POJ_4002 && HDU_4122 Alice's mooncake shop(RMQ)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): 3641    Accepted Submission(s): 927

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.

POJ_4002 && HDU_4122 Alice's mooncake shop(RMQ)Alice's mooncake shop

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.

   
    
        

Source 2011 Asia Fuzhou Regional Contest  

題意:

Alice開了一個24小時營業的月餅店,從2000年1月1号0點開始營業,營業M個小時。

Alice隻在整點時刻做月餅,做月餅不花時間。

每一個時刻做月餅的成本不一樣,每個月餅的保存期限為T,月餅的存儲費用是每塊月餅每小時花費S。

現在又N個訂單,問完成這些訂單需要花費的最少成本。

分析:

RMQ預處理即可。此題關鍵是對表達式化簡,假設點餐時間是nowT,若在 i 時間做月餅,那麼一個月餅的花費是 cost[i] + (nowT - i) * S,化簡得(cost[i] - i * S) + nowT * S,也就是說花費隻随 cost[i] 以及 i 變化。是以容易想到用RMQ預處理出區間的最小值,然後對于一個訂單看其可做月餅的時間區間的最小值即可。

值得一提的是,HDU的資料太水了,可以枚舉該訂單的可做月餅的時間區間,直接暴力過。

題目連結:http://poj.org/problem?id=4002

代碼清單:

/*******************************************************************************
 *** problem ID  : HDU_4122.cpp
 *** create time : Mon Nov 09 21:57:01 2015
 *** author name : nndxy
 *** author blog : http://blog.csdn.net/jhgkjhg_ugtdk77
 *** author motto: never loose enthusiasm for life, life is to keep on fighting!
 *******************************************************************************/

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <vector>
#include <cctype>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <bits/stdc++.h>

using namespace std;

#define exit() return 0

typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;

const int pose = 20;
const int maxn = 2500 + 5;
const int maxm = 100000 + 5;
const ll INF = 1e18 + 5;

char sm[15][15] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
int md[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

struct Time{
    int month, date, year, H, R;
};

char str[5];
Time order[maxn];
int mc[maxm];
int T, S;
int N, M;

ll minq[maxm][pose];

int get_month(char *s){
    for(int i = 1; i <= 12; i++){
        if(strcmp(s, sm[i]) == 0) return i;
    }
}

void input(){
    for(int i = 1; i <= N; i++){
        scanf("%s", str);
        scanf("%d%d", &order[i].date, &order[i].year);
        scanf("%d%d", &order[i].H, &order[i].R);
        order[i].month = get_month(str);
    }
    scanf("%d%d", &T, &S);
    for(int i = 1; i <= M; i++){
        scanf("%d", &mc[i]);
    }

}

int get_hour(Time ord){
    int hour = 0;
    for(int i = 2000; i < ord.year; i++){
        if(i % 4 == 0){
            hour += 366 * 24;
        }
        else hour += 365 * 24;
    }
    for(int i = 1; i < ord.month; i++){
        hour += md[i] * 24;
    }
    hour += (ord.date - 1) * 24 + ord.H + 1;
    if(ord.year % 4 == 0 && ord.month > 2) hour += 24;
    return hour;
}

void RMQ_ST(){
	//memset(minq, INF, sizeof(minq));

	int L = (int) (log(M) / log(2.0));

	for(int i = 1; i <= M; i++){
		minq[i][0] = (ll)mc[i] - (ll)i * (ll)S;
	}

	for(int j = 1; j <= L; j++){
		for(int i = 1; i + (1 << j) - 1 <= M; i++){
			minq[i][j] = min(minq[i][j - 1], minq[i + (1 << (j - 1))][j - 1]);
		}
	}
}

void solve(){
	RMQ_ST();
    ll sumcost = 0;
    for(int i = 1; i <= N; i++){
        int startH, endH;
        int nowH = get_hour(order[i]);
        if(nowH > M){
            endH = M;
            if(nowH - T <= 0) startH = 1;
            else startH = nowH - T;
        }
        else{
            endH = nowH;
            if(nowH - T <= 0) startH = 1;
            else startH = nowH - T;
        }
        int mi = (int) ((log(endH - startH + 1.0)) / (log(2.0)));
        ll cost = (min(minq[startH][mi], minq[endH - (1 << mi) + 1][mi]) + nowH * S) * order[i].R;
        
       	sumcost += cost;
    }
    printf("%I64d\n", sumcost);
}

int main(){
    while(scanf("%d%d", &N, &M) != EOF && N && M){
        input();
        solve();
    }   exit();
}
           

繼續閱讀