二分学习

题目描述

给定2个数组a、b以及一个整数x

判断是否存在a[i]+b[j]=x

输入

 第一行:两个整数n、m (1<n、m<10000)

第二行:n个整数,为数组a

第三行:m个整数,为数组b

第四行:一个整数k (1<k<20),表示k次查询

接下来的k行:每行一个整数x

输出

 若数组a中存在一个a[i],数组b中存在一个b[j],使得a[i]+b[j]=x,则输出Yes

如不存在,则输出No

样例输入

2 3
1 2
3 4 5
4
1
4
5
8

样例输出

No
Yes
Yes
No

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1e5+7;
 6 int a[maxn], b[maxn];
 7 int query(int *arr, int l, int r, int key){
 8     while(l <= r){
 9         int mid = (l + r) >> 1;
10         if(arr[mid] == key) return mid;
11         if(arr[mid] < key) l = mid + 1;
12         else r = mid - 1;
13     }
14     return -1;
15 }
16 int main()
17 {
18     int n, m, k;
19     scanf("%d%d",&n,&m);
20     for(int i = 0; i < n; i++)scanf("%d",&a[i]);
21     for(int i = 0; i < m; i++)scanf("%d",&b[i]);
22     cin >> k;
23     sort(a, a + n);sort(b, b + m);
24     int q;
25     while(k--){
26         cin >> q;
27         int flag = 1;
28         for(int i = 0; i < n; i++){
29             int y = q - a[i];
30             //cout << y << endl;
31             if(y > 0){
32                 int pos = query(b, 0, m, y);
33                 if(pos != -1){
34                     cout << "Yes" << endl;
35                     flag = 0;
36                     break;
37                 }
38             }
39  
40         }
41         if(flag == 1){
42             cout << "No" << endl;
43         }
44     }
45     return 0;
46 }

 

 

670 D2

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 10^9) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 10^9), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 10^9), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Examples
input
1 1000000000
1
1000000000
output
2000000000
input
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1
output
0
input
3 1
2 1 4
11 3 16
output
4
input
4 3
4 3 5 6
11 12 14 20
output
3

 1 #include <iostream>
 2 #include <cstdio>
 3 #define ll long long
 4 using namespace std;
 5 const int maxn = 1e5+7;
 6 ll a[maxn], b[maxn];
 7 
 8 int main()
 9 {
10     ll n, k;
11     while(~scanf("%lld%lld",&n,&k)){
12         for(int i = 0; i < n; i++)scanf("%lld",&a[i]);
13         for(int i = 0; i < n; i++)scanf("%lld",&b[i]);
14         ll mid, sum, flag, l = 0, r = 2e9+10;
15         while(l <= r){
16             mid = (l + r) >> 1;
17             sum = 0;flag = 1;
18             for(int i = 0; i < n; i++){
19                 if(a[i]*mid > b[i]){
20                     sum += a[i]*mid - b[i];
21                 }
22                 if(sum > k){
23                     flag = 0;
24                 }
25             }
26             //cout << l << ' ' << r << endl;
27             if(flag) l = mid + 1;
28             else r = mid - 1;
29         }
30         printf("%lld\n",l - 1);
31     }
32 
33     return 0;
34 }

 

 

题目描述

一群妖王排成一排站在苦情巨树下,寻找自己的转世恋人。
虽然都是妖王,但按照涂山的规定必须进行标号,标号为1的妖王排在最后面,标号为n的妖王排在最前面。每个妖王只有一个妖力值a[i]表示它们现在的地位。
妖王们是讲究实力的,当然不服比它妖力值低的居然可以排在前面,它们现在想知道在它前面,妖力值比它低,而且离它最远的距离是多少?

 

输入

输入n表示有n个妖王
第二行输入n个整数,表示每个妖王的妖力值a[i]

n<=2*105

1<=a[i]<=109

 

 

输出

对于每个妖王,妖力值比它低,而且离它最远的距离是多少,如果没有输出-1

 

样例输入

6
5 50 45 7 10 1

样例输出

4 3 2 1 0 -1



求最远距离的最小值,可以用一辅助数组M把最小值弄好,这样就可以进行二分查找了。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn = 2e5+10;
 5 
 6 int a[maxn],M[maxn];
 7 int MIN(int a,int b){
 8     return a>b?b:a;
 9 }
10 int main(){
11     int n;
12     while(~scanf("%d",&n)){
13         for(int i = 1; i <= n; i++) scanf("%d",&a[i]);
14         M[n] = a[n];
15         for(int i = n - 1; i > 0; i--) M[i] = MIN(M[i+1],a[i]);
16         for(int i = 1; i <= n; i++){
17             int ans = -1,l = i + 1,r = n;
18             while(l <= r){
19                 int mid = (l + r) >> 1;
20                 if(a[i] > M[mid]){
21                     ans = mid;
22                     l = mid + 1;
23                 }
24                 else r = mid - 1;
25             }
26             if(ans == -1) printf("-1");
27             else printf("%d",ans - i - 1);
28             if(i == n) printf("\n");
29             else printf(" ");
30         }
31     }
32     return 0;
33 }

 


posted @ 2017-03-12 11:49  starry_sky  阅读(145)  评论(0编辑  收藏  举报