Examples
Example 5-1 where is the marble (where is the Marble?). , Uva 10474)
I'm mainly familiar with the use of sort and lower_bound.
About Lower_bound:
http://blog.csdn.net/niushuai666/article/details/6734403
Besides, there's upper_bound.
http://blog.csdn.net/niushuai666/article/details/6734650
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<cctype>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#defineDebug printf ("!\n")#defineINF 10000#defineMAXN 5010#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Longinta[10005];intMain () {intN,q,i,x,cnt=1; while(~SF ("%d%d", &n,&q) &&N) {pf ("case#%d:\n", cnt++); for(i=0; i<n;i++) SF ("%d",&A[i]); Sort (A,a+N); while(q--) {SF ("%d",&x); intp = lower_bound (a,a+n,x)-A; if(a[p]!=x) {PF ("%d not found\n", x); Continue; } ElsePF ("%d found at%d\n", x,p+1); } }}
Example 5-2 wood block problem (the Blocks Problem,uva 101)
Mainly familiar with the vector of Pb and resize, and the processing method of the end of the string
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<cctype>#include<vector>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineINF 10000#defineMAXN 5010#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long LongVector<int> pile[ -];intN;voidFind_block (intXint&p,int&h) { for(p=0;p <n;p++) { for(h=0; H<pile[p].size (); h++) { if(Pile[p][h] = = x)return; } }}voidClear_block (intPinth) { inti; for(i = h +1; I<pile[p].size (); i++) { intb =Pile[p][i]; PILE[B].PB (b); } pile[p].resize (H+1);}voidOnto_block (intPintHintp2) { inti; for(i = h;i<pile[p].size (); i++) PILE[P2].PB (Pile[p][i]); Pile[p].resize (h);}voidprint () {intp,h; for(p=0;p <n;p++) {PF ("%d:", p); for(h=0; H<pile[p].size (); h++) {PF ("%d", Pile[p][h]); } blank; }}intMain () {inti,a,b; SF ("%d",&N); for(i=0; i<n;i++) PILE[I].PB (i); Chars[5],s1[5]; while(SF ("%s", s) && s[0]!='Q') {SF ("%d%s%d",&a,s1,&b); intPA,PB,HA,HB; Find_block (A,pa,ha); Find_block (B,PB,HB); if(PA==PB)Continue; if(!STRCMP (s),"Move") ) Clear_block (Pa,ha); if(!STRCMP (S1,"onto") ) Clear_block (PB,HB); Onto_block (PA,HA,PB); } print ();}
Purple book The 5th Chapter C++stl