ZOJ 3278 8G Island(二分)
来源:互联网 发布:淘宝关于退货运费规则 编辑:程序博客网 时间:2024/06/02 01:17
Description
8g Island has N boys and M girls. Every person has a "charm value". When a boy with charm value t1 and a girl with charm value t2 get together, their "8g value" is t1*t2. Every year the king will choose the Kth greatest number from all the N*M possible 8g values as the lucky number of the year. People on the island knows nothing but 8g, so they ask you to help them find the lucky number.
Input
The input contains multiply test cases(<= 10). Each test case contains three parts:
1 Three integers N, M(1 <= N,M <= 100000) and K(1 <= K <= N*M) in a line, as mentioned above.
2 N integers in a line, the charm value of each boy.
3 M integers in a line, the charm value of each girl.
All the charm values are integers between 1 and 100000(inclusive).
Process to the end-of-file.
Output
For each test case print a single line that contains the lucky number.
Sample Input
3 2 31 2 31 22 2 11 11 12 2 41 11 1
Sample Output
311
题目大意:
有两个数组,a[n],b[m], 求第K小a[i]*b[j]。
解题思路:
因为N,M的数量级是10^5 。所以不能算出所有的乘积再去找第K大。 那么想到第K大,是一个有序表。那么可以用二分。
首先二分一个答案X.单调性易证。
对于答案X,判断方法是:判断有多少个比他大的,将B数组排序,枚举A[I],二分a[i]*b[j] >= X。
#include <cstdio>#include <algorithm>#include <vector>#include <algorithm>#include <iostream>#include <string>#define LL long longusing namespace std;LL a[100000+1],b[100000+1];LL n,m,k;int cmp(int a,int b){ return a > b;}bool judge(LL x){ LL ok = 0; for(int i = 1;i <= n;i++) { LL l = 1,r = m; LL res = 0; while(l <= r) { LL mid = (l+r)>>1; if(a[i]*b[mid] >= x) { res = mid; l = mid+1; } else r = mid-1; } ok += res; } return ok >= k;}int main(){ while(scanf("%lld %lld %lld",&n,&m,&k) != EOF) { for(int i = 1;i <= n;i++) scanf("%lld",&a[i]); for(int i = 1;i <= m;i++) scanf("%lld",&b[i]); sort(b+1,b+m+1,cmp); sort(a+1,a+n+1,cmp); judge(1); LL l = a[n]*b[m],r = a[1]*b[1]; LL ans = l; while(l <= r) { LL mid = (l+r)>>1; if(judge(mid))//判断大于mid的个数 { l = mid+1; ans = mid; } else { r = mid-1; } } printf("%lld\n",ans); } return 0;}
- ZOJ 3278 8G Island 二分+二分
- ZOJ 3278 8G Island(二分)
- ZOJ 3278 8G Island (二分套二分)
- zoj 3278 & scau_oj 10273 8G Island (二分)
- ZOJ 3278: 8G Island
- ZOJ3278 8G Island
- ZOJ3279 —— 8G island
- zoj 3500 island communication
- zoj 3509 Island Communication
- zoj 2254 - Island Country
- ZOJ 1638 Greedy Island
- zoj 3278【二分】
- (二分,BFS).Treasure Island
- ZOJ 3810 A Volcanic Island
- zoj 3810 A Volcanic Island
- zoj 3810 A Volcanic Island
- ZOJ 3810 A Volcanic Island
- ZOJ 3810 A Volcanic Island 构造题
- hive命令行
- 浅谈算法和数据结构: 十一 哈希表
- 用Model-View-ViewModel构建iOS App
- Anagrams
- (iOS)基于XMPP协议的简单IM实现
- ZOJ 3278 8G Island(二分)
- function pointer(函数指针)
- VBA如何统计同一类型的数据的总和
- IOS学习 UIGestureRecognizer相关
- driver--驱动
- [PHP] - 逗号和点号的区别
- 指定控件个数,指定行数,平铺控件
- Mongodb在Windows下安装及配置
- 解剖Nginx·模块开发篇(1)跑起你的 Hello World 模块!