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;
    }
    
  • 相关阅读:
    VBS 操作Word
    VirtualMachineManager
    openssl用法详解
    JS获取浏览器窗口大小 获取屏幕,浏览器,网页高度宽度
    js实现touch移动触屏滑动事件
    JavaScript 获得代码行号和脚本文件名
    js的调用函数前先执行某语句问题
    结构型模式概述(Structural Pattern)
    适配器模式(Adapter Pattern)----------结构型模式
    CSAPP chapter2 记录(bit_level_coding)
  • 原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/6349338.html
Copyright © 2011-2022 走看看