zoukankan      html  css  js  c++  java
  • [51NOD1105]第k大的数(二分答案)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105

    先排序,二分上下界分别是最小的两个数和最大的两个数的乘积。注意到一个性质,就是a[i]*b[j],i从左到右,j从右到左。假如遇到一组a[i]*b[j]<=x,j就不必再减小了。这时候比x小的数一定是j个。下一次继续找i,这个b[j]一定不可能大于b[j+1]。问题就由找第k大的数,转换成找第n*n-k+1小的数。每次二分这个乘积x,看看是否符合条件即可。

      1 /*
      2 ━━━━━┒ギリギリ♂ eye!
      3 ┓┏┓┏┓┃キリキリ♂ mind!
      4 ┛┗┛┗┛┃\○/
      5 ┓┏┓┏┓┃ /
      6 ┛┗┛┗┛┃ノ)
      7 ┓┏┓┏┓┃
      8 ┛┗┛┗┛┃
      9 ┓┏┓┏┓┃
     10 ┛┗┛┗┛┃
     11 ┓┏┓┏┓┃
     12 ┛┗┛┗┛┃
     13 ┓┏┓┏┓┃
     14 ┃┃┃┃┃┃
     15 ┻┻┻┻┻┻
     16 */
     17 #include <algorithm>
     18 #include <iostream>
     19 #include <iomanip>
     20 #include <cstring>
     21 #include <climits>
     22 #include <complex>
     23 #include <fstream>
     24 #include <cassert>
     25 #include <cstdio>
     26 #include <bitset>
     27 #include <vector>
     28 #include <deque>
     29 #include <queue>
     30 #include <stack>
     31 #include <ctime>
     32 #include <set>
     33 #include <map>
     34 #include <cmath>
     35 using namespace std;
     36 #define fr first
     37 #define sc second
     38 #define cl clear
     39 #define BUG puts("here!!!")
     40 #define W(a) while(a--)
     41 #define pb(a) push_back(a)
     42 #define Rint(a) scanf("%d", &a)
     43 #define Rll(a) scanf("%lld", &a)
     44 #define Rs(a) scanf("%s", a)
     45 #define Cin(a) cin >> a
     46 #define FRead() freopen("in", "r", stdin)
     47 #define FWrite() freopen("out", "w", stdout)
     48 #define Rep(i, len) for(int i = 0; i < (len); i++)
     49 #define For(i, a, len) for(int i = (a); i < (len); i++)
     50 #define Cls(a) memset((a), 0, sizeof(a))
     51 #define Clr(a, x) memset((a), (x), sizeof(a))
     52 #define Full(a) memset((a), 0x7f7f, sizeof(a))
     53 #define lp p << 1
     54 #define rp p << 1 | 1
     55 #define pi 3.14159265359
     56 #define RT return
     57 #define lowbit(x) x & (-x)
     58 #define onenum(x) __builtin_popcount(x)
     59 typedef long long LL;
     60 typedef long double LD;
     61 typedef unsigned long long ULL;
     62 typedef pair<int, int> pii;
     63 typedef pair<string, int> psi;
     64 typedef map<string, int> msi;
     65 typedef vector<int> vi;
     66 typedef vector<LL> vl;
     67 typedef vector<vl> vvl;
     68 typedef vector<bool> vb;
     69 
     70 const int maxn = 50050;
     71 LL n, k, ans;
     72 LL a[maxn], b[maxn];
     73 
     74 bool ok(LL x) {
     75     LL ret = 0;
     76     LL j = n;
     77     For(i, 1, n+1) {
     78         while(j) {
     79             if(a[i] * b[j] > x) j--;
     80             else break;
     81         }
     82         ret += j;
     83     }
     84     return ret >= k;
     85 }
     86 
     87 int main() {
     88     // FRead();
     89     while(cin >> n >> k) {
     90         k = n * n - k + 1;
     91         For(i, 1, n+1) {
     92             cin >> a[i] >> b[i];
     93         }
     94         sort(a+1, a+n+1); sort(b+1, b+n+1);
     95         LL l = a[1] * b[1], r = a[n] * b[n];
     96         while(l <= r) {
     97             LL m = (l + r) >> 1;
     98             if(ok(m)) {
     99                 r = m - 1;
    100                 ans = m;
    101             }
    102             else l = m + 1;
    103         }
    104         cout << ans << endl;
    105     }
    106     RT 0;
    107 }
  • 相关阅读:
    Hbase表的管理
    Hbase指定规则扫描表
    vim配置
    caogao
    go on shell
    实习总结
    shell 脚本
    hadoop实战
    awk使用
    java reflect
  • 原文地址:https://www.cnblogs.com/kirai/p/5527550.html
Copyright © 2011-2022 走看看