#256 (Div. 2)D. Multiplication Table
题意:给出n,m,k,问(1.....n)*(1......m)共有n*m个答案,问第k小
思路:很熟悉的样子,和51nod的第K大类似
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=500004; 5 6 ll n,m,k; 7 ll a[N],b[N]; 8 9 ll hh(int y,ll x){ 10 int l=1,r=m,mid; 11 int ans=1; 12 while(l<=r){ 13 mid=(l+r)>>1; 14 if(a[y]*b[mid]>=x){ 15 ans=mid;r=mid-1; 16 } 17 else l=mid+1; 18 } 19 return ans; 20 } 21 ll check(ll x){ 22 ll sum=0; 23 for(int i=1;i<=n;i++){ 24 if(a[i]*b[m]<x) continue; 25 sum+=m-hh(i,x)+1; 26 } 27 return sum; 28 } 29 int main(){ 30 scanf("%I64d%I64d%I64d",&n,&m,&k); 31 for(int i=1;i<=n;i++){ 32 a[i]=i; 33 } 34 for(int i=1;i<=m;i++) b[i]=i; 35 ll l=a[1]*b[1],r=a[n]*b[m]; 36 ll ans=0; 37 k=n*m-k+1; 38 39 while(l<=r){ 40 ll mid=(l+r)>>1; 41 // cout<<mid<<" "<<check(mid)<<endl; 42 if(check(mid)>=k){ 43 l=mid+1; 44 ans=mid; 45 } 46 else r=mid-1; 47 48 } 49 printf("%I64d\n",ans); 50 }
不过这题不用再套一个二分,上面的跑了900多MS,差点GG
这个280MS
1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 const int N=500004; 5 6 ll n,m,k; 7 ll a[N],b[N]; 8 9 ll check(ll x){ 10 ll sum=0; 11 ll y; 12 for(int i=1;i<=n;i++){ 13 y=min(x,i*m); 14 sum+=y/i; 15 } 16 return sum; 17 } 18 int main(){ 19 scanf("%I64d%I64d%I64d",&n,&m,&k); 20 for(int i=1;i<=n;i++){ 21 a[i]=i; 22 } 23 for(int i=1;i<=m;i++) b[i]=i; 24 ll l=a[1]*b[1],r=a[n]*b[m]; 25 ll ans=0; 26 27 28 while(l<=r){ 29 ll mid=(l+r)>>1; 30 //cout<<mid<<" "<<check(mid)<<endl; 31 if(check(mid)>=k){ 32 r=mid-1; 33 ans=mid; 34 } 35 else l=mid+1; 36 37 } 38 printf("%I64d\n",ans); 39 }