hdu 4122 Alice's mooncake shop(單調隊列),hdumooncake

來源:互聯網
上載者:User

hdu 4122 Alice's mooncake shop(單調隊列),hdumooncake

題目連結:hdu 4122 Alice's mooncake shop

題目大意:給定N和M,表示有N個訂單,M個時刻可以做月餅,時刻以小時計算,任意時刻可以做若干個月餅。接著

N行為N個訂單的資訊,包括時間和數量。再給定T和S,表示每個月餅的保質時間和每儲存一小時的開銷。然後M行為

對應每個時刻製作月餅的代價。問說最少花費多少代價完成所有訂單。

解題思路:單調隊列或者RMQ,單調隊列即用一個deque維護一個代價遞增的隊列,每次將頭部保質期不夠的剔除。

RMQ可以將預先處理處每個區間時刻代價的最小值,需要加上儲存的代價,然後對於每個訂單只要查詢單前位置向前T

小時以內的最小值即可。

#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const char month[15][10] = {"", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"};const int day[15] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};typedef pair<int,int> pii;typedef long long ll;int N, M;deque<pii> Q;queue<pii> G;inline int get_month(char* s) {    for (int i = 1; i <= 11; i++)        if (strcmp(s, month[i]) == 0)            return i;    return 12;}inline bool Leap_year(int x) {    return (x % 4 == 0 && x % 100) || x % 400 == 0;}int get_time() {    char mon[10];    int y, d, h, m;    scanf("%s%d%d%d", mon, &d, &y, &h);    m = get_month(mon);    int ret = 0;    for (int i = 2000; i < y; i++)        ret += Leap_year(i) ? 366 : 365;    for (int i = 1; i < m; i++) {        if (i == 2 && Leap_year(y))            ret += 29;        else            ret += day[i];    }    ret += d - 1;    return ret * 24 + h;}void init () {    int ti, x;    Q.clear();    for (int i = 0; i < N; i++) {        ti = get_time();        scanf("%d", &x);        G.push(make_pair(x, ti));    }}ll solve () {    int S, T, x;    ll ret = 0;    scanf("%d%d", &T, &S);    for (int i = 0; i < M; i++) {        scanf("%d", &x);        while (!Q.empty() && x <= Q.back().first + (i - Q.back().second) * S)            Q.pop_back();        Q.push_back(make_pair(x, i));        while (!G.empty() && i == G.front().second) {            while (!Q.empty() && Q.front().second + T < i)                Q.pop_front();            ret += (Q.front().first + (i - Q.front().second) * S) * G.front().first;            G.pop();        }    }    return ret;}int main () {    while (scanf("%d%d", &N, &M) == 2 && N + M) {        init();        printf("%I64d\n", solve());    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.