Divide and conquer:Matrix(POJ 3685)

时间:2022-03-14 16:23:49

           Divide and conquer:Matrix(POJ 3685)

                矩阵

  题目大意:矩阵里面的元素按i*i + 100000 * i + j*j - 100000 * j + i*j填充(i是行,j是列),求最小的M个数

  这一题要用到两次二分,实在是二分法的经典,主要是每一列都是按照行来递增的,每一行我们都用二分法找到比mid小的那些数就好了

  参考http://blog.csdn.net/nw4869/article/details/24124019

  

 #include <iostream>
#include <algorithm>
#include <functional> using namespace std;
typedef long long LL_INT; LL_INT getnum(LL_INT, LL_INT);
bool judge(LL_INT, LL_INT, const int); int main(void)//这题双二分法
{
int test_sums, size;
LL_INT M;
scanf("%d", &test_sums); while (test_sums--)
{
scanf("%d%lld", &size, &M);
LL_INT lb = -0x3fffffffffffffff, rb = 0x3fffffffffffffff, mid;
while (rb - lb > )
{
mid = (lb + rb) / ;
if (judge(mid, M, size)) lb = mid;
else rb = mid;
}
printf("%lld\n", rb);
}
return ;
} LL_INT getnum(LL_INT i, LL_INT j)
{
return i*i + * i + j*j - * j + i*j;//注意这题的每一列都是递增的!
} bool judge(LL_INT x, LL_INT M, const int N)
{
LL_INT line_l, line_r, mid, sum = ;
for (int col = ; col <= N; col++)
{
line_l = ; line_r = N + ;
if (getnum(N, col) <= x)//先判断一下这个,还可以剪枝,而且还能避免判断n=1的时候的错误
sum += N;
else
{
while (line_r - line_l > )
{
mid = (line_l + line_r) / ;
if (getnum(mid, col) <= x) line_l = mid;
else line_r = mid;
}
if (getnum(line_l, col) <= x)
sum += line_l;
else
sum += line_l - ;
}
}
return sum < M;
}

  Divide and conquer:Matrix(POJ 3685)