Luo Gu $ P4040 \ [AHOI2014 / JSOI2014] $ otaku plan greedy

Positive Solutions: greedy

Report problem solving:

Portal $ QwQ $

In fact, long before the winter break on the exam passed ,,, but then $ gql $ not properly implemented, will only wrote a half, and there is no dichotomy sets of three points, I only got $ 70pts $

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rp(i,x,y) for(register ll i=x;i<=y;++i)
#define my(i,x,y) for(register ll i=x;i>=y;--i)

const ll N=10000+10;
ll m,f,n,p[N],s[N],mnv[N*200],mxs,ans,sum[N*200];

inline ll read()
{
    char ch=getchar();ll x=0;bool y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=getchar();
    if(ch=='-')ch=getchar(),y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=getchar();
    return y?x:-x;
}
void fd(ll v,ll tot)
{
    ll l=0,r=mxs;
    while(l<r)
    {
        int mid=(l+r+1)>>1;
        if((ll)sum[mid]*tot<=(ll)v)l=mid;
        else r=mid-1;
    }
    ans=max((ll)ans,l*tot+(v-sum[l]*tot)/mnv[l+1]);
}
void work()
{
    for(register ll i=f,tot=1;i<=m;i+=f,++tot)fd(m-i,tot);
    printf("%lld\n",ans);
}

int main()
{
//    freopen("food.in","r",stdin);
//    freopen("food.out","w",stdout);
    m=read();f=read();n=read();
    memset(mnv,127/3,sizeof(mnv));mxs=ans=0;
    rp(i,1,n){p[i]=read();s[i]=read()+1;mnv[s[i]]=min(mnv[s[i]],p[i]);mxs=max(s[i],mxs);}
    my(i,mxs-1,1)mnv[i]=min(mnv[i+1],mnv[i]);rp(i,1,mxs)sum[i]=sum[i-1]+mnv[i];
    if(f==0){printf("%lld\n",m/mnv[1]);return 0;}work();
}
View Code

Then sets three-pointers to finish the $ QwQ $

Or eight children to write explanations lie $ QwQ $ (in fact, from the summary examination Quine over $ QwQ $

First, we can obviously handle it so that it takes all the items with shelf life is a monotonically increasing (monotonic wave stack go on into

Then enumerate several rounds takeaway point, we can calculate take-away fee, you can find half can survive for days

About half of it is easy to prove the correctness

First, we can conclude that the optimal number of days for each round must be close

I have to prove under

If now there are two options

The first round of the first n-Buy $ $ second round Buy $ n + 100 $ th

The second is the first round Buy $ n + 50 $ second round Buy $ n + 50 $ th

First, before the $ n $ to eat must be the same (apparently eat low-cost low shelf life, this need not prove lie ,,,?

That $ 50 $ then it must be one of the second and $ n $ th after that $ 50 $ a is the same as the second after the first round of the $ n $ th

$ 50 $ more we compare it becomes a rest first after the second round of $ n + a $ 50 $ 50 $ one second after the second round of the $ n $

Because we've been through with the process so that it takes a monotonically increasing shelf life

So, after buying a $ n + 50 $ $ 50 $ a certain cost less than $ n $ $ 50 $ after spending two months of

So we can prove that the optimal number of days either $ T $ is either $ T + 1 $

So we can half the $ T $, on the Eau

However, if this method of solving the problem is to make life difficult for the Los Valley of the $ QAQ $ only $ 70pts $ (like $ 80pts $ pot I wrote a $ QwQ $

Where should say Optimization

That is, look at the above solution, you will find the only place that can optimize my take-away rounds are enumerated

How to optimize it? Trichotomy

It can be shown round and survived for several days takeaway is a quadratic function

Now I come to prove at $ QwQ $

First, if you do not make money, this is a convex function. Then find the money, subtracted under a convex function

Save the convex convex convex still, so this is a convex function, may third $ QwQ $

And then later on the steps above the $ 70pts $ is the same spicy!

(By the way ,,, is actually divided to sets of two-thirds of the $ QwQ $. Because the two sets of three sub-branch burst like $ ll $ $ kk $

Guess you like

Origin www.cnblogs.com/lqsukida/p/11545128.html