题意:给出一个N*N的矩阵A,A[i][j]的值等于i2 + 100000 ×i + j2 - 100000 × j + i × j,求这个矩阵中第M小的数
代码:
1 #include <cstdio> 2 #include <iostream> 3 using namespace std; 4 long long N; 5 long long cal(long long i,long long j){ 6 return i*i+100000*i+j*j-100000*j+i*j; 7 } 8 long long judge(long long d){ 9 long long j,l,r,mid,ans,sum; 10 sum=ans=0; 11 for(j=1;j<=N;j++){ 12 l=1,r=N; 13 while(l<=r){ 14 mid=(l+r)/2; 15 if(cal(mid,j)<=d){ 16 ans=mid; 17 l=mid+1; 18 } 19 else 20 r=mid-1; 21 } 22 sum+=ans; 23 } //函数关于i递增,二分求值小于d的数的个数 24 return sum; 25 } 26 int main(){ 27 long long i,j,l,r,t,M,mid,ans; 28 scanf("%I64d",&t); 29 while(t--){ 30 scanf("%I64d%I64d",&N,&M); 31 l=-1e10,r=1e10; 32 while(l<=r){ 33 mid=(l+r)/2; 34 if(judge(mid)>=M){ 35 ans=mid; 36 r=mid-1; 37 } 38 else 39 l=mid+1; 40 } 41 printf("%I64d\n",ans); 42 } 43 return 0; 44 }
原文地址:https://www.cnblogs.com/mj-liylho/p/9499545.html
时间: 2024-10-08 08:59:48