Matrix (poj-3685, 2-point answer + 2-point)

Posted by sunder on Sun, 20 Oct 2019 20:47:15 +0200

1. Title link:

POJ-3685

2. Main idea:

For n and m.

Denotes that there is a matrix of n × n to find the value of the m-th small element.

Calculation formula of matrix elements:

Analysis:

The elements of the easy matrix are increased on the same column.

The first two answers.

Because it is to find the smallest number m, there must be a number m < answer (the number has repetition).

In the check(mid) function, first enumerate the columns, and then divide each column into two numbers ≤ mid.

ps: control the boundary and good accumulation

4. Code implementation:

#include <set>
#include <map>
#include <ctime>
#include <queue>
#include <cmath>
#include <stack>
#include <vector>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define eps 1e-6
#define pi acos(-1.0)
#define ll long long int
using namespace std;
const int M = (int)1e5;
const ll mod = (ll)1e9 + 7;
const ll inf = 0x3f3f3f3f3f;

ll n, m;

ll f(ll i, ll j)
{
    return i * i + j * j + M * (i - j) + i * j;
}

bool check(ll x)
{
    ll cnt = 0;
    for(int i = 1; i <= n; ++i)
    {
        int l = 0;
        int r = n;
        while(l < r)
        {
            int mid = (l + r + 1) >> 1;
            if(f(mid, i) > x)
                r = mid - 1;
            else
                l = mid;
        }
        cnt += l;
    }
    return cnt >= m;
}

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%lld %lld", &n, &m);
        ll l = -inf;
        ll r = inf;
        while(l < r)
        {
            ll mid = (l + r) >> 1;
            if(check(mid))
                r = mid;
            else
                l = mid + 1;
        }
        printf("%lld\n", r);
    }
    return 0;
}