双向链表直接模拟。

用一个辅助数组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 ;
}

最新文章

  1. 【翻译】首个基于NHibernate的应用程序
  2. hdu 4911Inversion
  3. Jmeter中通过BeanShell获取当前时间
  4. Linux学习之八——利用变量
  5. (10)nehe教程4--旋转
  6. 3、Spring整合Hibernate
  7. redis ins 调试
  8. [Python爬虫笔记][随意找个博客入门(一)]
  9. CCF CSP 201503-2 数字排序 (map+自定义排序)
  10. 图解HTTPS协议
  11. FileStream说明
  12. spring boot Tomcat文件上传找不到零时文件夹
  13. 为datagrid、treegrid增加右键表头菜单,用于显示或隐藏列,注意:冻结列不在此菜单中
  14. 关于bottle WEB框架中签名cookie的一点理解
  15. django学习~第四篇
  16. 我是陌生人 Java中导入、导出Excel
  17. centos 6.9 +nginx 配置GIT HTTPS服务器(证书采用自签名)
  18. LeetCode题解之Find All Duplicates in an Array
  19. POJ1251 Jungle Roads (最小生成树&amp;Kruskal&amp;Prim)题解
  20. php Pthread 多线程 (六) Pool类 线程池

热门文章

  1. list 用法的随手记
  2. unity简单例子
  3. theano提示:g++ not detected的解决办法
  4. ROS机器人程序设计
  5. Excle 常用函数
  6. ReactiveObjC框架的简单介绍
  7. Dijkstra&amp;&amp;Floyd
  8. C#+Winform开发窗体程序
  9. Percona-Tookit工具包之pt-pmp
  10. python__标准库 : 正则表达式(re)