Gunner II
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others) total submission (s): 1740 Accepted Submission (s): 635
Problem Descriptionlong long ago, there was a gunner whose name is Jack. He likes to go hunting very much. One day he go to the grove. There is n birds and n trees. The i-th bird stands on the top of the i-th tree. The trees stand in straight line from left to the right. Every tree has its height. Jack stands on the left side of the most tree. When Jack shots a bullet inch height h to the right, the nearest bird which stands in the tree with height H'll falls.
Jack would shot many times, he wants to know which bird would fall during each shot.
Inputthere is multiple test cases (about 5), every case gives N, m on the first line, n indicates there is n trees and N Birds, m means Jack would shot m times.
In the second line, there is n numbers h[1],h[2],h[3],..., H[n] which describes the height of the trees.
In the third line, there is m numbers q[1],q[2],q[3],..., Q[m] which describes the height of the Jack ' s shots.
Please process to the end of file.
[Technical specification]
All input items is integers.
1<=n,m<=100000 (10^5)
1<=h[i],q[i]<=1000000000 (10^9)
Outputfor each q[i], output an integer in a single line indicates the ID of bird Jack shots down. If Jack can ' t shot any bird, just output-1.
The ID starts from 1.
Sample INPUT5 51 2 3 4 11 3 1 4 2
Sample Output13542
HintHuge input, Fast IO is recommended.
The problem is to do the twists and turns, just start a look is not a hash table, inverted record is good, but in Tel and Mel, I gave up ...
So think of two points, for already used how to do, and then open the number of records have been used, but the array is too big ah, so I use a map to record, but is the number ah, wood things, converted into a string not good ... Want to use the STL two classification, but the structure of Ah, then write it yourself ...
Finished writing, feel very likely WA, because two points feel write a bit down, handed the next unexpectedly ac ... I just want to laugh.
AC Code:
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<map>#include<string>using namespacestd;Const intinf=0x3f3f3f3f;#defineSI (x) scanf ("%d", &x)#definePI (x) printf ("%d", X)#defineP_ printf ("")#defineMem (x, y) memset (x,y,sizeof (x))Const intmaxn=1e5+ -;structnode{intV,num; BOOL operator< (ConstNode &b)Const{ if(v!=B.V) { returnv<B.V; } Else returnnum<B.num; }}; Node DT[MAXN];intMain () {intn,m; while(~SCANF ("%d%d",&n,&m)) {Map<string,int>MP; for(intI=1; i<=n;i++) SI (DT[I].V), dt[i].num=i; Sort (dt+1, dt+n+1); intx; Sort (dt+1, dt+n+1); for(intI=0; i<m;i++) {SI (x); Chars[Ten]; Itoa (X,s,Ten); intL=1, r=n+1, Mid; while(l<=S) {Mid= (l+r) >>1; if(X<=DT[MID].V) r=mid-1; ElseL=mid+1; } if(dt[r+1+mp[s]].v==x) {printf ("%d\n", dt[r+1+mp[s]].num); Mp[s]++; } ElsePuts"-1"); } } return 0;}
My hash table, Tle: (contributed 9 times tle,mle) put it down.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6 using namespacestd;7 Const intinf=0x3f3f3f3f;8 #defineSI (x) scanf ("%d", &x)9 #definePI (x) printf ("%d", X)Ten #defineP_ printf ("") One #defineMem (x, y) memset (x,y,sizeof (x)) A Const intmaxn=5000010; - intTP; - intHSH[MAXN],HEAD[MAXN],POS[MAXN],NXT[MAXN]; the intans[100010]; - intq[100010]; - voidAddintPintx) { - inti=x%MAXN; +hsh[tp]=x; -pos[tp]=p; +nxt[tp]=Head[i]; Ahead[i]=tp++; at } - intMain () { - intn,m; - while(~SCANF ("%d%d",&n,&m)) { - intx; -MEM (head,-1); tp=0; inMEM (NXT,0); Mem (HSH,0); - for(intI=1; i<=n;i++){ toscanf"%d",&x); add (i,x); + } - for(intI=1; i<=m;i++) scanf ("%d",&q[i]); the for(inti=m;i>=1; i--){ *x=Q[i]; $ intflot=0;Panax Notoginseng for(intj=head[x%maxn];j!=-1; j=Nxt[j]) { - if(x==Hsh[j]) { theans[i]=Pos[j]; +flot=1; Ahsh[j]=0; Break; the } + } - if(!flot) ans[i]=-1; $ } $ for(intI=1; i<=m;i++) printf ("%d\n", Ans[i]); - } - return 0; the}
Gunner II (two points, map, digital conversion)