zoukankan      html  css  js  c++  java
  • [codeforces722C]Destroying Array

    [codeforces722C]Destroying Array

    试题描述

    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 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.

    输入

    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.

    输出

    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.

    输入示例

    8
    5 5 4 4 6 6 5 5
    5 2 8 7 1 3 4 6

    输出示例

    18
    16
    11
    8
    8
    6
    6
    0

    数据规模及约定

    见“输入

    题解

    倒着做,用并查集。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <cstring>
    #include <string>
    #include <map>
    #include <set>
    using namespace std;
    
    const int BufferSize = 1 << 16;
    char buffer[BufferSize], *Head, *Tail;
    inline char Getchar() {
    	if(Head == Tail) {
    		int l = fread(buffer, 1, BufferSize, stdin);
    		Tail = (Head = buffer) + l;
    	}
    	return *Head++;
    }
    int read() {
    	int x = 0, f = 1; char c = Getchar();
    	while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
    	while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
    	return x * f;
    }
    
    #define maxn 100010
    #define LL long long
    int val[maxn], ord[maxn];
    LL ans[maxn];
    
    int fa[maxn];
    LL sum[maxn];
    int findset(int x) { return x == fa[x] ? x : fa[x] = findset(fa[x]); }
    
    int main() {
    	int n = read();
    	for(int i = 1; i <= n; i++) val[i] = read();
    	for(int i = 1; i <= n; i++) ord[i] = read();
    	
    	for(int i = 1; i <= n; i++) sum[i] = val[i], fa[i] = 0;
    	LL mx = 0;
    	for(int i = n; i; i--) {
    		ans[i] = mx;
    		int u = ord[i];
    		fa[u] = u;
    		if(fa[u-1]) {
    			int v = findset(u - 1);
    			fa[v] = u; sum[u] += sum[v];
    		}
    		if(fa[u+1]) {
    			int v = findset(u + 1);
    			fa[v] = u; sum[u] += sum[v];
    		}
    		mx = max(mx, sum[u]);
    	}
    	
    	for(int i = 1; i <= n; i++) printf("%I64d
    ", ans[i]);
    	
    	return 0;
    }
    
  • 相关阅读:
    P3723 【[AH2017/HNOI2017]礼物】
    P4555 【[国家集训队]最长双回文串】
    manacher学习笔记
    多项式学习笔记
    题解 CF1097F 【Alex and a TV Show】
    docker数据卷容器
    python基础学习
    docker监控之cadvisor
    docker监控系统
    ssh配置详解及公私钥批量分发
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6349338.html
Copyright © 2011-2022 走看看