HDU—4699 Editor 双向链表+子集和
惨。今天聪哥选了2013 多校10做训练,结果一题都没做出来。这个题目是数据结构,正好是我强项
如果只是插入 删除 光标左右移动,那都小菜,用链表全解决,关键是那个Q k 要求 a1到aq的连续子序列和最大(子序列一定要以a1开头)。
然后我用记录每个节点的当前最大和总体最大,这样只要知道第k个是哪个数就可以了,但由于是用的链表,并不知道第k个是哪个。。一开始试了下暴力去扫,结果TL。。
其实我能想到每个点由前一个点过渡最大值,就离答案不远了,既然我不知道当前节点是第几个数,我就手动加一个量,专门记录数量的增减嘛。。然后用一个数组专门记录第几个数的最大值是多少,也可以用向量咯
哎,当时比赛的时候居然没想到
还有种用双桟的方法写的,Q处理一样的,只是用双桟写比链表要好写一些,因为题目只对光标所在处操作,故光标前弄个桟 光标后弄个桟 即可。
代码里真正查询的是 sum数组和v向量,d和ans是未修改代码残留的,没用,其实用向量还是写复杂了,直接另外用个数组记录第几个数的最大值就行了,遇到k直接输出更方便
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
const int N = +;
int cnt;
int Q,S,T,cur,n;
vector<int> v;
int sum[N];
struct node
{
int pre,next;
int xi;
int d;
int ans;
} num[N];
void SI()
{
n++;
int x;
scanf("%d",&x);
sum[n]=sum[n-]+x;
node* now=&num[cnt];
//插入操作
now->pre=cur;
now->next=num[cur].next;
num[cur].next=cnt;
num[now->next].pre=cnt;
cur=cnt;
cnt++;
now->xi=x;
now->d=x;
now->ans=x;
if (now->pre!=S){
node* bf=&num[now->pre];
now->d=bf->d+x;
now->ans=now->d;
now->ans=max(now->ans,bf->ans);
}
int r=v.size();
if (now->pre==S){
v.push_back(n);
}
else{
if ((sum[n])>sum[v[r-]]){
v.push_back(n);
}
}
}
void SD()
{
if (cur==S) return;
int r=v.size();
if (n==v[r-]) v.pop_back();
n--;
node* bf=&num[num[cur].pre];
node* af=&num[num[cur].next];
bf->next=num[cur].next;
af->pre=num[cur].pre;
if (num[cur].next!=T){
af->d=bf->d+af->xi;
af->ans=af->d;
af->ans=max(af->ans,bf->ans);
}
cur=num[cur].pre;
}
void SL()
{
if (cur!=S){
int r=v.size();
if (n==v[r-]) v.pop_back();
n--;
cur=num[cur].pre;
}
}
void SR()
{
if (num[cur].next!=T){
cur=num[cur].next;
n++;
int r=v.size();
sum[n]=sum[n-]+num[cur].xi;
if (sum[n]>sum[v[r-]]) v.push_back(n);
}
}
void query()
{
int k;
scanf("%d",&k);
int r=v.size();
if (v[r-]<=k) printf("%d\n",sum[v[r-]]);
else {
int loc=upper_bound(v.begin(),v.end(),k)-v.begin();
printf("%d\n",sum[v[loc-]]);
}
}
void test()
{
cout<<"Test:"<<endl;
cout<<cur<<endl;
for (int i=S;i!=T;i=num[i].next){
cout<<num[i].xi<<" ";
}
cout<<endl;
for (int i=;i<v.size();i++)
cout<<v[i]<<"-"<<sum[v[i]]<<" ";
cout<<endl;
}
int main()
{
char ch[];
while (scanf("%d",&Q)!=EOF)
{
v.clear();
cnt=;
n=;
S=,T=N-;
sum[S]=sum[T]=;
v.push_back(S);
num[S].next=T;
num[S].ans=num[S].d=num[S].xi=;
num[T].ans=num[T].d=num[T].xi=;
num[T].pre=S;
cur=S;
while (Q--){
//test();
scanf("%s",ch);
if (ch[]=='I'){
SI();
}
else
if (ch[]=='D'){
SD();
}
else
if (ch[]=='L'){
SL();
}
else
if (ch[]=='R'){
SR();
}
else{
query();
}
}
}
return ;
}
HDU—4699 Editor 双向链表+子集和的更多相关文章
- HDU 4699 Editor(双向链表)
双向链表直接模拟. 用一个辅助数组maxSum来维护一下前k项中[1,k]的最大和. 因为光标是一格一格的移动,所以每次光标右移的时候动态更新一下即可. 时间复杂度O(n). #include < ...
- HDU 4699 - Editor - [对顶栈]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4699 Problem Description Sample Input8I 2I -1I 1Q 3LD ...
- HDU 4699 Editor (2013多校10,1004题)
Editor Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Su ...
- hdu 4699 Editor 模拟栈
思路:刚开始用STL中的栈,一直RE……,之后改为手动模拟栈操作,在注意点细节就可以了!!! 代码如下: #include<cstdio> #include<cstring> ...
- HDU 4699 Editor 维护栈
维护两个栈,分别存光标前和光标后的数 再维护前缀和的栈 和 前缀和最大值的栈 注意一下左移,右移,删除到顶了就不操作了 5个操作 I x : 光标处插入x -----> s1.push(x) ...
- HDU 4699 Editor(模拟 对顶栈)
题目大意: 给定一个整数序列 维护5种操作 次数<1e6 I x: 光标位置插入x 然后光标位于x之后 D: 删除光标前一个数 L: 光标左移 R: 光标右移 Q k: 询问位置k之前的最大前缀 ...
- [置顶] hdu 4699 2个栈维护 or 伸展树
hdu 4699 Editor 题意:对一个数列进行操作,光标位置后面插入一个权值为x的数,删除光标前的那个数,光标左移一位,光标右移一位,求到k位置的最大的前缀和.. 注意这里的k是在光标之前的, ...
- Project Euler 106:Special subset sums: meta-testing 特殊的子集和:元检验
Special subset sums: meta-testing Let S(A) represent the sum of elements in set A of size n. We shal ...
- Project Euler 103:Special subset sums: optimum 特殊的子集和:最优解
Special subset sums: optimum Let S(A) represent the sum of elements in set A of size n. We shall cal ...
随机推荐
- 每个项目中,你必须知道的11个Java第三方类库。
Java第三方library ecosystem是一个很广阔的范畴.不久前有人撰文:每个项目中,你必须知道的11个Java第三方类库. 单元测试 1.DBUnit DBunit是一个基于junit扩展 ...
- C# 创建Windows Service(Windows服务)程序
本文介绍了如何用C#创建.安装.启动.监控.卸载简单的Windows Service 的内容步骤和注意事项. 一.创建一个Windows Service 1)创建Windows Service项目 2 ...
- 获取QQ群中的所有群友QQ
package com.jm.mail.tools; import java.io.BufferedReader; import java.io.IOException; import java.io ...
- arm linux 移植 PHP
背景: PHP 是世界上最好的语言. host平台 :Ubuntu 16.04 arm平台 : 3531d arm-gcc :4.9.4 php :7.1.30 zlib :1.2.11 libxml ...
- XV6源代码阅读-进程线程
Exercise1 源代码阅读 1.基本头文件:types.h param.h memlayout.h defs.h x86.h asm.h mmu.h elf.h types.h:仅仅是定义uint ...
- jenkins#构建并部署springboot的jar包
0. 前提是有一个可以用的没有问题的Jenkins环境,这是基础 1. 安装publish over ssh 插件,(如果网速太慢,请去github 克隆代码,然后自己构建,然后上传安装此插件) 2. ...
- Combobox出现System.Data.DataRowView的原因
这种情况多次遇到.有时候明明完全相同的代码,在不同的场景运行却是两种结果, 其中一种坏的结果就是 comboBox所有的项都显示为System.Data.DataRowView 今天仔研究了一下,应该 ...
- c# copydata 消息
using PublicCode; using System; using System.Collections.Generic; using System.ComponentModel; using ...
- 清除windows激活信息
1.管理员运行命令提示符 在命令提示符中输入 slmgr /upk---删除当前KMS密匙 出现"成功地卸载了产品密匙"后,继续依次执行下面两个命令 slmgr /ckms---此 ...
- use matplotlib to drew a table
$sudo apt-get install python3-matplotlib gyf@gyf-VirtualBox:~$ python3Python 3.6.9 (default, Nov 7 ...