POJ 3685 Matrix (二分搜索)

来源:互联网 发布:mac 找不到原身 编辑:程序博客网 时间:2024/05/24 00:48

Matrix
Time Limit: 6000MS Memory Limit: 65536KTotal Submissions: 5131 Accepted: 1381

Description

Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i2 + 100000 × i + j2 - 100000 × j + i × j, you are to find the M-th smallest element in the matrix.

Input

The first line of input is the number of test case.
For each test case there is only one line contains two integers, N(1 ≤ N ≤ 50,000) and M(1 ≤ M ≤ N × N). There is a blank line before each test case.

Output

For each test case output the answer on a single line.

Sample Input

121 12 12 22 32 43 13 23 83 95 15 255 10

Sample Output

3-99993312100007-199987-99993100019200013-399969400031-99939

这题自己逗了自己半天 - - 又是一个二分套二分  外层二分答案  但是内层只能二分i 因为只有i>0的时候 关于i的函数是单调的

- - 查了半天bug

AC代码如下:

////  Created by TaoSama on 2015-04-28//  Copyright (c) 2015 TaoSama. All rights reserved.//#include <algorithm>#include <cctype>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <iomanip>#include <iostream>#include <map>#include <queue>#include <string>#include <set>#include <vector>using namespace std;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int N = 1e5 + 10;long long n, m;bool check(long long x) {    long long cnt = 0;    for(int j = 1; j <= n; ++j) {        int l = 0, r = n + 1;        while(l + 1 < r) {            int i = l + r >> 1;            long long col = 1LL * j * j + 1LL * i * i + 1LL * i * j + (i - j) * 100000LL;            if(col < x) l = i;            else r = i;        }        cnt += l;    }    return cnt < m;}int main() {#ifdef LOCAL    freopen("in.txt", "r", stdin);//  freopen("out.txt","w",stdout);#endif    ios_base::sync_with_stdio(0);    int t; cin >> t;    while(t--) {        cin >> n >> m;        long long l = -100000 * n, r = 3 * n * n + 100000 * n;        while(l + 1 < r) {            long long mid = l + r >> 1;            if(check(mid)) l = mid;            else r = mid;        }        cout << l << '\n';    }    return 0;}


0 0
原创粉丝点击