zoukankan      html  css  js  c++  java
  • HDU 4122 Alice's mooncake shop 题解

    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.

    InputThe 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.OutputYou 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.
    

     思路:先把每天的代价c 统一变成 c - i * S的形式,在新的价值定义下相对花费没有变,这样用个单调队列维护一下就行了,取的时候再转换成绝对花费,复杂度O(n)。

      1 #include <iostream>
      2 #include <fstream>
      3 #include <sstream>
      4 #include <cstdlib>
      5 #include <cstdio>
      6 #include <cmath>
      7 #include <string>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <queue>
     11 #include <stack>
     12 #include <vector>
     13 #include <set>
     14 #include <map>
     15 #include <list>
     16 #include <iomanip>
     17 #include <cctype>
     18 #include <cassert>
     19 #include <bitset>
     20 #include <ctime>
     21 
     22 using namespace std;
     23 
     24 #define pau system("pause")
     25 #define ll long long
     26 #define pii pair<int, int>
     27 #define pb push_back
     28 #define mp make_pair
     29 #define clr(a, x) memset(a, x, sizeof(a))
     30 
     31 const double pi = acos(-1.0);
     32 const int INF = 0x3f3f3f3f;
     33 const int MOD = 1e9 + 7;
     34 const double EPS = 1e-9;
     35 
     36 /*
     37 #include <ext/pb_ds/assoc_container.hpp>
     38 #include <ext/pb_ds/tree_policy.hpp>
     39 
     40 using namespace __gnu_pbds;
     41 tree<pli, null_type, greater<pli>, rb_tree_tag, tree_order_statistics_node_update> T;
     42 */
     43 
     44 const int md[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
     45 int yd[22];
     46 bool is_leap(int y) {
     47     return y % 4 == 0;
     48 }
     49 int get_mon(char s[]) {
     50     if (!strcmp("Jan", s)) return 1;
     51     if (!strcmp("Feb", s)) return 2;
     52     if (!strcmp("Mar", s)) return 3;
     53     if (!strcmp("Apr", s)) return 4;
     54     if (!strcmp("May", s)) return 5;
     55     if (!strcmp("Jun", s)) return 6;
     56     if (!strcmp("Jul", s)) return 7;
     57     if (!strcmp("Aug", s)) return 8;
     58     if (!strcmp("Sep", s)) return 9;
     59     if (!strcmp("Oct", s)) return 10;
     60     if (!strcmp("Nov", s)) return 11;
     61     if (!strcmp("Dec", s)) return 12;
     62 }
     63 int sum[2][13], n, m, t, s, cost[100015];
     64 struct gg {
     65     int t, r;
     66     gg () {}
     67     gg (int t, int r) : t(t), r(r) {}
     68 } g[2515];
     69 deque<int> que;
     70 int main() {
     71     for (int i = 1; i <= 12; ++i) sum[0][i] = sum[0][i - 1] + md[i];
     72     sum[1][1] = sum[0][1];
     73     for (int i = 2; i <= 12; ++i) sum[1][i] = sum[0][i] + 1;
     74     for (int i = 1; i <= 20; ++i) yd[i] = yd[i - 1] + (is_leap(2000 + i - 1) ? 366 : 365);
     75     while (~scanf("%d%d", &n, &m) && n + m) {
     76         ll ans = 0;
     77         for (int i = 1; i <= n; ++i) {
     78             char s[5];
     79             int d, y, h, r, tt;
     80             scanf("%s%d%d%d%d", s, &d, &y, &h, &r);
     81             tt = yd[y - 2000];
     82             int f = is_leap(y), m = get_mon(s);
     83             tt += sum[f][m - 1];
     84             tt += d - 1;
     85             tt = tt * 24 + h + 1;
     86             g[i] = gg(tt, r);
     87         }
     88         scanf("%d%d", &t, &s);
     89         for (int i = 1; i <= m; ++i) {
     90             scanf("%d", &cost[i]);
     91             cost[i] -= i * s;
     92         }
     93         while (que.size()) que.pop_back();
     94         for (int i = 1, j = 1; i <= n && j <= m; ++i) {
     95             int tt = g[i].t, r = g[i].r;
     96             while (j <= tt && j <= m) {
     97                 while (que.size() && cost[que.back()] >= cost[j]) que.pop_back();
     98                 que.push_back(j++);
     99             }
    100             while (que.front() < tt - t) que.pop_front();
    101             int x = que.front();
    102             ans += r * (cost[x] + (ll)tt * s);
    103         }
    104         printf("%I64d
    ", ans);
    105     }
    106     return 0;
    107 }
    108 /*
    109 10 10
    110 Sep 3 2010 22 1
    111 Mar 4 2008 0 1
    112 */
  • 相关阅读:
    年轻人要明白,职场里不只有晋升
    为什么我建议年轻人多出去走走?
    聊聊所谓的弹性工作制
    从零开始搭建Java开发环境第四篇:精选IDEA中十大提高开发效率的插件!
    从零开始搭建Java开发环境第三篇:最新版IDEA常用配置指南,打造你的最酷IDE
    ORACLE日期时间函数大全
    我做的 地税信息中心设备台账
    IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?
    分析
    微信端的界面与后台之间的接口稳定性压力测试用什么工具比较好点
  • 原文地址:https://www.cnblogs.com/BIGTOM/p/8492770.html
Copyright © 2011-2022 走看看