Binary [swust OJ 327] minimum maximum and maximum minimum

Source: Internet
Author: User
Minimum Maximum and maximum minimum (0327)Time limit (MS): 2500 memory limit (Kb): 65535 submission: 329 accepted: 18 Description

It is also a data processing question (>_< ). Let's look at the input directly.

Input

A group of test instances. Line 1: two numbers n, m (1 <= n <= 1000000, 1 <= m <= 100000), N indicates the number of numbers, and M indicates the number of queries. Line 2: contains N integers A (I) (0 <= a (I) <= 1000000000 ). Separate Two numbers with spaces. Next m rows: each row contains two integers, A and B (1 <= A <= 2,-1000000000 <= B <= 1000000000 ). Indicates a query. When a = 1, the maximum number of N numbers smaller than B is output. When a = 2, the minimum value of N numbers greater than B is output. If there is no number that meets the condition,-1 is output.

Output

Each query outputs an answer. Each answer occupies one row.

Sample Input
12345 5 21 2 3 3 51 32 5
Sample output
123 2-1
Hintscpc_zhangjian
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF 0x7fffffff # define n 1000010 int n, m; int A [n]; int low_bound (int K) {int L = 1, R = n + 1; while (L <R) {int M = L + (R-l)/2; if (a [m]> = k) r = m; else l = m + 1;} return l ;} int up_bound (int K) {int L = 1, R = n + 1; while (L <r) {int M = L + (R-l)/2; if (A [m] <= k) L = m + 1; else r = m;} return l;} int main () {int I, j, X; scanf ("% d", & N, & M); for (I = 1; I <= N; I ++) {scanf ("% d ", & A [I]);} Sort (a + 1, A + n + 1 ); // tease ratio, unordered, always wa, cannot stand = while (M --) {int op, X; scanf ("% d", & OP, & X); If (OP = 1 & A [1]> = x | op = 2 & A [n] <= X) {cout <-1 <Endl; continue;} If (OP = 1) {int ans = low_bound (x); ans --; cout <A [ANS] <Endl;} else {int ans = up_bound (x); cout <A [ANS] <Endl ;}} return 0 ;}

 

 

Binary [swust OJ 327] minimum maximum and maximum minimum

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.