Bad Hair Day
Test instructions: to N (n <= 800,000) cattle, each cow has a height of h, each cow can only see the right side than its short cow's hair, will each cow see the cow's hair add up to how much?
Idea: Each head to the stack of cows, the top of the stack than its dwarf cattle out of the stack, because these cows have no chance to see the cattle back, so out of the stack, then add the number of elements in the stack can;
#include <iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<vector>#include<cmath>#include<stdlib.h>#include<time.h>#include<stack>#include<Set>#include<map>#include<queue>using namespacestd;#defineRep0 (I,L,R) for (int i = (l); i < (R); i++)#defineREP1 (I,L,R) for (int i = (l); I <= (r); i++)#defineRep_0 (i,r,l) for (int i = (r); i > (l); i--)#defineRep_1 (i,r,l) for (int i = (r); I >= (l); i--)#defineMS0 (a) memset (A,0,sizeof (a))#defineMS1 (a) memset (A,-1,sizeof (a))#defineMSi (a) memset (A,0x3f,sizeof (a))#defineINF 0x3f3f3f3f#defineLson L, M, RT << 1#defineRson m+1, R, RT << 1|1typedef pair<int,int>PII;#defineA First#defineB Second#defineMK Make_pairtypedef __int64 Ll;template<typename t>voidRead1 (T &m) {T x=0, f=1;CharCh=GetChar (); while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} M= x*F;} Template<typename t>voidRead2 (T &a,t &b) {Read1 (a); Read1 (b);} Template<typename t>voidRead3 (T &a,t &b,t &c) {Read1 (a); Read1 (b); Read1 (c);} Template<typename t>void out(T a) {if(a>9) out(ATen); Putchar (A%Ten+'0');}Const intMAXN = 8e4+7;intSTK[MAXN];intMain () {ll P=0, ans =0, N,h; Read1 (n); Rep0 (i,0, N) {read1 (h); while(P && h >= stk[p]) p--; stk[++P] =h; Ans+ = P1; //cout<<p<< ". \ n";} printf ("%i64d\n", ans); return 0;}
POJ 3250 Bad Hair day monotonic stack Primer