zoukankan      html  css  js  c++  java
  • Intel Code Challenge Elimination Round (Div.1 + Div.2, combined) C. Destroying Array

    C. Destroying Array
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given an array consisting of n non-negative integers a1, a2, ..., an.

    You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.

    After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.

    Input

    The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.

    The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).

    The third line contains a permutation of integers from 1 to n — the order used to destroy elements.

    Output

    Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.

    Examples
    input
    4
    1 3 2 5
    3 4 1 2
    output
    5
    4
    3
    0
    input
    5
    1 2 3 4 5
    4 2 3 5 1
    output
    6
    5
    5
    1
    0
    input
    8
    5 5 4 4 6 6 5 5
    5 2 8 7 1 3 4 6
    output
    18
    16
    11
    8
    8
    6
    6
    0
    Note

    Consider the first sample:

    1. Third element is destroyed. Array is now 1 3  *  5. Segment with maximum sum 5 consists of one integer 5.
    2. Fourth element is destroyed. Array is now 1 3  *   * . Segment with maximum sum 4 consists of two integers 1 3.
    3. First element is destroyed. Array is now  *  3  *   * . Segment with maximum sum 3 consists of one integer 3.
    4. Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.

    题意:n个数,删除点的顺序,求每次最大的连续和;

    思路:并查集,倒着写;线段树也可以;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll long long
    #define pi (4*atan(1.0))
    #define eps 1e-14
    const int N=2e5+10,M=4e6+10,inf=1e9+10,mod=1e9+7;
    const ll INF=1e18+10;
    int fa[N],flag[N];
    ll sum[N],ans[N],a[N],b[N];
    int fa18(int x)
    {
        return x==fa[x]?x:fa[x]=fa18(fa[x]);
    }
    void update(int u,int v,int i)
    {
        int x=fa18(u);
        int y=fa18(v);
        if(x!=y)
        {
            fa[x]=y;
            sum[y]+=sum[x];
            ans[i]=max(ans[i+1],max(ans[i],sum[y]));
        }
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%d",&b[i]);
        for(int i=1;i<=n;i++)
            fa[i]=i,sum[i]=a[i];
        for(int i=n;i>=1;i--)
        {
            if(b[i]==fa[b[i]])
            ans[i]=max(ans[i+1],max(sum[b[i]],ans[i]));
            if(flag[b[i]-1])update(b[i]-1,b[i],i);
            if(flag[b[i]+1])update(b[i]+1,b[i],i);
            flag[b[i]]=1;
        }
        for(int i=2;i<=n+1;i++)
        {
            printf("%lld ",ans[i]);
        }
        return 0;
    }
  • 相关阅读:
    原创:搜索算法之两个数组取交集的算法
    原创:中文分词的逆向最大匹配算法
    搜索推荐系统根据用户搜索频率(热搜)排序
    原创:Solr Wiki 中关于Suggester(搜索推荐)的简单解读
    从海量文本中统计出前k个频率最高的词语
    原创:从海量数据中查找出前k个最小或最大值的算法(java)
    NOIWC2019 懵逼记
    BZOJ 4568: [Scoi2016]幸运数字(倍增+线性基)
    BZOJ 3207: 花神的嘲讽计划Ⅰ(莫队+哈希)
    BZOJ 3653: 谈笑风生(主席树)
  • 原文地址:https://www.cnblogs.com/jhz033/p/5927837.html
Copyright © 2011-2022 走看看