矩阵

  题目大意:矩阵里面的元素按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)的更多相关文章

  1. Divide and conquer:Sumsets(POJ 2549)

    数集 题目大意:给定一些数的集合,要你求出集合中满足a+b+c=d的最大的d(每个数只能用一次) 这题有两种解法, 第一种就是对分,把a+b的和先求出来,然后再枚举d-c,枚举的时候输入按照降序搜索就 ...

  2. Divide and conquer:Subset(POJ 3977)

    子序列 题目大意:给定一串数字序列,要你从中挑一定个数的数字使这些数字和绝对值最小,求出最小组合数 题目的数字最多35个,一看就是要数字枚举了,但是如果直接枚举,复杂度就是O(2^35)了,显然行不通 ...

  3. Divide and conquer:Showstopper(POJ 3484)

    Showstopper 题目大意:数据挖掘是一项很困难的事情,现在要你在一大堆数据中找出某个数重复奇数次的数(有且仅有一个),而且要你找出重复的次数. 其实我一开始是没读懂题意的...主要是我理解错o ...

  4. Divide and conquer:Garland(POJ 1759)

     挂彩灯 题目大意:就是要布场的时候需要挂彩灯,彩灯挂的高度满足: H1 = A Hi = (Hi-1 + Hi+1)/2 - 1, for all 1 < i < N HN = B Hi ...

  5. Divide and conquer:Median(POJ 3579)

        快速求两数距离的中值 题目大意:给你一个很大的数组,要你求两个数之间的距离的中值 二分法常规题,一个pos位就搞定的事情 #include <iostream> #include ...

  6. Divide and conquer:Drying(POJ 3104)

    烘干衣服 题目大意:主人公有一个烘干机,但是一次只能烘干一件衣服,每分钟失水k个单位的水量,自然烘干每分钟失水1个单位的水量(在烘干机不算自然烘干的那一个单位的水量),问你最少需要多长时间烘干衣服? ...

  7. 算法与数据结构基础 - 分治法(Divide and Conquer)

    分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...

  8. 【LeetCode】分治法 divide and conquer (共17题)

    链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...

  9. [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

随机推荐

  1. PHP: 手把手编写自己的 MVC 框架实例教程

    1 什么是MVC MVC模式(Model-View-Controller)是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller ...

  2. UIGestureRecognizer

    •为了完成手势识别,必须借助于手势识别器----UIGestureRecognizer • •利用UIGestureRecognizer,能轻松识别用户在某个view上面做的一些常见手势 • •UIG ...

  3. HDU 2014

    #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> typedef float ElementType; void Select_Sort ...

  4. POJ 2151 Check the difficulty of problems

    以前做过的题目了....补集+DP        Check the difficulty of problems Time Limit: 2000MS   Memory Limit: 65536K ...

  5. iOS开发——源代码管理——svn 命令行下常用的几个命令

    1.将文件checkout到本地目录    svn checkout path(path是服务器上的目录)    例如:svn checkout svn://192.168.1.1/pro/domai ...

  6. C 语言 homework(2)

    <C语言程序设计>实验报告 学 号 160809215 姓 名 韩笑 专业.班 计科16-2班 学 期 2016-2017 第1学期 指导教师 黄俊莲 吴喆 实验地点 C区二层机房 机 器 ...

  7. 9.9---n皇后问题(CC150)

    思路:首先写一个检查能不能摆的函数.boolean checkValid(int[] columns,int row1, int column1);意思是row1行摆在column1列可不可以. 然后 ...

  8. 无密码执行sudo指令

    参考:http://askubuntu.com/questions/334318/sudoers-file-enable-nopasswd-for-user-all-commands 配置: 执行vi ...

  9. Python 2.x闭包(enclosure)中的变量访问&修改

    http://stackoverflow.com/questions/3190706/nonlocal-keyword-in-python-2-x ---answer---- Python can r ...

  10. [mysql]max_allowed_packet ,centos

    在通过脚本向mysql写入大量测试数据时,出现这个问题,记录下: https://dev.mysql.com/doc/refman/5.5/en/packet-too-large.html 修改/et ...