codeforces 442C C. Artem and Array(贪心)
题目链接:
2 seconds
256 megabytes
standard input
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.
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.
In a single line print a single integer — the maximum number of points Artem can get.
5
3 1 5 2 6
11
5
1 2 3 4 5
6
5
1 100 101 100 1
102 题意: 给出n个数,每次删去一个数,得到的分数是min(a,b),a和b是这个左右相邻的数,边界上的数不能删去,现在要求怎么样才能得到最大的得分; 思路: 贪心的想,每次如果一个数不小于它相邻的两个数,那么这个数就可以删去,得到的分数就是min(a,b),最后剩下的就是一个倒v形状的或者一半,最高的那两个选不了,再把剩下的相加就好了; AC代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn=5e5+10;
int n,a[maxn],q[maxn]; int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
LL ans=0;
q[1]=a[1];int p=1;
for(int i=2;i<=n;i++)
{
while(q[p]<=a[i]&&q[p]<=q[p-1]&&p>1)
{
ans=ans+min(q[p-1],a[i]);
p--;
}
q[++p]=a[i];
}
sort(q,q+p+1);
for(int i=1;i<p-1;i++)ans=ans+q[i];
cout<<ans<<endl; return 0;
}
codeforces 442C C. Artem and Array(贪心)的更多相关文章
- codeforces 442C C. Artem and Array(有深度的模拟)
题目 感谢JLGG的指导! 思路: //把数据转换成一条折线,发现有凸有凹 //有凹点,去掉并加上两边的最小值//无凹点,直接加上前(n-2)个的和(升序)//数据太大,要64位//判断凹与否,若一边 ...
- [CF442C] Artem and Array (贪心+单调栈优化)
题目链接:http://codeforces.com/problemset/problem/442/C 题目大意:一个数列,有n个元素.你可以做n-2次操作,每次操作去除一个数字,并且得到这个数字两边 ...
- CodeForces - 721D Maxim and Array (贪心)
Recently Maxim has found an array of n integers, needed by no one. He immediately come up with idea ...
- 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
C. Artem and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- [codeforces 360]A. Levko and Array Recovery
[codeforces 360]A. Levko and Array Recovery 试题描述 Levko loves array a1, a2, ... , an, consisting of i ...
- Codeforces 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- Codeforces Round #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
随机推荐
- OC静态库里NSClassFromString得到nil的解决
如果你在静态库中有从类名反射回类的代码, 如下: NSString *myClassStr = @"myClass"; Class myClazz = NSClassFromStr ...
- 一个Chrome拓展——HttpPost
周末花了点时间做了一个chrome拓展,叫HttpPost,顾名思义是用来测试http的post请求. 先直接看效果 插件与拓展 在说这个做的过程前,先说明什么是Chrome插件.Chrome拓展 1 ...
- java调用html模板发送html内容的邮件
在项目需要发送邮件,普通内容的邮件觉得太单调.太丑,没逼格,所以说直接把用到的邮件内容做成一个html模板,发送之前将对应参数替换掉,发送html内容的高逼格邮件. 首先需要引用jar包,这就不多说了 ...
- Sharepoint 2013 开启App和配置App
在任何站点中,点Add App,然后点Sharepoint Store,如果没有Enable apps,打开app store的时候出出现错误: Sorry, apps are turned off. ...
- 在 SharePoint 2013 中配置 Office Web Apps
原文发布于 2012 年 7 月 23 日(星期一) 如您所知或您即将知道,SharePoint 2013 中的 Office Web Apps 不再是 SharePoint 场中的服务应用程序.相反 ...
- Gnome排序算法
Gnome排序(地精排序),起初由Hamid Sarbazi-Azad 于2000年提出,并被称为stupid排序,后来被Dick Grune描述并命名为“地精排序”,作为一个排序算法,和插入排序类似 ...
- Eclipse中Program arguments和VM arguments的说明
在运行程序的时候,我们一般可以进行run configuration的配置,就比如tomcat源码导入eclipse之后,我们可以发现其运行配置如下: 其中Program arguments配置的元素 ...
- mysql集群之MYSQL CLUSTER
1. 参考文档 http://xuwensong.elastos.org/2014/01/13/ubuntu-%E4%B8%8Bmysql-cluster%E5%AE%89%E8%A3%85%E5%9 ...
- CentOS下如何完全卸载MySQL?卸载自带的mysql
CentOS下如何完全卸载MySQL?解决卸载不干净的问题 系统:CentOS 6.5,MySQL:MySql 5.6 这里主要解决使用rpm安装的卸载问题,安装方法见:CentOS安装mysql*. ...
- PS网页设计教程XXV——使用Photoshop设计的老式组合布局
作为编码者,美工基础是偏弱的.我们可以参考一些成熟的网页PS教程,提高自身的设计能力.套用一句话,“熟读唐诗三百首,不会作诗也会吟”. 本系列的教程来源于网上的PS教程,都是国外的,全英文的.本人尝试 ...