1. 程式人生 > >【HDU 4699】 Editor

【HDU 4699】 Editor

return con %d edit sum inline span private ace

【題目鏈接】

http://acm.hdu.edu.cn/showproblem.php?pid=4699

【算法】

維護兩個棧,一個棧放光標之前的數,另外一個放光標之後的數

在維護棧的同時求最大前綴和,即可

【代碼】

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 5;
const int INF = 2e9;

class mstack
{
        private :
                
int tot; int s[MAXN]; public : inline void clear() { tot = 0; } inline void push(long long x) { tot++; s[tot] = x; } inline
void pop() { tot--; } inline long long top() { return s[tot]; } inline bool empty() { return tot == 0; } } s1,s2;
int q,pos; long long x; long long sum[MAXN],f[MAXN]; char opt[5]; int main() { while (scanf("%d",&q) != EOF) { f[0] = -INF; s1.clear(); s2.clear(); pos = 0; while (q--) { scanf("%s",&opt); if (opt[0] == I) { scanf("%lld",&x); s1.push(x); pos++; sum[pos] = sum[pos-1] + x; f[pos] = max(f[pos-1],sum[pos]); } if (opt[0] == D) { if (s1.empty()) continue; s1.pop(); pos--; } if (opt[0] == L) { if (s1.empty()) continue; x = s1.top(); s1.pop(); s2.push(x); pos--; } if (opt[0] == R) { if (s2.empty()) continue; x = s2.top(); s2.pop(); s1.push(x); pos++; sum[pos] = sum[pos-1] + x; f[pos] = max(f[pos-1],sum[pos]); } if (opt[0] == Q) { scanf("%lld",&x); printf("%lld\n",f[x]); } } } return 0; }

【HDU 4699】 Editor