标签:
题目大意:有一个N * N的矩阵,其中Aij = i * i + i * 100000 - 100000 * j + j * j + i * j,问这个矩阵中,第M小的数是多少
解题思路:观察这个式子,可以发现j不变的情况下,随着i的增大,Aij也相应增大,由这个受到启发
二分枚举第M小的数,然后按列寻找,找到第一个大于这个数的位置,就可以知道该列中有多少个数是大于这个数的了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
typedef long long ll;
ll N, M;
ll Count(int i, int j) {
return ll(i + 100000 + j) * i + ll(j - 100000) * j;
}
bool judge(ll mid) {
ll cnt = 0;
for(int j = 1; j <= N; j++) {
int l = 1, r = N;
while(l <= r) {
int Mid = (l + r) / 2;
if( Count(Mid,j) <= mid)
l = Mid + 1;
else
r = Mid - 1;
}
cnt += l - 1;
}
return cnt < M;
}
ll solve() {
ll l = -((ll)N * N * 3 + 100000 * N) , r = (ll)N * N * 3 + 100000 * N;
while(l <= r) {
ll mid = (l + r) / 2;
if(judge(mid))
l = mid + 1;
else
r = mid - 1;
}
return l;
}
int main(){
int test;
scanf("%d", &test);
while(test--) {
scanf("%lld%lld", &N, &M);
printf("%lld\n",solve());
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/l123012013048/article/details/45704235