#1128: Dichotomy • Two find (two methods first sorted in two points O (NLOGN) + Direct binary + quick-line idea O (2N))

Source: Internet
Author: User


#1128: two points • Two points find
Time limit: 10000ms
Single point time limit: 1000ms
Memory Limit: 256MB

Describe

Nettle recently played "Battleship これ", so nettle collected a lot of boats (here we assume nettle krypton a lot of gold, opened countless seats). After removing the duplicate boat, there are still N (1≤n≤1,000,000) different kinds of boats left. Each ship has a rare value, the rare value of any two ships is different, the smaller the rarity of the ship the more rare, the higher the value.
Nettle now made a ship through Dajian, and he wondered if the ship was repeating. If it is repeated, then the ship in the nettle all the ships in the rare number of rows.

Question One
Nettle has already arranged all of his ships according to the rare values from small to large (A[1..N]), we have to do is to see whether the new ship (assuming a rare value of k) is in this sequence, and there is a corresponding a[i]=k, I is how much?

Tip One: Binary lookup of ordered arrays
Question Two
Because Nettle had too many ships, he did not want to give all the boats a rare value, but rather told us the rarity of each ship. How do we solve this problem in this situation?

Hint two: binary lookup of non-ordered array
Input

Line 1th: 2 integer n,k. n indicates the length of the array, and K indicates the number to be searched;
Line 2nd: N integers, representing A[1..N], guaranteed not to have duplicate numbers, 1≤a[i]≤2,000,000,000.
Output

Line 1th: An integer t, indicating that K is a small number in the array, if K is not in the array, output-1.
Sample input

10 5180
2970 663 5480 4192 4949 1 1387 4428 5180 2761

Sample output

9

First sort in two minutes 48ms

#include <cstdio> #include <algorithm>//#include <bits/stdc++.h>using namespace std;template<    Class T>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1000011;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t);i< (n); i++) TyPedef Long Long ll;typedef double db;typedef pair<int,int> P; #define BUG printf ("---\ n"); #define MOD 10007LL A[MAXN        ];int BS (int left,int right,int key) {while (left<=right) {int mid= (left+right) >>1;        if (Key==a[mid]) return mid;        else if (A[mid]>key) right=mid-1;    else left=mid+1; } return-1;}    int main () {//#ifndef Online_judge//Freopen ("In.txt", "R", stdin);    #endif//Online_judge int n,m,i,j,k,t;        while (Read_ (n,k)) {for (i,1,n+1) {read (a[i]);        } sort (a+1,a+n+1);    Writeln (BS (1,N+1,K)); } return 0;}


Using the idea of dichotomy, 28ms#include<cstdio> #include <algorithm>//#include <bits/stdc++.h>using namespace std;    Template<class t>inline T Read (t&x) {char C;    while ((C=getchar ()) <=32) if (c==eof) return 0;    BOOL Ok=false;    if (c== '-') Ok=true,c=getchar ();    for (x=0; c>32; C=getchar ()) x=x*10+c-' 0 ';    if (OK) x=-x; return 1;} Template<class t> inline T read_ (t&x,t&y) {return read (x) &&read (y);} Template<class t> inline T read__ (t&x,t&y,t&z) {return read (x) &&read (y) &&read (z);}    Template<class t> inline void Write (T x) {if (x<0) Putchar ('-'), x=-x;    if (x<10) putchar (x+ ' 0 '); else write (X/10), Putchar (x%10+ ' 0 ');}    Template<class t>inline void Writeln (T x) {write (x); Putchar (' \ n ');} -------ZCC IO template------const int MAXN=1000011;CONST double inf=999999999; #define Lson (rt<<1), L,m#define Rson (rt<<1|1), M+1,r#define M ((l+r) >>1) #define for (i,t,n) for (int i= (t); I< (n); i++) typedef long Long Ll;typedef double db;typedef pair<int,int> P; #define BUG printf ("---\ n"); #define MoD        10007LL a[maxn];int bs (int left,int right,int key) {if (left>right) {if (A[left]==key) return left-1;    else return-1;    } int Mid=a[left];    int low=left;    int high=right; while (Low


#1128: Dichotomy • Two find (two methods first sorted in two points O (NLOGN) + Direct binary + quick-line idea O (2N))

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.