Pig eating Watermelon (wmelon)-sort-find

Source: Internet
Author: User

Question A: Pig eat Watermelon (Wmelon) time limit: 1 Sec memory limit: up to MB
Submitted: 14 Solution:
Submitted State [Discussion Version] Title Description

One day, the greedy pig came to a big orchard, the orchard has n (n≤100000) A big watermelon, the quality of each watermelon is not greater than the length of the whole type (longint), and the quality of each watermelon is different. Pig very bored, first put all the watermelon by small to large, and then choose M (m≤l00000) A quality is Ki watermelon, please help him to eat the watermelon to find out.

Input

Enter N in line 1th, and then enter n integers for the following n lines;
Then enter M, and then the following m lines, one integer ki per line.

Output

Output m lines, one integer per line, indicating the position of Ki in this n number after reordering.

Sample input
31321231451123
Sample output
1
Problem-solving ideas: distance from the last time to do the problem has been a week, mainly because the problem does not go on, the foundation is too weak, a little more complex algorithm problems do not come out, because the heart is more impetuous, the algorithm also can not understand ... Always remind yourself: Keep a calm heart!!
The output in this question says: "After the rearrangement, Ki in the position of the inside, let me mistakenly think every time after the number of the back to be reduced 1!!! Then submit two times wrong.
Then, after the Zxp reminder, the AC has been submitted.
Because the amount of data too much, not every one to find once, so even with the binary search also have to Nlogn, is also relatively large.
The best way is to find only once: the number of the search from small to large sort, and then from the order of the watermelon began to find, and then find a back from the previous position to find it, so time complexity is n;
  Example: Watermelon: 2 3 5 7 8 9 set an i;
Order to find: 3 8 set a J;
J=1, I from 1 to 3 in the subscript, j=2, I from 3 subscript to 8 subscript, look after i from 1 to n;
Then sort by the input order and output the sorted subscript.

Code:
#include <iostream>#include<cstdio>#include<algorithm>#include<cstdlib>using namespacestd;structnode{Long intK; Long intKSORTW; Long intkcinw;}; Node xigua[100002];intCompConst void*a,Const void*b) {   return*(int*) a-* (int*) b;}intCMP (node A,node b) {returna.k<B.K;}intCMP2 (node A,node b) {returna.kcinw<b.kcinw;}intMain () {Long intN; Long inta[100002]; Long intm; Long intcou=0; scanf ("%ld",&N);  for(Long intI=1; i<=n;i++) {scanf ("%ld",&A[i]); } qsort (A+1Nsizeof(Long int), comp); scanf ("%ld",&m);  for(Long intI=1; i<=m;i++) {scanf ("%ld",&XIGUA[I].K); XIGUA[I].KCINW=i; } sort (Xigua+1, xigua+m+1, CMP); Long intI=1; Long intj=1;  while(1){        if(j==m+1){             Break; }        if(a[i]==XIGUA[J].K) {           //printf ("%ld\n", I);xigua[j].ksortw=i; J++; }Else{i++; }} sort (Xigua+1, xigua+m+1, CMP2);  for(intI=1; i<=m;i++) {printf ("%d\n", XIGUA[I].KSORTW); }    return 0;}

Pig eating Watermelon (wmelon)-sort-find

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.