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

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.

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
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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

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();
}



你可能感兴趣的:(Regional,数据结构)