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;
    }
  • 相关阅读:
    冒险岛数据封包分析
    【转】安装版系统封装入门详细解说
    拦截其它程序的网络数据封包
    封包加密解密-01
    vs2010下载Microsoft Visual Studio 2010 Express(vs2010中文版下载)速成官方合集正式版
    《Visual C++开发实战1200例 第1卷》扫描版[PDF]
    《Visual Basic开发实战1200例》包括第I卷、第II卷共计1200个例子,本书是第I卷,共计600个例子。
    服务器上常见软件
    HDU1686
    HDU3336
  • 原文地址:https://www.cnblogs.com/jhz033/p/5927837.html
Copyright © 2011-2022 走看看