题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFnoi%E8%80%83%E5%90%8E%E6%AC%A2%E4%B9%90%E8%B5%9B%29/%E4%B9%8C%E9%B8%A6%E5%96%9D%E6%B0%B4
题解:真是一道神题!考场上绝对想不到标算orz!
题解写在代码注释里
代码:
#include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<iostream> #include<vector> #include<map> #include<set> #include<queue> #include<string> #define inf 1000000000 #define maxn 100000+5 #define maxm 500+100 #define eps 1e-10 #define ll long long #define pa pair<int,int> #define for0(i,n) for(int i=0;i<=(n);i++) #define for1(i,n) for(int i=1;i<=(n);i++) #define for2(i,x,y) for(int i=(x);i<=(y);i++) #define for3(i,x,y) for(int i=(x);i>=(y);i--) #define mod 1000000007 using namespace std; inline int read() { int x=,f=;char ch=getchar(); while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();} while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();} return x*f; }
int n,m,h,s[maxn],a[maxn],b[maxn],id[maxn];
inline void add(int x,int y){for(;x<=n;x+=x&(-x))s[x]+=y;}
inline int sum(int x){int t=;for(;x;x-=x&(-x))t+=s[x];return t;}
inline bool cmp(int x,int y){return b[x]==b[y]?x<y:b[x]<b[y];} int main() { freopen("input","r",stdin); freopen("output.txt","w",stdout); n=read();m=read();h=read();
for1(i,n)a[i]=read();
for1(i,n)
{
int x=read();
if(a[i]<=h)b[i]=(h-a[i])/x+;//b[i]表示该点最多可以被饮几次水
if(b[i]>)add(i,);id[i]=i;
}
//可以证明,最后的答案一定是某个b[i],所以我们按b[i]从小到大处理
sort(id+,id+n+,cmp);
int now=,pos=,ans=,st=n+;//now 表示当前的趟数
//for1(i,n)cout<<i<<' '<<id[i]<<' '<<b[i]<<endl;
for1(i,n)if(b[id[i]]){st=i;break;}
for2(i,st,n)
{
while(now<=m)
{
int t=sum(n)-sum(pos);
if(ans+t<b[id[i]])ans+=t,now++,pos=;//如果可以走完这一趟
else break;
}
if(now>m)break;
int l=pos,r=n,t=sum(pos);
while(l<=r)
{
int mid=(l+r)>>;
if(sum(mid)-t+ans>=b[id[i]])r=mid-;else l=mid+;
}//二分到下一个点
ans=b[id[i]];pos=l;
for(;b[id[i]]==b[id[i+]];i++)add(id[i],-);//去除不能再次饮水的点
add(id[i],-);
}
printf("%d\n",ans); return ; }