zoukankan      html  css  js  c++  java
  • cf442C Artem and Array

    C. Artem and Array
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Artem has an array of n positive integers. Artem decided to play with it. The game consists of n moves. Each move goes like this. Artem chooses some element of the array and removes it. For that, he gets min(a, b) points, where a and b are numbers that were adjacent with the removed number. If the number doesn't have an adjacent number to the left or right, Artem doesn't get any points.

    After the element is removed, the two parts of the array glue together resulting in the new array that Artem continues playing with. Borya wondered what maximum total number of points Artem can get as he plays this game.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 5·105) — the number of elements in the array. The next line contains n integers ai (1 ≤ ai ≤ 106) — the values of the array elements.

    Output

    In a single line print a single integer — the maximum number of points Artem can get.

    Sample test(s)
    input
    5
    3 1 5 2 6
    
    output
    11
    
    input
    5
    1 2 3 4 5
    
    output
    6
    
    input
    5
    1 100 101 100 1
    
    output
    102

    贪心啊……

    爆出内幕:昨天cf比赛的时候,zld大神觉得此题贪心,结果没开long long第10个点wa。

    于是卓神信誓旦旦的说,这题贪心有反例。呵呵呵……

    首先“填坑”:把两边都比中间大的数直接合掉,结果它就变成单调栈……

    维护单调栈。最后sort一遍更新答案 

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    int n,top,zhan[500001];
    long long ans;
    int main()
    {
    	scanf("%d",&n);
    	for (int i=1;i<=n;i++)
    	{
    		int x;
    		scanf("%d",&x);
    		while (top>1 && zhan[top-1]>=zhan[top]&& zhan[top]<=x) 
    		{
    			ans+=min(zhan[top-1],x);
    			top--;
    		}
    		zhan[++top]=x;
    	}
    	sort(zhan+1,zhan+top+1);
    	for (int i=1;i<=top-2;i++)
    	{
    		ans+=zhan[i];
    	}
    	printf("%lld",ans);
    }


  • 相关阅读:
    低调 、隐忍、善良应是最应该修炼的
    达内C++培训课程
    这三天低效率开发的总结,我都做了些什么啊?
    linux sysfs(1)
    编码问题
    Linux中的system函数的实现和解释
    北京邮电大学 程序设计课程设计 电梯 文件输入版本(已调试,大致正确运行==)
    "Dallas" CTP3 发布通告
    结合使用PowerPivot 和 "Dallas" CTP3
    Windows Azure 解决方案系列: 能源监测减少支出,通过托管平台拓展业务
  • 原文地址:https://www.cnblogs.com/mfmdaoyou/p/7209269.html
Copyright © 2011-2022 走看看