Otaku plan: unimodal function thirds

Description

Since fascinated by puzzles, JYY becomes a thorough otaku. In order to solve the problem of food and clothing, JYY had to rely on takeout to maintain their livelihood.

A total takeaways n kinds of food, respectively, by the n 1 number. First i kinds of food have a fixed price Pi and shelf life of Si . First i kinds of food in Si will expire days. JYY not eaten in food. For example, if today JYY ordered a shelf life of 0 -day food, then JYY must put this food to eat tomorrow or today, otherwise the food can no longer eat. Shelf life can be a day, so this food must be eaten on the day of purchase.

JYY now has M dollars every takeout food delivery need to pay extra delivery fee Small-brother F yuan. Food delivery brother able-bodied, you can instantly bring any food to more than JYY.

JYY want to know, in the case of at least able to eat a meal a day to meet not expired takeaway, he can house up to how many days?

Topic Link

First, this question strange and boring with double the range of data needed to calculate costs, and then some special data calculation data will burst long long.

Is relatively easy to find all the money spent on the outside part of the delivery fee, the other part is to buy take-away.

So if we have determined how many times we take-away points, then you can take the optimal decision.

The number of take-away too many bad points, too much money to spend on a tip.

The number of take-away point is not too good, because that might limit the shelf life and are forced to buy the more expensive takeaway.

Wonderful unimodal function in nature.

Specific Why can see that it is unimodal function. . emm, first, intuition, the second is experience.

It appears to be right, and there is no counter-examples ah, that is right now. . . Well, then it is right.

The third function is to substantially routine is split into three parts, take to narrow end section 4 according to the magnitude relation.

Currently interval endpoint is l, r, you have to separate the two new endpoints for the ml, mr.

Two Kinds of Method: ml = l + (rl) / 3; mr = l + (rl) * 2/3; or ml = l + r >> 1; mr = ml + 1;

Essentially the same, the complexity of the latter is lower base 2 logarithm, the bottom former is 1.5.

Anyway, we have to function mess paragraph 3, and then what?

We have to determine the maximum (minimum) value has been [l, r] again, and then only three cases (for example the maximum value in order):

1) In [l, ml] in, then there is f (ml)> f (mr)> f (r)

2) In [ml, mr] in, then there is f (ml)> f (mr)> f (r) or f (l) <f (ml) <f (mr)

3) [mr, r] in, then there is f (l) <f (ml) <f (mr)

So if we can find satisfying f (l) <f (ml) <f (mr) then the answer is certainly not [l, ml] in

If the condition f (ml)> f (mr)> f (r) is not necessarily the answer [mr, r] in.

And f (l), f (r) of the comparison does not make sense, so we are concerned about is f (ml), f (mr) the size of the relationship

Depending on their size relationship we can answer part of the narrow range.

To prevent the interval can be reduced, we want to make sure the size of the interval is at least 4 (l <r-2), and finally to take the max in the three possible values.

1 while(l<r-2){
2     ml=l+r>>1;mr=ml+1;
3     if(f(ml)<f(mr)) l=ml;
4     else r=mr;
5 }
6 ans=max(max(f(l),f(l+1)),f(r));
Roughly one-third frame

Then the remaining question is the decision-making / calculated.

Obviously, the cheapest takeaway is certainly buy, short high price we would not buy a shelf life of those.

So we have takeaway sorted by price, same price, leaving only the longest shelf life, high prices exclude short shelf life.

Then we have continued to buy the most expensive on it, the amount of money to buy is (the shelf - a shelf life) × price × number of take-away point (may burst long long)

So let's subtract Delivery fees in the total amount of money m, the rest of the money continues to take such decisions, if not a certain kind of sold out so you can buy several buy several.

This can be calculated for days. Sets of three-pointers, to get.

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 #define int long long
 5 struct ps{int w,t;friend bool operator<(ps a,ps b){return a.w<b.w;}}lp[205],p[205];
 6 int n,m,f,cnt,mx;
 7 int Q(int tms){
 8     int lft=m-f*tms;
 9     for(int i=1;i<=cnt;++i)
10         if(lft/p[i].w>=1.0*tms*(p[i].t-p[i-1].t)){
11             if(i==cnt)return p[i].t*tms;
12             lft-=(p[i].t-p[i-1].t)*p[i].w*tms;
13         }
14         else return p[i-1].t*tms+lft/p[i].w;
15 }
16 signed main(){
17     scanf("%lld%lld%lld",&m,&f,&n);
18     for(int i=1;i<=n;++i)scanf("%lld%lld",&lp[i].w,&lp[i].t),lp[i].t++;
19     sort(lp+1,lp+1+n);
20     for(int i=1;i<=n;++i)if(lp[i].t>mx)p[++cnt]=lp[i],mx=lp[i].t;
21     int l=1,r=m/f,ml,mr,lans,rans;
22     while(l<r-2){
23         ml=l+r>>1;mr=ml+1;
24         lans=Q(ml);rans=Q(mr);
25         if(lans<rans)l=ml;else r=mr;
26     }
27     printf("%lld\n",max(max(Q(l),Q(r)),l+1<=r?Q(l+1):0));
28 }
The complete code 811B

 

Guess you like

Origin www.cnblogs.com/hzoi-DeepinC/p/11425675.html