zoukankan      html  css  js  c++  java
  • cf 442C. 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

    并不会做.

    接触了一个交单调栈的东西...

    又是单调栈又是单调队列

    是时候仔细了解下了.

     1 /*************************************************************************
     2     > File Name: code/2015summer/#3/D.cpp
     3     > Author: 111qqz
     4     > Email: rkz2013@126.com 
     5     > Created Time: 2015年07月28日 星期二 13时47分48秒
     6  ************************************************************************/
     7 
     8 #include<iostream>
     9 #include<iomanip>
    10 #include<cstdio>
    11 #include<algorithm>
    12 #include<cmath>
    13 #include<cstring>
    14 #include<string>
    15 #include<map>
    16 #include<set>
    17 #include<queue>
    18 #include<vector>
    19 #include<stack>
    20 #define y0 abc111qqz
    21 #define y1 hust111qqz
    22 #define yn hez111qqz
    23 #define j1 cute111qqz
    24 #define tm crazy111qqz
    25 #define lr dying111qqz
    26 using namespace std;
    27 #define REP(i, n) for (int i=0;i<int(n);++i)  
    28 typedef long long LL;
    29 typedef unsigned long long ULL;
    30 const int N=5E5+7;
    31 int a[N],b[N];
    32 int n;
    33 int main()
    34 {
    35     cin>>n;
    36     int x;
    37     int top = -1;
    38     LL ans = 0;
    39     for ( int i = 1 ; i <= n ; i++ )
    40     {
    41       scanf("%d",&x);
    42       while (top>=1&&a[top-1]>=a[top]&&a[top]<=x)            //维护了一个单调栈
    43       {
    44           ans = ans + min(x,a[top-1]);
    45           top--;
    46       }
    47       top++;
    48       a[top]=x;
    49     }                                                        
    50 
    51 
    52     sort(a,a+top+1);
    53     for ( int i = 0 ; i <top-1 ; i++)
    54     {
    55     ans = ans + a[i];
    56     }
    57     cout<<ans<<endl;
    58     
    59   
    60     return 0;
    61 }
  • 相关阅读:
    springboot2 整合雪花算法,并兼容分布式部署
    docker 在 linux 搭建私有仓库
    jdbc 几种关系型数据库的连接 和 driver_class,以及简单的使用
    springboot2 整合发送邮件的功能
    oracle 唯一新约束 和 逻辑删除的 冲突处理办法
    oracle 一些常见操作方法
    spring-cloud-stream 整合 rabbitmq
    springboot2 整合 rabbitmq
    docker 安装 rabbitmq 消息队列
    网络统计学目录
  • 原文地址:https://www.cnblogs.com/111qqz/p/4684881.html
Copyright © 2011-2022 走看看