Artem and Array
Codeforces Round #253 (Div. 1) C:http://codeforces.com/problemset/problem/442/C
题意:给你一个序列,然后你每次可以删除一个数,然后得到一个价值,这个价值是这个数左右相邻数的小的那一个。问你最多能取得多少价值。
题解:首先可以证明,如果两个大的数之间夹着一个小的数,那么这个小的数可以直接删除,这样贪心是正确的。最后得到的序列只可能是递增或者递减或者先递增后递减。对于前面两种,可以知道,从倒数第二大的数开始往小的数删除,这样贪心是正确的。对于,第三种来说,排完序后发现,也可以行倒数第二大的开始。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=*1e5+;
long long a[N],temp,ans;
int n,top;
int main(){
while(~scanf("%d",&n)){
ans=top=;
for(int i=;i<=n;i++){
scanf("%I64d",&temp);
while(top>&&a[top]<=temp&&a[top]<=a[top-]){
ans+=min(a[top-],temp);
top--;
}
a[++top]=temp;
}
sort(a+,a+top+);
for(int i=;i<=top-;i++)
ans+=a[i];
printf("%I64d\n",ans);
}
}
Artem and Array的更多相关文章
- codeforces 442C C. Artem and Array(贪心)
题目链接: C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- cf442C Artem and Array
C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Codeforces 442C Artem and Array(stack+贪婪)
题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数.删除一个数的得分为两边数的最小值,假设左右有一边不存在则算作0分. 问最大得分是多少. ...
- Codeforces 442C Artem and Array (看题解)
Artem and Array 经过分析我们能发现, 如果对于一个a[ i ] <= a[ i + 1 ] && a[ i ] <= a[ i - 1 ]可以直接删掉. 最 ...
- [CF442C] Artem and Array (贪心+单调栈优化)
题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- Artem and Array CodeForces - 442C (贪心)
大意: 给定序列$a$, 每次任选$a_i$删除, 得分$min(a_{i-1},a_{i+1})$(无前驱后继时不得分), 求最大得分. 若一个数$x$的两边都比$x$大直接将$x$删除, 最后剩余 ...
- 「CF442C」 Artem and Array
题目链接 戳我 \(Solution\) 观察发现如果一个数两边都比他大,删掉他可以保证最优,这个应该是显然的.这个东西用单调栈维护一下,最后剩下的就是个单调递减或单调递增的数列,从小到大排个序取前面 ...
- Codeforces Round 253 (Div. 2)
layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...
随机推荐
- Lucene IndexReader,IndexWriter,IndexSearcher 缓存应用
1.IndexManager类,用于提供IndexReader,IndexWriter,IndexSearcher获取接口 import java.io.File; import java.io.IO ...
- [Flux] 2. Overview and Dispatchers
Flux has four major components: Stores, Dispatchers, Views, and Actions. These components interact l ...
- [CSS] Transforms
Degrees and Turns Degrees are just one value that can be set to a rotate transform to determine how ...
- 亲测 安装windows7
1.不安装更新 2.自定义高级(重新安装windows的新副版本) 3.安装到c盘 中间电脑会重启两次. 安装完之后 安装显卡驱动→这时会出现 家庭网络组选择即可.
- Java——(六)Collection之Queue集合
------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- Queue集合 Queue集合用于模拟队列这种数据结构,队列通常是指“先进先出‘(FIFO)的容 ...
- php 两个数组是否相同,并且输出全面的数据,相同的加一个字段标示
方法一: $date是数组,数组中有字段id,name; $data1是数组,数组中有字段sort_id,name; 所以要通过$date[$i]['id']==$data1[$j]['sort_id ...
- 第一篇:python高级之函数
python高级之函数 python高级之函数 本节内容 函数的介绍 函数的创建 函数参数及返回值 LEGB作用域 特殊函数 函数式编程 1.函数的介绍 为什么要有函数?因为在平时写代码时,如果没 ...
- Html5新增的语义化标签(部分)
2014年10月29日,万维网联盟宣布,经过接近8年的艰苦努力,html5的标准规范终于制定完成.这是互联网的一次重大变革,这也许是一个时代的来临! 总结一些h5新增的语义化标签,记录下来方便自己学习 ...
- PHP 开启报错机制
屏蔽PHP错误提示 方法一:在有可能出错的函数前加@,然后or die("") 如: @mysql_connect(...) or die("Database Conne ...
- 关于XML(一)。
关于XML 什么是XML? XML是可扩展标记语言.类似于HTML,XML的宗旨是旨在传输数据,而非显示数据.其标签没有预定义,您需要自行定义标签.XML具有自我描述性,是W3C的推荐标准. XML与 ...