HDU 4122 Alice's mooncake shop (line segment tree)

Source: Internet
Author: User

Question:

The cost of mooncakes made by a mooncake store varies by hour.

How long does it take to save it for money. Ask you the minimum cost for completing all orders.


Train of Thought Analysis:

Ans = segma (Num [] * (cost [] + (I-j) * s ))

After sorting it out, we will find that the formula is

Cost []-J * s + I * s

For each order, we take out the I for analysis.

Therefore, we use cost-J * s to build a new one.

Find the minimum cost during storage.


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#include <string>#define lson num<<1,s,mid#define rson num<<1|1,mid+1,e#define maxn 2555#define maxm 100005#define inf 0x3f3f3f3fusing namespace std;typedef long long LL;int n,m;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}};string tab[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};LL tre[maxm<<2];int getmonth(string x){    for(int i=1;i<=12;i++)    if(x==tab[i])return i;}bool leap(int x){    if(((x%4==0) && x%100!=0) || x%400==0)return true;    return false;}LL gethour(int month,int day,int year,int hour){    LL res=day-1;    int is=leap(year);    for(int i=1;i<month;i++)res+=days[is][i];    for(int i=2000;i<year;i++)    res+=365+leap(i);    res*=24;    res+=hour+1;    return res;}void build(int num,int s,int e){    tre[num]=inf;    if(s==e)return;    int mid=(s+e)>>1;    build(lson);    build(rson);}void update(int num,int s,int e,int pos,LL val){    if(s==e)    {        tre[num]=val;        return;    }    int mid=(s+e)>>1;    if(pos<=mid)update(lson,pos,val);    else update(rson,pos,val);    tre[num]=min(tre[num<<1],tre[num<<1|1]);}LL query(int num,int s,int e,int l,int r){    if(l<=s && r>=e)    {        return tre[num];    }    int mid=(s+e)>>1;    if(r<=mid)return query(lson,l,r);    else if(l>mid)return query(rson,l,r);    else return min(query(lson,l,mid),query(rson,mid+1,r));}string tmp;LL num[maxn];LL cost[maxm];LL time[maxm];int main(){    while(cin>>n>>m)    {        if(n==0 && m==0)break;        for(int i=1;i<=n;i++)        {            int d,y,h,Num;            cin>>tmp;            cin>>d>>y>>h>>Num;            num[i]=Num;            time[i]=gethour(getmonth(tmp),d,y,h);        }        LL S,T;        build(1,1,m);        cin>>T>>S;        for(int i=1;i<=m;i++)        {            cin>>cost[i];            cost[i]-=i*S;            update(1,1,m,i,cost[i]);        }        LL ans=0;        for(int i=1;i<=n;i++)        {            if(time[i]>m)break;            ans+=num[i]*(query(1,1,m,max(1LL,time[i]-T+1),time[i])+time[i]*S);        }        cout<<ans<<endl;    }    return 0;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.