2 条题解
-
0小鸟 (slz钟鼎皓) LV 8 @ 2024-7-28 15:28:10
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stack>
using namespace std;
#define INF 0x3f3f3f3f const int maxn = 1e6 + 7; stack<int>s1,s2; int f[maxn],sum[maxn]; int main() { int Q;scanf("%d",&Q); sum[0] = 0; int cnt = 0; memset(f,-INF,sizeof(f)); while(Q--) { char op[10];int n; scanf("%s",op); if(op[0] == 'I') { cnt++; scanf("%d",&n); sum[cnt] = sum[cnt - 1] + n; f[cnt] = max(f[cnt - 1],sum[cnt]); s1.push(n); } else if(op[0] == 'D') { if(!s1.empty()) { cnt--; s1.pop(); } } else if(op[0] == 'L') { if(!s1.empty()) { cnt--; int tmp = s1.top(); s2.push(tmp); s1.pop(); } } else if(op[0] == 'R') { if(!s2.empty()) { cnt++; int tmp = s2.top(); sum[cnt] = sum[cnt - 1] + tmp; f[cnt] = max(f[cnt - 1],sum[cnt]); s1.push(tmp); s2.pop(); } } else if(op[0] == 'Q') { scanf("%d",&n); printf("%d\n",f[n]); } } return 0; }
-
02024-6-22 20:16:50@
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> #include <stack>
using namespace std;
#define INF 0x3f3f3f3f const int maxn = 1e6 + 7; stack<int>s1,s2; int f[maxn],sum[maxn]; int main() { int Q;scanf("%d",&Q); sum[0] = 0; int cnt = 0; memset(f,-INF,sizeof(f)); while(Q--) { char op[10];int n; scanf("%s",op); if(op[0] == 'I') { cnt++; scanf("%d",&n); sum[cnt] = sum[cnt - 1] + n; f[cnt] = max(f[cnt - 1],sum[cnt]); s1.push(n); } else if(op[0] == 'D') { if(!s1.empty()) { cnt--; s1.pop(); } } else if(op[0] == 'L') { if(!s1.empty()) { cnt--; int tmp = s1.top(); s2.push(tmp); s1.pop(); } } else if(op[0] == 'R') { if(!s2.empty()) { cnt++; int tmp = s2.top(); sum[cnt] = sum[cnt - 1] + tmp; f[cnt] = max(f[cnt - 1],sum[cnt]); s1.push(tmp); s2.pop(); } } else if(op[0] == 'Q') { scanf("%d",&n); printf("%d\n",f[n]); } } return 0; }
- 1
信息
- ID
- 40
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- 2
- 标签
- 递交数
- 80
- 已通过
- 47
- 上传者