UVA 10763 Foreign Exchange (sort + binary search)

Source: Internet
Author: User

This is the first time I figure out what complexity should be written, perhaps this chapter just introduced, 500000 of the data must be used Nlogn, so I

Think of two points, its complexity is LOGN, and then the N data traversal, just NLOGN, the first two tle, and then I do not have confidence in my practice

The ... See a blog said it should be this way, and then I will be determined to change their algorithm to go, haha, the concentration of not reached five stars, up to

Three PCs ...

Ideas:

I used the structure of the preservation of the first order of each pair of sequences, and then the second element in the first element of the second search, using the

Lower_bound,upper_bound, note that there is a function to write it, in fact, these two can be directly called ... Take care to use

Upper_bound the last element is ok ... Tle is actually not my algorithm, but because I write these two functions in the loop.

, it's silly ... In this way each loop will be called ... Look online Some people say the last set of data n is less than 1000, plus if (n>1000)

for (;;;) You can, haha, really smart, I learned ... Try to use it later.

Post code:

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h>using namespace Std;struct node{int I,j;} A[500005];int visit[500005];//is used to mark whether an int n;int cmp (const void *a,const void *b) has been paired, or the struct's two-level sort {if ((node *) a)->i! = ( (node *) b)->i) return ((node *) a)->i-((node *) b)->i;return ((node *) a)->j-((node *) b)->j;}  int lower_bound (int v)//binary lookup, returns the first position equal to the element if the value to be found exists, otherwise returns the first position it is to be inserted (the sequence must be sorted first) {int m,x = 1,y = N;while (x < y) {m = x + (Y-x)/2;if (a[m].i >= v) y = m; elsex = m + 1;} return x;} int upper_bound (int v)//binary lookup, if the element exists, returns its next position (except the last one, that is, if the last element equals V, returns N), and if it does not, returns the sequence {int M;int x = 1, which is still ordered after it has been inserted; int y = n;while (x < y) {m = x + (y-x)/2;if (a[m].i<=v) x = M+1;elsey = m;} return x;} int main () {int i,j;while (CIN >> N, N) {memset (visit,0,sizeof (visit)); for (i=1; i<=n; i++) {//cin >> a[i].i & Gt;> A[i].j;//cin input stream is more time consuming scanf ("%d%d", &AMP;A[I].I,&AMP;A[I].J);} Qsort (A+1,n,sizeof (a[0]), CMP)//for (i=1; i<=n; i++)//{//CIN >> a[i].i >> a[i].j;//scanf ("%d%d", &AMP;A[I].I,&AMP;A[I].J);//cout << a[i].i << "" <<  A[I].J << Endl;//}int Flag1 = 0;for (i=1; i<=n; i++) {if (visit[i] = = 1)//paired no longer accesses continue;int flag = 0;int First = Lower_bound (A[I].J); int last = Upper_bound (A[I].J),//cout << "first =" << first << "" << Last & lt;< endl;if (a[first].i! = A[I].J)//no direct exit with which the output no {Flag1 =1;break;} if (last = = N)//give the case of n a special sentence last ++;for (J=first; j<last; J + +) {if (visit[j] = = 1) continue;if (A[J].J = a[i].i) {Visit[j] = 1; flag = 1;break;}} if (!flag) {Flag1 = 1;break;} Visit[i] = 1;} if (FLAG1) cout << "No" << endl;//puts ("no") elsecout << "yes" << endl;//puts ("Yes");} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

UVA 10763 Foreign Exchange (sort + binary search)

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.