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
- POJ 3685 Matrix (二分搜索)
- POJ 3685 - Matrix(二分搜索)
- POJ-3685---Matrix (二分)
- POJ 3685 Matrix(二分套二分)
- 二分-poj-3685-Matrix
- POJ - 3685 Matrix 二分
- POJ 3685:Matrix 二分
- POJ - 3685 Matrix(二分)
- POJ - 3685 Matrix (二分搜索:查找第k大的值)
- poj 3685 Matrix(二分嵌套)
- POJ 3685Matrix(两次二分)
- poj 3685 Matrix (二分+枚举+二分)
- poj 3685 Matrix (二分套二分)
- poj 3685 Matrix 二分里套二分
- POJ3685 Matrix 二分搜索
- POJ 3685 Matrix 详细题解 (二分嵌套)
- POJ 3685 Matrix 二维的二分
- poj - 3233 - Matrix Power Series(二分)
- Yii2-资源管理(Assets)
- Linux下Nagios的安装与配置
- Android 5.0 Lollipop中新的Activity过渡效果介绍
- shell 脚本执行
- Debian 6.0.4安装配置lnmp(Nginx+MySQL+PHP)第二版
- POJ 3685 Matrix (二分搜索)
- Maven 手动添加 JAR 包到本地仓库
- Android布局文件之GridLayout
- 接口和抽象类有什么区别
- maven默认路径
- to_date 和 to_char 使用区别
- 动态规划解最长回文子序列(非最长回文字串)并优化空间复杂度
- 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
- Eclipse打开当前工程文件所在文件夹