码迷,mamicode.com
首页 > 其他好文 > 详细

442C

时间:2017-07-12 01:09:37      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:closed   clu   bit   its   删掉   isp   code   pen   ide   

贪心

感觉思路很奥妙 首先我们把那些比两边小的数删掉,因为不删的话两边的数就会选这个数,这样就成了先上升后下降的序列,很明显除了第一第二大的数都能选,然后统计就好了。

技术分享
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 500010;
int n, top;
ll ans;
ll a[N], st[N];
int main()
{
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) 
        scanf("%lld", &a[i]);
    for(int i = 1; i <= n; ++i) 
    {
        while(top > 1 && st[top - 1] >= st[top] && st[top] <= a[i]) 
        {
            ans += min(a[i], st[top - 1]);
            --top;
        }
        st[++top] = a[i];    
    }
    sort(st + 1, st + top + 1);
    for(int i = 1; i < top - 1; ++i) ans += st[i];
    printf("%lld\n", ans);
    return 0;
}
View Code

 

442C

标签:closed   clu   bit   its   删掉   isp   code   pen   ide   

原文地址:http://www.cnblogs.com/19992147orz/p/7152959.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!