Educational Codeforces Round 2_B. Queries about less or equal elements

来源:互联网 发布:淘宝csv数据怎么制作 编辑:程序博客网 时间:2024/06/11 10:04
B. Queries about less or equal elements
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given two arrays of integers a andb. For each element of the second array bj you should find the number of elements in arraya that are less than or equal to the valuebj.

Input

The first line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the sizes of arraysa and b.

The second line contains n integers — the elements of arraya ( - 109 ≤ ai ≤ 109).

The third line contains m integers — the elements of arrayb ( - 109 ≤ bj ≤ 109).

Output

Print m integers, separated by spaces: thej-th of which is equal to the number of such elements in arraya that are less than or equal to the valuebj.

Sample test(s)
Input
5 41 3 5 7 96 4 2 8
Output
3 2 1 4
Input
5 51 2 1 2 53 1 4 1 5
Output
4 2 4 2 5
二分||
#include <bits/stdc++.h>using namespace std;typedef long long int LL ;const int maxn = 2e5+100;LL a[maxn], b[maxn], c[maxn];int n, m;int erfen(LL p) {int le = 1, ri = n, mid;mid = (le+ri)/2;for (int i = 1; i<300; i++) {if (p>=a[mid]) le = mid;else if (p<a[mid]) ri = mid;mid = (le+ri)/2;}if (p == a[mid] && p!=a[ri])return mid;else if (p == a[mid] && p==a[ri])return ri;if (p>a[mid] && p<a[ri])return mid;else if (p>a[mid] && p >= a[ri]) return ri;else if (p<a[mid] && p<a[le])return mid-1;else if (p<a[mid] && p>=a[le])return le;}int main() {memset(a, 0, sizeof(a));memset(b, 0, sizeof(b));memset(c, 0, sizeof(c));cin >> n >> m;for (int i = 1; i<=n; i++)cin >> a[i];for (int i = 1; i<=m; i++)cin >> b[i];sort(a+1, a+n+1);//sort(b, b+m);int num = 0;for (int i = 1; i<=m; i++) {c[num++] = erfen(b[i]);}for (int i = 0; i<num-1; i++)cout << c[i] << " ";cout << c[num-1] << endl;return 0;}


0 0
原创粉丝点击