zoukankan      html  css  js  c++  java
  • HDU 4699 Editor (2013多校10,1004题)

    Editor

    Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 118    Accepted Submission(s): 38


    Problem Description
     
    Sample Input
    8 I 2 I -1 I 1 Q 3 L D R Q 2
     
    Sample Output
    2 3
    Hint
    The following diagram shows the status of sequence after each instruction:
     
    Source
     
    Recommend
    zhuyuanchen520
     

    这题只要用双向链表模拟一下。

    查询最大前缀和,相当于维护一个单调队列。

      1 /* ***********************************************
      2 Author        :kuangbin
      3 Created Time  :2013/8/22 13:38:35
      4 File Name     :F:2013ACM练习2013多校101004.cpp
      5 ************************************************ */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <iostream>
     10 #include <algorithm>
     11 #include <vector>
     12 #include <queue>
     13 #include <set>
     14 #include <map>
     15 #include <string>
     16 #include <math.h>
     17 #include <stdlib.h>
     18 #include <time.h>
     19 using namespace std;
     20 const int MAXN = 1000010;
     21 
     22 int a[MAXN];
     23 int next[MAXN],pre[MAXN],tot;
     24 int NewNode()
     25 {
     26     next[tot] = -1;
     27     pre[tot] = -1;
     28     tot++;
     29     return tot-1;
     30 }
     31 int root;
     32 int sum[MAXN];
     33 vector<int>vec;
     34 int now;
     35 int n;
     36 void init()
     37 {
     38     tot = 0;
     39     root = NewNode();
     40     vec.clear();
     41     n = now = 0;
     42 }
     43 
     44 void I(int x)
     45 {
     46     n++;
     47     int t = NewNode();
     48     a[t] = x;
     49     pre[t] = now;
     50     next[t] = next[now];
     51     if(next[now] != -1)pre[next[now]] = t;
     52     next[now] = t;
     53     now = t;
     54     if(n == 1)
     55     {
     56         sum[n] = x;
     57         vec.push_back(n);
     58     }
     59     else
     60     {
     61         sum[n] = sum[n-1] + x;
     62         int sz = vec.size();
     63         if(sum[n] > sum[vec[sz-1]])
     64             vec.push_back(n);
     65     }
     66 }
     67 void D()
     68 {
     69     if(n == 0)return;
     70     int sz = vec.size();
     71     if(vec[sz-1] == n)
     72         vec.pop_back();
     73     n--;
     74     int t = pre[now];
     75     next[t] = next[now];
     76     if(next[now] != -1)pre[next[now]] = t;
     77     now = t;
     78 }
     79 void L()
     80 {
     81     if(n == 0)return;
     82     int sz = vec.size();
     83     if(vec[sz-1] == n)
     84         vec.pop_back();
     85     now = pre[now];
     86     n--;
     87 }
     88 void R()
     89 {
     90     if(next[now] == -1)return;
     91     n++;
     92     now = next[now];
     93     if(n == 1)
     94     {
     95         sum[n] = a[now];
     96         vec.push_back(n);
     97     }
     98     else
     99     {
    100         int sz = vec.size();
    101         sum[n] = sum[n-1] + a[now];
    102         if(sum[n] > sum[vec[sz-1]])
    103             vec.push_back(n);
    104     }
    105 }
    106 
    107 
    108 int Q(int k)
    109 {
    110     int sz = vec.size();
    111     if(vec[sz-1] <= k)
    112         return sum[vec[sz-1]];
    113     int t = upper_bound(vec.begin(),vec.end(),k) - vec.begin();
    114     return sum[vec[t-1]];
    115 }
    116 
    117 
    118 int main()
    119 {
    120     //freopen("in.txt","r",stdin);
    121     //freopen("out.txt","w",stdout);
    122     int M;
    123     int x;
    124     char op[10];
    125     while(scanf("%d",&M) == 1)
    126     {
    127         init();
    128         while(M--)
    129         {
    130             scanf("%s",&op);
    131             if(op[0] == 'I')
    132             {
    133                 scanf("%d",&x);
    134                 I(x);
    135             }
    136             else if(op[0] == 'D')
    137                 D();
    138             else if(op[0] == 'L')
    139                 L();
    140             else if(op[0] == 'R')
    141                 R();
    142             else
    143             {
    144                 scanf("%d",&x);
    145                 printf("%d
    ",Q(x));
    146             }
    147         }
    148     }
    149     return 0;
    150 }
  • 相关阅读:
    Asp.net2.0 中自定义过滤器对Response内容进行处理 dodo
    自动化测试工具 dodo
    TestDriven.NET 2.0——单元测试的好助手(转) dodo
    JS弹出窗口的运用与技巧 dodo
    ElasticSearch 简介 规格严格
    修改PostgreSQL字段长度导致cached plan must not change result type错误 规格严格
    Linux系统更改时区(转) 规格严格
    mvn编译“Cannot find matching toolchain definitions for the following toolchain types“报错解决方法 规格严格
    ElasticSearch 集群 & 数据备份 & 优化 规格严格
    Elasticsearch黑鸟教程22:索引模板的详细介绍 规格严格
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3275587.html
Copyright © 2011-2022 走看看