# BZOJ 3874: [Ahoi2014]宅男计划

Time Limit: 1 Sec
Memory Limit: 256 MB

Description

【故事背景】

【问题描述】

JYY现在有M块钱，每一次叫外卖需要额外付给送外卖小哥外送费F元。

Input

Output

Sample Input

32 5 2

5 0

10 2
Sample Output

3

HINT

【样例说明】

JYY的最佳策略是：

【数据规模与约定】

#### Code####

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#include<bitset>
#include<vector>
using namespace std;
#define PA pair<double,double>
#define LL long long
long long read()
{
long long s=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){s=(s<<1)+(s<<3)+ch-'0';ch=getchar();}
return s*f;
}
//smile please
long double M,F,ma;
int n;
pair<long double,long double>a[205],b[205];;
long double g(LL c)
{
long double sum=F;LL now=0;
for(int i=1;i<=n;i++)
if(min(a[i].first,(long double)c)>now)
sum+=(min(a[i].first,(long double)c)-now)*a[i].second,
now=min(a[i].first,(long double)c);
if(now<c)return M+1;
return sum;
}
long double f(LL day,LL cf)
{
return g(day/cf)*(cf-day%cf)+g(day/cf+1)*(day%cf);
}
bool pan(LL day)
{
LL l=max(day/ma,(long double)1),r=day;
while(l+4<r)
{LL m1=(l+l+r)/3,m2=(l+r+r)/3;
long double f1=f(day,m1),f2=f(day,m2);
if(f1<=M||f2<=M)return 1;
if(f1<f2)
r=m2;
else
l=m1;
}
for(LL i=l;i<=r;i++)
if(f(day,i)<=M)
return 1;
return 0;
}
LL ef(LL l,LL r)
{
if(l==r)return l;
LL mid=l+r+1>>1;
if(pan(mid))
return ef(mid,r);
else
return ef(l,mid-1);
}
bool cmp(PA a,PA b)
{return a.second<b.second;}
int main()
{
M=read(),F=read(),n=read();
for(int i=1;i<=n;i++)
a[i].second=read(),a[i].first=read()+1,
ma=max(ma,a[i].first);
sort(&a[1],&a[n+1],cmp);
cout<<ef(0,M)<<endl;
return 0;
}



posted on 2016-03-05 20:40  wuyuhan  阅读(630)  评论(0编辑  收藏  举报