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 ...
随机推荐
- ORACLE EXP命令
本文对Oracle数据的导入导出 imp ,exp 两个命令进行了介绍, 并对其对应的參数进行了说明,然后通过一些演示样例进行演练,加深理解.文章最后对运用这两个命令可能出现的问题(如权限不够,不同o ...
- 关于C++的疑问剖析
1)众所周知,抽象类是不存在对象的,只提供接口而不提供实现.但是抽象类能不能作为一个类指针,指向其子类的对象呢? class Interface { public: ; }; class Implem ...
- hibernate中使用HQL进行数据库查询
1.写的规则比较简单,我讲一下,如图Station这个不是数据库中的表,而是entity包中的类名Station,可以省略 select * 2.返回的类型自动转化为String类型,不用你自己再转化 ...
- python爬虫scrapy的Selectors参考文档
http://doc.scrapy.org/en/1.0/topics/selectors.html#topics-selectors-htmlcode
- [o] SQLite数据库报错: Invalid column C
向SQLite数据库内新增列,之前出现过报错为提示no such column,通过删除并重建数据库文件解决,这次报错为无效的数据列: java.lang.IllegalArgumentExcepti ...
- sql - 获取日期中的年
使用 YEAR函数, 如 --day为rain表中的字段. select YEAR(day) from t_rain
- ashx页面 “检测到有潜在危险的 Request.Form 值”的解决方法(控制单个处理程序不检测html标签)
如题: 使用web.config的configuration/location节点. 在configuration节点内新建一个location节点,注意这个节点和system.webserver那些 ...
- How to Make LastPass Even More Secure with Google Authenticator
Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...
- java 更改list 某一元素?
if(!elTd.getElementsByTag("p").isEmpty()){ int i=eduList.size(); if(i>0){ ...
- MySQL数据库迁移详细步骤(转)
========================================================================================== 一.背景简介 == ...