zoukankan      html  css  js  c++  java
  • BZOJ 1058: [ZJOI2007]报表统计( 链表 + set )

    这种题用数据结构怎么写都能AC吧...按1~N弄个链表然后每次插入时就更新答案, 用set维护就可以了... 

    -----------------------------------------------------------------------------------

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<set>
    #include<cctype>
     
    using namespace std;
     
    const int maxn = 500009;
     
    inline int read() {
    char c = getchar();
    int ret = 0, f = 0;
    for(; !isdigit(c); c = getchar()) if(c == '-') f = 1;
    for(; isdigit(c); c = getchar()) ret = ret * 10 + c - '0';
    return f ? -ret : ret;
    }
     
    struct L {
    int v;
    L* next;
    } mempool[maxn << 1], *pt = mempool, *head[maxn], *last[maxn];
     
    void AddL(int p, int v) {
    pt->v = v; pt->next = head[p]; head[p] = pt++;
    if(!last[p]) last[p] = head[p];
    }
     
    int N, ans;
    multiset<int> G, S;
    char s[50];
     
    void upd(int v) {
    multiset<int> :: iterator it = S.lower_bound(v);
    if(it != S.end())
    ans = min(ans, *it - v);
    if(it != S.begin())
    ans = min(ans, v - *--it);
    S.insert(v);
    }
     
    int main() {
    ans = 1 << 30;
    N = read();
    int m = read(), p, c;
    for(int i = 0; i < N; i++) {
    AddL(i, c = read());
    if(i)
    G.insert(abs(c - p));
    upd(p = c);
    }
    while(m--) {
    scanf("%s", s);
    if(s[4] == 'S')
    printf("%d ", ans);
    else if(s[4] == 'G')
    printf("%d ", *G.begin());
    else {
    int p = read() - 1, v = read();
    if(p + 1 != N) {
    G.erase(G.lower_bound(abs(head[p]->v - last[p + 1]->v)));
    G.insert(abs(v - last[p + 1]->v));
    }
    G.insert(abs(v - head[p]->v));
    upd(v);
    AddL(p, v);
    }
    }
    return 0;
    }

    ----------------------------------------------------------------------------------- 

    1058: [ZJOI2007]报表统计

    Time Limit: 15 Sec  Memory Limit: 162 MB
    Submit: 2414  Solved: 836
    [Submit][Status][Discuss]

    Description

    小Q的妈妈是一个出纳,经常需要做一些统计报表的工作。今天是妈妈的生日,小Q希望可以帮妈妈分担一些工作,作为她的生日礼物之一。经过仔细观察,小Q发现统计一张报表实际上是维护一个可能为负数的整数数列,并且进行一些查询操作。在最开始的时候,有一个长度为N的整数序列,并且有以下三种操作: INSERT i k 在原数列的第i个元素后面添加一个新元素k; 如果原数列的第i个元素已经添加了若干元素,则添加在这些元素的最后(见下面的例子) MIN_GAP 查询相邻两个元素的之间差值(绝对值)的最小值 MIN_SORT_GAP 查询所有元素中最接近的两个元素的差值(绝对值) 例如一开始的序列为 5 3 1 执行操作INSERT 2 9将得到: 5 3 9 1 此时MIN_GAP为2,MIN_SORT_GAP为2。 再执行操作INSERT 2 6将得到: 5 3 9 6 1 注意这个时候原序列的第2个元素后面已经添加了一个9,此时添加的6应加在9的后面。这个时候MIN_GAP为2,MIN_SORT_GAP为1。于是小Q写了一个程序,使得程序可以自动完成这些操作,但是他发现对于一些大的报表他的程序运行得很慢,你能帮助他改进程序么?

    Input

    第一行包含两个整数N,M,分别表示原数列的长度以及操作的次数。第二行为N个整数,为初始序列。接下来的M行每行一个操作,即“INSERT i k”,“MIN_GAP”,“MIN_SORT_GAP”中的一种(无多余空格或者空行)。

    Output

    对于每一个“MIN_GAP”和“MIN_SORT_GAP”命令,输出一行答案即可。

    Sample Input

    3 5
    5 3 1
    INSERT 2 9
    MIN_SORT_GAP
    INSERT 2 6
    MIN_GAP
    MIN_SORT_GAP

    Sample Output

    2
    2
    1

    HINT

    对于100%的数据,N , M ≤500000 对于所有的数据,序列内的整数不超过5*10^8。

    Source

  • 相关阅读:
    hibernate 报query result offset is not supported
    MAC下 mySQL及workbench安装
    FC105 FC106 Scale功能块使用说明
    报错!!!Servlet.service() for servlet [action] in context with path [/myssh] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException
    PLC300寻址指令
    vscode 配置
    Integrate Intellij with Cygwin
    Configure VSCode
    Linux下安装gradle
    Linux下安装nodejs-源码安装
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5029035.html
Copyright © 2011-2022 走看看