Blue book as a study reference, a stack of learning
Example 1:
To implement a stack, support Push, Pop and GetMin (the smallest value query stack) (1) is completed in O
: Thinking algorithm
minimum of two stacks established, A stored original data, B stored in the bottom of the stack at the beginning of each piece of data
The Push (X), X in A is inserted, is inserted min (stack data B, X) in B. GetMin only need to perform output B.top () can
2 examples:
Input 8 I 2 I -1 I 1 Q 3 L D R Q 2 Output 2 3
Top of the stack algorithm // #include <bits / STDC ++ H.> The using namespace STD; const int = INF -0x3F3F3F3F; const int MAXN = 1000010; // int S1 [MAXN]; // int S2 [MAXN]; int SUM [MAXN]; // number x before and int f [MAXN]; // maximum number of successive front and x int main () { int CAS; ! the while (Scanf ( "% D", & CAS) the EOF = ) { Memset (SUM, 0, the sizeof (SUM)); Memset (F, INF, the sizeof F); int POS1 = 0; // int POS2 = 0; Stack <int> S1; Stack <int> S2; the while ( cas--) { String S; s.resize (. 5); Scanf ( "% S", & S [0]); IF (S [0] == 'I') { // CIN>> s1[++pos1]; int temp; scanf("%d", &temp); // cout << 666 << " " << temp << endl; s1.push(temp); pos1++; sum[pos1] = sum[pos1 - 1] + temp; f[pos1] = max(f[pos1 - 1], sum[pos1]); // cout << pos1 << " III" << f[pos1] << endl; } else if(s[0] == 'D') { // pos1--; if(s1.empty()) continue; pos1--; s1.pop(); } else if(s[0] == 'L') { if(s1.empty()) continue; // if(pos1 != 0) { // s2[++pos2] = s1[pos1--]; // } pos1--; int temp = s1.top(); s1.pop(); s2.push(temp); } else if(s[0] == 'R') { if(!s2.empty()) { // s1[++pos1] = s2[pos2--]; // sum[pos1] = sum[pos1 - 1] + s1[pos1]; // f[pos1] = max(f[pos1 - 1], sum[pos1]); int temp = s2.top(); s2.pop(); s1.push(temp); pos1++; sum[pos1] = sum[pos1 - 1] + temp; f[pos1] = max(f[pos1 - 1], sum[pos1]); } } else if(s[0] == 'Q') { int x; scanf("%d", &x); printf("%d\n", f[x]); } } } }
Example three: the sequence problem and out of the stack
Catalan number is defined using the calculated C (2N, N) / (N + 1) number of theoretical calculation involves knowledge.
#include<bits/stdc++.h> using namespace std; #define fir(i,a,b) for(int i=a;i<=b;i++) #define ll long long const ll M=1e9;//M为压位的最大值 ll a[60004],l,sum[120004]; int n; void Prime(int b,int f) { for(int j=2;j*j<=b && b!=1;j++)//质因数分解. while(b%j==0) { sum[j]+=f; b/=j; } if(b) sum[b]+=f; } void High(ll c) { for(int i=1;i<=l;i++) a[i]*=c; for(int i=1;i<=l;i++) a[i+1]+=a[i]/M,a[i]%=M;//我们需要压缩位置快速处理 while(a[l+1]) ++l; } main int () { A [. 1] =. 1, L =. 1; Scanf ( "% D", & n-); for (int I =. 1; I <= n-; I ++) // number of combinations for the two division, This question we have to use a fast prime factor decomposition method, to deal with. Prime (n-I +,. 1); for (int I = 2; I <= n-+. 1; I ++) Prime (I, -1); for (int I = 2; I <= n-2 *; I ++) for (LL J = 0; J <SUM [I]; J ++) High (I); // precision printf ( "% lld", a [L]); for (I = L-LL. 1; I; - I) the printf ( "% 09lld", A [I]); // output return 0; }
Example IV:
PS: I have another day research study above algorithm == Where is wrong (the feeling that the metaphysics error)
// // #include <bits/stdc++.h> // #include <cstdio> // #include <algorithm> // using namespace std; // typedef long long ll; // const int MAXN = 100010; // ll a[MAXN]; // int main () { // int n; // while((scanf("%d", &n) != EOF) && n) { // //memset(a, 0, sizeof a); // for(int i = 0; i < n; ++i) { // scanf("%lld", &a[i]); // } // ll p; // a[n + 1] = p = 0; // ll s[MAXN];ll w[MAXN]; // s[0] = a[0]; // w[0] = 1; // ll ans = 0; // for(int i = 1; i <= n + 1; ++i) { // if(a[i] > s[p]) { // s[++p] = a[i]; // w[p] = 1; // } // else { // ll width = 0; // while(s[p] > a[i]) { // width += w[p]; // ans = max(ans, (ll)width * s[p]); // --p; // } // s[++p] = a[i]; // w[p] = width + 1; // } // } // printf("%ld\n", ans); // } // } #include <iostream> #include <cstdio> #include <stack> using namespace std; typedef long long ll; const ll Maxn = 1e6 + 5; stack <int> s; int n, x; ll a[Maxn], m, ans, Right[Maxn], Left[Maxn]; int main () { while (~scanf ("%d", &n), n) { ans = 0; a[0] = -1; a[n + 1] = -1; for (ll i = 1; i <= n; i++) { scanf ("%lld", &a[i]); } while (!s.empty()) { s.pop(); } //从左向右 s.push(0); for (ll i = 1; i <= n; i++) { for (x = s.top(); a[x] >= a[i]; x = s.top()) { s.pop(); } Left[i] = x + 1; s.push(i); } while (!s.empty()) { s.pop(); } //从右至左 s.push(n + 1); for (int i = n; i > 0; i--) { for (x = s.top(); a[x] >= a[i]; x = s.top()) { s.pop(); } Right[i] = x - 1; s.push(i); if ((Right[i] - Left[i] + 1) * a[i] > ans) { ans = (Right[i] - Left[i] + 1) * a[i]; } } printf ("%lld\n", ans); } return 0; }