zoukankan      html  css  js  c++  java
  • Codeforces 442C

    题目链接

    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
    
    
    Accepted Code:
     1 /*************************************************************************
     2     > File Name: E.cpp
     3     > Author: Stomach_ache
     4     > Mail: sudaweitong@gmail.com
     5     > Created Time: 2014年06月23日 星期一 22时48分29秒
     6     > Propose: 
     7  ************************************************************************/
     8 
     9 #include <cmath>
    10 #include <string>
    11 #include <vector>
    12 #include <cstdio>
    13 #include <fstream>
    14 #include <cstring>
    15 #include <iostream>
    16 #include <algorithm>
    17 using namespace std;
    18 
    19 #define min(x, y) ((x) < (y) ? (x) : (y))
    20 typedef long long LL;
    21 int n, a[500005];
    22 int b[500005];
    23 
    24 int
    25 main(void) {
    26       while (~scanf("%d", &n) && n) {
    27           for (int i = 1; i <= n; i++) {
    28               scanf("%d", a + i);
    29         }
    30           if (n <= 2) {
    31               puts("0"); continue;
    32         }
    33         LL  ans = 0;
    34         int cnt = 0;
    35         for (int i = 1; i <= n; i++) {
    36               b[cnt++] = a[i];
    37               while (cnt > 2 && b[cnt-2] <= b[cnt-1] && b[cnt-2] <= b[cnt-3]) {
    38                   ans += min(b[cnt-1], b[cnt-3]);
    39                 b[cnt-2] = b[cnt-1];
    40                 cnt--;
    41             }
    42         }    
    43         for (int i = 1; i < cnt-1; i++) {
    44               ans += min(b[i-1], b[i+1]);
    45         }
    46         printf("%I64d
    ", ans);
    47     }
    48 
    49     return 0;
    50 }
     
    
    
  • 相关阅读:
    【Python3 爬虫】U27_多线程爬虫之Queue线程安全队列
    【Python3 爬虫】U26_多线程爬虫之生产者与消费者模式
    【Python3 爬虫】U25_多线程爬虫之多线程共享全局变量及锁机制
    【Python3 爬虫】U24_多线程爬虫之Thread类创建多线程
    Verilog 加法器和减法器(1)
    逻辑门电路详解
    RV32C指令集
    RV32A/RV64A指令集
    RV32M/RV64M指令集
    Risc-V指令集中文文档
  • 原文地址:https://www.cnblogs.com/Stomach-ache/p/3805862.html
Copyright © 2011-2022 走看看