日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ_3685_Matrix_(二分,查找第k大的值)

發布時間:2025/4/16 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ_3685_Matrix_(二分,查找第k大的值) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

描述


http://poj.org/problem?id=3685

一個n*n的矩陣,(i,j)的值為i*i+100000*i+j*j-100000*j+i*j,求第m小的值.

?

Matrix
Time Limit: 6000MS?Memory Limit: 65536K
Total Submissions: 5980?Accepted: 1700

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 -99993 3 12 100007 -199987 -99993 100019 200013 -399969 400031 -99939

Source

POJ Founder Monthly Contest – 2008.08.31, windy7926778

分析


POJ_3579很像.假定一個第m小的值x,如果<=x的值<m,那么x偏小.在統計<=x的值的時候還要用到二分,對于一個確定的j,值是關于i單增的,枚舉j,二分查找使得值<=x的最大的i.

注意:

1.第一層二分的邊界:最大值是i,j取n,去掉負值;最小值是i,j取n,去掉正值.

2.第二層二分統計個數時,i在(1,n)內不一定存在使得值<=x的,所以二分范圍不能是(1.n).如x=-1,n=1,如果在(1,n)內,值只有3,這樣最后l=r=1,表示有一個<=x的值,其實一個都沒有,所以應該在(0,n)內二分,而這里因為寫成了m=l+(r-l+1)/2,有一個"+1",所以(r-l+1)/2>=1,m>=1,所以不必擔心會去到0,如果不是這樣,在val函數中應加一條:if(i==0) return -100000*n;也就是給0的情況一個最小值,如果有n+1,應賦最大值.也就是假設了左右兩個端點,最后如果落在這兩個實際不存在的點上,那么就是沒有答案.

3.數據范圍.整型上限大概2*10^9,這道題極限情況1.75*10^10,必須用long long.檢查的時候還是要耐心.

1 #include<cstdio> 2 #define ll long long 3 4 ll q,n,m; 5 6 ll val(ll i,ll j){ return i*i+100000*i+j*j-100000*j+i*j; } 7 8 ll bsearch(ll j,ll x) 9 { 10 ll l=0,r=n; 11 while(l<r) 12 { 13 ll m=l+(r-l+1)/2; 14 if(val(m,j)<=x) l=m; 15 else r=m-1; 16 } 17 return l; 18 } 19 20 bool C(ll x) 21 { 22 ll cnt=0; 23 for(ll j=1;j<=n;j++) cnt+=bsearch(j,x); 24 return cnt<m; 25 } 26 27 void solve() 28 { 29 ll l=-100000*n,r=3*n*n+100000*n; 30 while(l<r) 31 { 32 ll mid=l+(r-l)/2; 33 if(C(mid)) l=mid+1; 34 else r=mid; 35 } 36 printf("%lld\n",l); 37 } 38 39 void init() 40 { 41 scanf("%lld",&q); 42 while(q--) 43 { 44 scanf("%lld%lld",&n,&m); 45 solve(); 46 } 47 } 48 49 int main() 50 { 51 freopen("matrix.in","r",stdin); 52 freopen("matrix.out","w",stdout); 53 init(); 54 fclose(stdin); 55 fclose(stdout); 56 return 0; 57 } View Code

?

轉載于:https://www.cnblogs.com/Sunnie69/p/5423832.html

總結

以上是生活随笔為你收集整理的POJ_3685_Matrix_(二分,查找第k大的值)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。