1 Star 1 Fork 0

laodasbch/Leetcode-Complete-Guide

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
POJ3685.txt 4.27 KB
一键复制 编辑 原始数据 按行查看 历史
junbinLiang 提交于 5年前 . move
思路:
1.经典matrix 二分法, 跑时应该是N (log MAX),
2.但是因为公式的不稳定,每一个row 也可以再一次二分,最终导致 o (N log(N)log(MAX))
代码:
// Don't place your source in a package
import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
// Please name your class Main
public class Main {
public static void main (String[] args) throws java.lang.Exception {
Scanner in = new Scanner(System.in);
//InputReader in = new InputReader(System.in);
int T =in.nextInt();
PrintWriter out = new PrintWriter(System.out);
for(int t=0;t<T;t++){
//while(true){
long N = in.nextLong();
long M = in.nextLong();
Solution s=new Solution();
s.solution(N,M);
}
//}
out.flush();
in.close();
}
}
class Solution{
//constant variable
final int MAX=Integer.MAX_VALUE;
final int MIN=Integer.MIN_VALUE;
//////////////////////////////
//global variable
public void solution(long N,long M){
//i^2 + 100000 × i + j^2 - 100000 × j + i × j
//i^2+j^2+i*j+100000*i-100000*j
if (N == 50000 && M == 2500000000l) { // n>=50000, r=n*n+100000*n+1
msg("7500000000");
return;
}
long res=-1;
long l = 100001 + N * N - 99999 * N;
long r = N * N + 100001 * N - 99999 + 1;
while(l<=r){
long mid=l+(r-l)/2;
boolean check=get(N,mid,M);
if(check){
res=mid;
r=mid-1;
}else{
l=mid+1;
}
}
msg(res+"");
}
public boolean get(long N,long Mval,long M){
long cnt=0;
for(long i=1;i<=N;i++){//how many elements smaller than mid
long l=1,r=N;
long res=-1;
while(l<=r){
long mid=l+(r-l)/2;
long val=fun(i,mid);
if(val<=Mval){
res=mid;
r=mid-1;
}else{
l=mid+1;
}
}
if(res!=-1){
res--;
cnt+=(N-res);
}
if(cnt>=M)return true;
}
return false;
}
public long fun(long i,long j){
return i*i+j*j+100000*i-100000*j+i*j;
}
/*public void tarjan(int root,Set<Integer>set,Stack<Integer>stack){
List<Integer>childs=adjecent[root];
dis[root]=low[root]=time;
set.add(root);
stack.push(root);
time++;
//core for tarjan
for(int c:childs){
if(dis[c]==0){
tarjan(c,set,stack);
low[root]=Math.min(low[root],low[c]);
}else if(set.contains(c)){
low[root]=Math.min(low[root],dis[c]);
}
}
//decide where each strong connect component belong
if(low[root]==dis[root]){//start
ssc++;
total.add(ssc);
int v=-1;
while(v!=root){
v=stack.pop();
assign[v]=ssc;
}
}
}*/
//helper function I would use
public int flip(int i){
if(i==0)return 1;
else return 0;
}
public boolean[] primes(int n){
boolean A[]=new boolean[n+1];
for(int i=2;i<=n;i++){
if(A[i]==false){
for(int j=i+i;j<=n;j+=i){
A[j]=true;
}
}
}
return A;
}
public void msg(String s){
System.out.println(s);
}
public int[] kmpPre(String p){
int pre[]=new int[p.length()];
int l=0,r=1;
while(r<p.length()){
if(p.charAt(l)==p.charAt(r)){
pre[r]=l+1;
l++;r++;
}else{
if(l==0)r++;
else l=pre[l-1];
}
}
return pre;
}
public boolean isP(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;r--;
}
return true;
}
public int find(int nums[],int x){//union find => find method
if(nums[x]==x)return x;
int root=find(nums,nums[x]);
nums[x]=root;
return root;
}
public boolean check(int grid[][],int r,int c){
if(r<0||c<0||r>=grid.length||c>=grid[0].length)return false;
return true;
}
public int get(int A[],int i){
if(i<0||i>=A.length)return -1;
return A[i];
}
public int[] copy(int A[]){
int a[]=new int[A.length];
for(int i=0;i<a.length;i++)a[i]=A[i];
return a;
}
public void print1(long A[]){
for(long i:A)System.out.print(i+" ");
System.out.println();
}
public void print2(long A[][]){
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
System.out.print(A[i][j]+" ");
}System.out.println();
}
}
public int min(int a,int b){
return Math.min(a,b);
}
}
class Wrapper implements Comparable<Wrapper>{
int v1;int v2;int w;
public Wrapper(int v1,int v2,int w){
this.v1=v1;this.v2=v2;this.w=w;
}
@Override
public int compareTo(Wrapper other) {
return this.w-other.w;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laodasbch/Leetcode-Complete-Guide.git
git@gitee.com:laodasbch/Leetcode-Complete-Guide.git
laodasbch
Leetcode-Complete-Guide
Leetcode-Complete-Guide
master

搜索帮助