HDU 4122 Alice's mooncake shop // RMQ 线段树

题目描述

HDU 4122 Alice’s mooncake shop

解题思路

题目大意:
有一家24小时营业的月饼店, 会连续营业m个小时, 且月饼每个小时的单价会浮动.在第i个小时会有一份订单.订单可以现做,也可以提前做好保存在冰箱里,(放在冰箱里每小时会花费一定的费用,且月饼有保质期为T).问在满足所有订单的前提下,最少的制作费用是多少?

抽象出来就是,对于第i个小时的订单. 查询区间 [i-T, i] 这段时间内的制作最小值即可.

参考代码

#include 
#include 
#include 
#include 
#define lson rt<<1
#define rson rt<<1|1
#define mid ((l+r)>>1)

using namespace std;

const int MAX_N = 2510;
const int MAX_NN = 100010;
const int inf = 0x7fffffff;
typedef __int64 ll;
map<string, int> mp;

struct Order {
    int year, day, hour, num, h;
    string month;
} order[MAX_N];

struct SegTree {
    ll cost;
} node[MAX_NN << 2];

int mon[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
int price[MAX_NN], k;
inline int max(int a, int b){return a>b?a:b;}
inline int min(int a, int b){return ainline bool isleap(int year) {return ((year%4 == 0 && year%100 != 0) || year%400 == 0);}
inline void pushup(int rt) {node[rt].cost = min(node[lson].cost, node[rson].cost);}

//月份映射
void init() {
    k = 1;
    mp["Jan"] = 1;    mp["Feb"] = 2;
    mp["Mar"] = 3;    mp["Apr"] = 4;
    mp["May"] = 5;    mp["Jun"] = 6;
    mp["Jul"] = 7;    mp["Aug"] = 8;
    mp["Sep"] = 9;    mp["Oct"] = 10;
    mp["Nov"] = 11;   mp["Dec"] = 12;
}

// 建树
void build(int l, int r, int rt) {
    if (l == r) {
        node[rt].cost = price[k++];
        return ;
    }
    build(l, mid, lson);
    build(mid+1, r, rson);
    pushup(rt);
}

// 查询区间[L, R]的最小值
ll query(int L, int R, int l, int r, int rt) {
    if (L <= l && r <= R) return node[rt].cost;
    ll ret = inf;
    if (L <= mid)   ret = min(ret, query(L, R, l, mid, lson));
    if (R > mid)    ret = min(ret, query(L, R, mid+1, r, rson));
    return ret;
}

// 根据输入的日期转换成小时
int cal_hour(int year, string month, int day, int hour) { 
    mon[2] = (isleap(year) ? 29 : 28);
    int ans = hour + (day-1) * 24 + 1;
    for (int i = mp[month]-1; i >= 1; --i)
        ans += mon[i] * 24;
    year--;
    while (year >= 2000) {
        ans += (365 * 24 + (isleap(year) ? 24 : 0));
        year--;
    }
    return ans;
}

int main() {
    int n, m, S, T;
    while (~scanf("%d %d", &n, &m) && (n || m)) {
        init();
        for (int i = 1; i <= n; ++i) {
            cin >> order[i].month;
            scanf("%d %d %d %d", &order[i].day, &order[i].year, &order[i].hour, &order[i].num);
            order[i].h = cal_hour(order[i].year, order[i].month, order[i].day, order[i].hour);
        }
        scanf("%d %d", &T, &S);
        for (int i = 1; i <= m; ++i) {
            scanf("%d", &price[i]);
            price[i] += S * (m - i); 
            // price[i]表示的是从第i个小时保存到第m个小时(即最后一个小时)的花费 
            //(即 price[i] = 第i个小时的花费 + 保存在冰箱(m-i)个小时的花费)
        }
        build(1, m, 1);
        ll ans = 0;
        for (int i = 1; i <= n; ++i) {
            int l = max(1, order[i].h - T), r = order[i].h;
            ll p = query(l, r, 1, m, 1) - (m - order[i].h) * S;
            // p还要减去后面的是因为订单在第i个小时就要了
            // 那么,第i个小时之后都不用保存在冰箱了,要减去那部分的花费
            ans += p * order[i].num;
        }
        printf("%I64d\n", ans);
    }
    return 0;
}

你可能感兴趣的:(HDU 4122 Alice's mooncake shop // RMQ 线段树)