双向链表直接模拟。
用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和。
因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可。
时间复杂度O(n)。
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAXN = ;
const int INF = << ; struct node
{
int val;
node *next;
node *pre;
}; int sum[MAXN];
int maxSum[MAXN]; int main()
{
//freopen( "1004.in", "r", stdin );
//freopen( "test.txt", "w", stdout );
int Q;
node *head = ( node *)malloc(sizeof(node)); while ( scanf( "%d", &Q ) == )
{
head->pre = NULL;
head->next = NULL; node *cur = head;
maxSum[] = -INF;
sum[] = ;
int pos = ; char op[];
int a;
for ( int i = ; i < Q; ++i )
{
scanf( "%s", op );
if ( op[] == 'I' )
{
scanf( "%d", &a ); ++pos;
sum[pos] = sum[pos-] + a;
maxSum[pos] = max( sum[pos], maxSum[pos-] ); node *p = (node *)malloc( sizeof(node) );
p->val = a;
p->next = NULL;
p->pre = cur; if ( cur->next )
{
p->next = cur->next;
cur->next->pre = p;
} cur->next = p;
cur = cur->next;
}
else if ( op[] == 'Q' )
{
scanf( "%d", &a );
if ( pos < a ) a = pos;
printf( "%d\n", maxSum[a] );
}
else if ( op[] == 'L' )
{
if ( cur->pre != NULL )
{
cur = cur->pre;
--pos;
}
}
else if ( op[] == 'R' )
{
//printf( "cur->next=%d %d\n", cur->next, NULL );
if ( cur->next != NULL )
{
cur = cur->next;
++pos;
sum[pos] = sum[pos - ] + cur->val;
maxSum[pos] = max( sum[pos], maxSum[pos-] );
}
}
else if ( op[] == 'D' )
{
--pos; node *p = cur;
cur = p->pre; cur->next = p->next; if ( p->next )
p->next->pre = cur; free(p);
}
/*
node *pp = head->next;
while ( pp )
{
printf( "%d ", pp->val );
pp = pp->next;
}
puts("");
*/
}
}
return ;
}