description
Takeaways a total of N kinds of food, there are 1 to N number. The first kind of food i have a fixed price and shelf life of Pi Si. I-Si kinds of food expire in days. JYY not eaten in food.
For example, if today JYY ordered a shelf life of one day's food, then JYY it must be today or
tomorrow who eat the food, otherwise food can no longer eat. Shelf life can be 0 days,
like this food must be eaten in 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
case meet at least every day can eat take-away meal not expired, he can house up to how many days?
analysis
First, get rid of junk food, that is more expensive than other high shelf life is shorter, and certainly do not buy; the remaining price and shelf life are monotonically increasing, the higher the price the longer the shelf life of certain
The number of people to consider the impact of food delivery for the answer, it is easy to know too many times or too little is not good, take-away food costs or expensive, but in fact the relationship between the number of answers to a single peak function
The number of one-third food delivery people to the \ (x \) , and then determine to \ (x \) times the longest continued life; every room, are greedy money evenly, then consider only one room of the answer then by \ (x \)
For just a way to start to buy food to eat, they are actually buying the shortest shelf life of several servings, enough shelf-life, the second buy several copies of the short shelf life, and so on
This course \ (O (n) \) to do well; there is little money left over, you greedy considering the fight continued a few days later in which more than once to buy a meal; a general two-thirds greedy
code
#pragma GCC optimize("O3")
#pragma G++ optimize("O3")
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define MAXN 205
#define ll long long
#define reg register ll
#define fo(i,a,b) for (reg i=a;i<=b;++i)
#define fd(i,a,b) for (reg i=a;i>=b;--i)
using namespace std;
ll n,fee,tot,money;
bool bz[MAXN];
struct node
{
ll x,y,id;
}a[MAXN],b[MAXN];
inline ll read()
{
ll x=0,f=1;char ch=getchar();
while (ch<'0' || '9'<ch){if (ch=='-')f=-1;ch=getchar();}
while ('0'<=ch && ch<='9')x=x*10+ch-'0',ch=getchar();
return x*f;
}
inline bool cmp(node a,node b){return a.x<b.x;}
inline ll check(ll x)
{
ll rest=money-x*fee,ave=rest/x,left=rest-ave*x,rec,day=0,ans=0;
fo(i,1,n)
{
if (a[i].y>=day && a[i].x<=ave)
{
ll tmp=min(a[i].y-day+1,ave/a[i].x);
ave-=a[i].x*tmp,day+=tmp;
}
rec=i;
if (a[i].x>ave)break;
}
left+=ave*x;
fo(i,rec,n)
{
if (a[i].y>=day && a[i].x<=left)
{
ll tmp=left/a[i].x;
left-=tmp*a[i].x,ans+=tmp;
}
if (ans)break;
}
return day*x+ans;
}
int main()
{
freopen("food.in","r",stdin);
freopen("food.out","w",stdout);
money=read(),fee=read(),n=read();
fo(i,1,n)a[i].x=read(),a[i].y=read(),a[i].id=i;
memset(bz,1,sizeof(bz));
fo(i,1,n)fo(j,1,n)if (i!=j && a[j].x<=a[i].x && a[j].y>=a[i].y){bz[i]=0;break;}
fo(i,1,n)if (bz[i])b[++tot]=a[i];n=tot;
sort(b+1,b+n+1,cmp);fo(i,1,n)a[i]=b[i];
ll l=1,r=fee?money/fee:money+1,midl,midr;
while (l<r)midl=l+(r-l)/3,midr=r-(r-l)/3,check(midl)>=check(midr)?r=midr-1:l=midl+1;
printf("%lld\n",check(l));
return 0;
}