单调队列-Hdu-4122-Alice's mooncake shop
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4122
题目意思:
一家月饼店,有n个订单,从2001年1月1日0时开始24小时营业开m个小时,且每个时间点做一个月斌的花费不一样,每个订单由时间(年月日时)定月饼数量组成。店主在每个整时点都可以做月饼,并且做月饼的时间可以忽略。每个月饼有保质期t,保存每个月饼每小时需花费s。求完成所有订单,最少的花费。
解题思路:
1、先算出每个订单的小时点。
2、对每个订单时间点i,很显然花费min(cost[j]+(i-j)*s)(i-j<=t)最划算。cost[j]+(i-j)*s=cost[j]-j*s+i*s,所以对于每个i,求出前面的满足j>=i-t,的cost[j]-j*s的最小值即可,很显然用单调队列可以维护。
代码:
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<ctime>
#define eps 1e-6
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define ll __int64
#define lson l,m,(rt<<1)
#define rson m+1,r,(rt<<1)|1
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; //0表示平年,1表示闰年
int days[2][13]={0,31,28,31,30,31,30,31,31,30,31,30,31,
0,31,29,31,30,31,30,31,31,30,31,30,31};
int dd[2]={365,366}; //每年的天数
map<string,int>myp;
string mon;
#define Maxn 2700
#define Maxm 110000
ll pp[Maxn],hh[Maxn];//订单的月饼数量和时间点
ll q[Maxm],cost[Maxm];//花费
ll Mi[Maxm];//Mi[i]表示时间点i时的最小花费单价
ll n,m; bool isleap(int y) //是否为闰年
{
if((y%4==0&&y%100)||(y%400==0))
return true;
return false;
} ll Cal(int y,int m,int d,int h) //计算距离2000年1月1日0时的小时数,从开始标记
{
ll res=0;
for(int i=2000;i<y;i++) //计算前面的年份
res+=dd[isleap(i)]*24;
int is=isleap(y);
for(int i=1;i<m;i++) //计算当前年的前面月的天数
res+=days[is][i]*24;
res+=(d-1)*24;//当月的小时数
res+=(h+1); //从1小时开始记
return res; } int main()
{
myp["Jan"]=1,myp["Feb"]=2,myp["Mar"]=3,myp["Apr"]=4;
myp["May"]=5,myp["Jun"]=6,myp["Jul"]=7,myp["Aug"]=8;
myp["Sep"]=9,myp["Oct"]=10,myp["Nov"]=11,myp["Dec"]=12;
int mon,day,yea,hour; while(scanf("%I64d%I64d",&n,&m)&&n+m)
{
for(int i=1;i<=n;i++)
{
string a;
cin>>a>>day>>yea>>hour>>pp[i];
hh[i]=Cal(yea,myp[a],day,hour);
}
ll t,s; scanf("%I64d%I64d",&t,&s);
for(int i=1;i<=m;i++)
{
scanf("%I64d",&cost[i]);
cost[i]-=i*s; //需要维护的值
} int head=1,tail=0; //从1开始标号 // printf("%I64d\n",m);
for(int i=1;i<=m+1;i++)
{
while(head<=tail&&cost[q[tail]]>=cost[i])
tail--;
q[++tail]=i;
while(head<=tail&&q[head]+t<i)
head++;
Mi[i]=cost[q[head]]+i*s;
}
//printf(":%I64d\n",Mi[10]);
ll ans=0;
for(int i=1;i<=n;i++)
ans+=Mi[hh[i]]*pp[i];
printf("%I64d\n",ans);
//printf("%I64d\n",Mi[hh[i]]*pp[i]); }
return 0;
}
单调队列-Hdu-4122-Alice's mooncake shop的更多相关文章
- hdu 4122 Alice's mooncake shop(单调队列)
题目链接:hdu 4122 Alice's mooncake shop 题意: 有n个订单和可以在m小时内制作月饼 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接 ...
- HDU 4122 Alice's mooncake shop 单调队列优化dp
Alice's mooncake shop Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- HDU 4122 Alice's mooncake shop (RMQ)
Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- HDU 4122 Alice's mooncake shop (单调队列/线段树)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4122 题意:好难读懂,读懂了也好难描述,亲们就自己凑合看看题意把 题解:开始计算每个日期到2000/1/ ...
- HDU 4122 Alice's mooncake shop
单调队列,裸的!!坑死了,说好的“All the orders are sorted by the time in increasing order. 呢,我就当成严格上升的序列了,于是就各种错.测试 ...
- HDU 4122 Alice's mooncake shop --RMQ
题意: 一个月饼店做月饼,总营业时间m小时,只能在整点做月饼,可以做无限个,不过在不同的时间做月饼的话每个月饼的花费是不一样的,假设即为cost[i],再给n个订单,即为在某个时间要多少个月饼,时间从 ...
- 【HDOJ】4122 Alice's mooncake shop
RMQ的基础题目,简单题. /* 4122 */ #include <iostream> #include <sstream> #include <string> ...
- Alice's mooncake shop HDU - 4122 单调队列
题意: 有n个订单和可以在m小时内制作月饼,制作月饼不考虑时间(即,你可以在一个时刻在所有需要的月饼都做完) 接下来是n个订单的信息:需要在mon月,d日,year年,h小时交付订单r个月饼 接下来一 ...
- hdu 4122 Alice's mooncake shop (线段树)
题目大意: 一个月饼店每一个小时做出月饼的花费不一样. 储存起来要钱.最多存多久.问你把全部订单做完的最少花费. 思路分析: ans = segma( num[]*(cost[] + (i-j)*s) ...
- HDU 4122
Alice's mooncake shop Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- Android Fragment详解(六):Fragement示例
把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...
- 从点亮一个LED开始,Cortex-A9裸机程序设计
电路原理图: 如何点亮一个LED? 通过对原理图进行分析,我们能够发现给三极管的基极加上一个高点平时,三级管be结导通构成通路,此时二极管就点亮了.若要将LED熄灭只需取消高电平输出. 如何使三级管基 ...
- mongodb.open失效导致访问地址404
今天做编辑文章功能的时候发现一个问题,编辑并保存完成后再次跳转到当前文章所在的地址,结果报404,打断点发现查询数据库的时候mongodb.open方法失效.百度后找到了原因: 编辑保存的时候打开了数 ...
- [转]iOS hybrid App 的实现原理及性能监测
转自:http://www.cocoachina.com/ios/20151118/14270.html iOS hybrid App 的实现原理及性能监测 2015-11-18 11:39 编辑: ...
- <经验杂谈>查询表结构的SQL语句
在我们使用SQL数据库的过程中,经常会遇到查询表结构的情况,以下就是sql语句的写法: --查询非系统数据库 SELECT name FROM Master..SysDatabases 查询数据库下所 ...
- 限制内容长度(CSS,jQuery)
CSS(宽度限制在100px之内,超出就会点点点) <style type="text/css"> p{width: 100px;display: inline-blo ...
- Cogs 12 运输问题2 (有上下界网络流)
#include <cstdlib> #include <algorithm> #include <cstring> #include <iostream&g ...
- poj3090欧拉函数求和
E - (例题)欧拉函数求和 Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB ...
- hdu2588 gcd 欧拉函数
GCD Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu4355 三分
F - 三分 Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:32768KB 64bit I ...