POJ 3685 二分套二分(水题

题意:给出一个N*N的矩阵A,A[i][j]的值等于i2 + 100000 ×i + j2 - 100000 × j + i × j,求这个矩阵中第M小的数

代码:

 1 #include <cstdio>
 2 #include <iostream>
 3 using namespace std;
 4 long long N;
 5 long long cal(long long i,long long j){
 6     return i*i+100000*i+j*j-100000*j+i*j;
 7 }
 8 long long judge(long long d){
 9     long long j,l,r,mid,ans,sum;
10     sum=ans=0;
11     for(j=1;j<=N;j++){
12         l=1,r=N;
13         while(l<=r){
14             mid=(l+r)/2;
15             if(cal(mid,j)<=d){
16                 ans=mid;
17                 l=mid+1;
18             }
19             else
20             r=mid-1;
21         }
22         sum+=ans;
23     }                           //函数关于i递增,二分求值小于d的数的个数
24     return sum;
25 }
26 int main(){
27     long long i,j,l,r,t,M,mid,ans;
28     scanf("%I64d",&t);
29     while(t--){
30         scanf("%I64d%I64d",&N,&M);
31         l=-1e10,r=1e10;
32         while(l<=r){
33             mid=(l+r)/2;
34             if(judge(mid)>=M){
35                 ans=mid;
36                 r=mid-1;
37             }
38             else
39             l=mid+1;
40         }
41         printf("%I64d\n",ans);
42     }
43     return 0;
44 }

原文地址:https://www.cnblogs.com/mj-liylho/p/9499545.html

时间: 2024-10-08 08:59:48

POJ 3685 二分套二分(水题的相关文章

POJ 3685 二分套二分

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

POJ-3579 Median---二分第k大(二分套二分)

题目链接: https://cn.vjudge.net/problem/POJ-3579 题目大意: 求的是一列数所有相互之间差值的序列的最中间的值是多少. 解题思路: 可以用二分套二分的方法求解第m大,和POJ-3685类似,这里的模板也差不多 枚举第m大x,判断小于等于x的数目是不是大于m,如果大于m说明x >= 第m大,调整区间r = mid - 1 不然l = mid + 1 此处是大于m而不是小于m是因为一个数可能出现多次,那么小于等于中间的数目可能就比m大 在计算小于等于x的数目的时

POJ 3685 Matrix 二分套二分

POJ 3685 Matrix 二分 题意 有一个N阶方阵,方正中第i行第j列的元素值为\(d_{i,j}=i^{2}+1e5*i+j^{2}-1e5*j+i*j\),我们需要找出这个方阵中第M小的元素值. 解题思路 分析这个公式,我们发现:当j固定的时候,这个公式关于i(取值范围:从0到n)是单调增加的,所以这里我们可以二分一个答案,然后一列一列的找小于(等于)它的个数,这样加起来我们就能知道我们枚举的这个答案是第几小了. 需要注意的是,第一个最外层的二分有点不同,因为我们二分的答案可能不存在

POJ 3030. Nasty Hacks 模拟水题

Nasty Hacks Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 13136   Accepted: 9077 Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of malicious software which teenagers may use to fool their friends.

poj 3444 Wavelet Compression 模拟水题

水题,直接贴代码. //poj 3444 //sep9 #include <iostream> using namespace std; const int maxN=260; int a[maxN],b[maxN]; int main() { int i,n,m; while(scanf("%d",&n)==1&&n){ for(i=1;i<=n;++i) scanf("%d",&a[i]); m=1; while

【POJ】Cow Multiplication(水题)

Cow Multiplication http://poj.org/problem?id=3673 题意:输入两个数A B,比如123和45   然后算123*45这个运算是指1*4 + 1*5 + 2*4 + 2*5 + 3*4 + 3*5 = 54. 思路:水题. #include<iostream> #include<cmath> #include<cstring> using namespace std; typedef long long ll; const

poj 1004:Financial Management(水题,求平均数)

Financial Management Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 126087   Accepted: 55836 Description Larry graduated this year and finally has a job. He's making a lot of money, but somehow never seems to have enough. Larry has deci

二分套二分 hrbeu.acm.1211Kth Largest

Kth Largest TimeLimit: 1 Second   MemoryLimit: 32 Megabyte Description There are two sequences A and B with N (1<=N<=10000) elements each. All of the elements are positive integers. Given C=A*B, where '*' representing Cartesian product, c = a*b, whe

poj3685 二分套二分

F - 二分二分 Crawling in process... Crawling failed Time Limit:6000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Submit Status 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