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;
    }
  • 相关阅读:
    Yii “CDbConnection failed to open the DB connection: could not find driver"解决办法
    安装多个PHP环境会导致phpinfo和php -v中查看到的PHP版本不一致
    sql pivot、unpivot和partition by用法
    sql把字符数组转换成表
    sql获取数组指定元素
    sql获取数组长度
    Razor基础语法
    ADO.NET基础
    Asp.Net网站统一处理错误信息
    WebApp页面开发小结
  • 原文地址:https://www.cnblogs.com/jhz033/p/5927837.html
Copyright © 2011-2022 走看看