Codeforces Round #253 DIV1 C 馋
http://codeforces.com/contest/442/problem/C
题意非常easy,基本上肯定有坑坑洼洼的样子。看题目案例,从第三个跟第二个没有凹的案例来看的话,多写几个以及多画画过程略微推一下就会发现。除了最大的两个数以外都能够得到,然后就是凹的情况了,凹的情况肯定是唯一的,把中间的数除去得到一个值。可是凹凸有结合该怎么办。猜一把先把凹的单独一个个给攻克了,产生没有凹的序列再处理。然后刚好对于第一个案例进行測试,发现答案正确,于是就这么贪心的敲了一个
对于凹的情况 能够使用栈来处理,处理完对于没有凹的情况直接排序 除了最大的两个数以外其他 能够都取了
#include<iostream>
#include<cstdio>
#include<list>
#include<algorithm>
#include<cstring>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<cmath>
#include<memory.h>
#include<set> #define ll long long #define eps 1e-8 const int inf = 0xfffffff; const ll INF = 1ll<<61; using namespace std; //vector<pair<int,int> > G;
//typedef pair<int,int > P;
//vector<pair<int,int> > ::iterator iter;
//
//map<ll,int >mp;
//map<ll,int >::iterator p;
int n;
int num[1000000 + 5]; stack<int > s; ll ans; void init() {
memset(num,0,sizeof(num));
while(!s.empty())s.pop();
ans = 0ll;
} bool input() {
while(scanf("%d",&n) == 1) {
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
return false;
}
return true;
} void cal() {
bool flag = false;
s.push(num[0]);
for(int i=1;i<n;i++) {
if(num[i] <= s.top()) {s.push(num[i]);flag = true;continue;}
if(num[i] >= s.top() && flag) {
s.pop();
ans += min(num[i],s.top());
if(num[i] <= s.top())flag = true;
else {
while(true) {
int tmp = s.top();
s.pop();
if(s.empty()) {
s.push(tmp);
break;
}
if(tmp > s.top() || num[i] < tmp) {
s.push(tmp);
break;
}
if(num[i] >= tmp)ans += min(s.top(),num[i]);
}
}
if(num[i] <= s.top())flag = true;
else flag = false;
s.push(num[i]);
continue;
}
s.push(num[i]);
}
memset(num,0,sizeof(num));
int cnt = 0;
while(!s.empty()) {
num[cnt++] = s.top();
s.pop();
}
sort(num,num+cnt);
for(int i=0;i<cnt-2;i++)
ans += num[i];
} void output() {
printf("%I64d\n",ans);
} int main() {
while(true) {
init();
if(input())return 0;
cal();
output();
}
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
Codeforces Round #253 DIV1 C 馋的更多相关文章
- Codeforces Round #253 (Div. 1) (A, B, C)
Codeforces Round #253 (Div. 1) 题目链接 A:给定一些牌,然后如今要提示一些牌的信息,要求提示最少,使得全部牌能够被分辨出来. 思路:一共2^10种情况,直接暴力枚举,然 ...
- Codeforces Round #543 Div1题解(并不全)
Codeforces Round #543 Div1题解 Codeforces A. Diana and Liana 给定一个长度为\(m\)的序列,你可以从中删去不超过\(m-n*k\)个元素,剩下 ...
- Codeforces Round #545 Div1 题解
Codeforces Round #545 Div1 题解 来写题解啦QwQ 本来想上红的,结果没做出D.... A. Skyscrapers CF1137A 题意 给定一个\(n*m\)的网格,每个 ...
- Codeforces Round #539 Div1 题解
Codeforces Round #539 Div1 题解 听说这场很适合上分QwQ 然而太晚了QaQ A. Sasha and a Bit of Relax 翻译 有一个长度为\(n\)的数组,问有 ...
- Codeforces Round 253 (Div. 2)
layout: post title: Codeforces Round 253 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- [Codeforces Round #254 div1] C.DZY Loves Colors 【线段树】
题目链接:CF Round #254 div1 C 题目分析 这道题目是要实现区间赋值的操作,同时还要根据区间中原先的值修改区间上的属性权值. 如果直接使用普通的线段树区间赋值的方法,当一个节点表示的 ...
- Codeforces Round #253 (Div. 2) D. Andrey and Problem
关于证明可以参考题解http://codeforces.com/blog/entry/12739 就是将概率从大到小排序然后,然后从大到小计算概率 #include <iostream> ...
- Codeforces Round #253 (Div. 2) D题
题目大意是选出一个其他不选,问问最大概率: 刚开始想到DP:F[I][J][0]:表示从 前I个中选出J个的最大值, 然后对于F[I][J][1]=MAX(F[I-1][J][1],F[I-1][J- ...
- Codeforces Round #253 (Div. 1) A. Borya and Hanabi 暴力
A. Borya and Hanabi Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/442/p ...
随机推荐
- 反射API
反射,是指一种能在运行时动态加载.分析类的能力.反射被广泛地用于那些需要在运行时检测或修改程序行为的程序中.这是一个相对高级的特性,使用反射技术应当具备相当的Java语言基础.我们可以通过反射机制让应 ...
- Android中获取电池电量
/** * * @author chrp * *显示当前电池电量 */ public class MainActivity extends Activity { private TextView tv ...
- 积累的VC编程小技巧之列表框
1.列表框中标题栏(Column)的添加 创建一个List Control,其ID为IDC_LIST,在其Styles属性项下的View项里选择Report.Align项里选择Top.Sort项里选择 ...
- Jquery发送ajax请求以及datatype参数为text/JSON方式
Jquery发送ajax请求以及datatype参数为text/JSON方式 1.方式一:datatype:'text' 2.方式二:datatype:'JSON' 3.使用gson-1.5.jar包 ...
- 陈年查尔斯们,请考虑下记者们的感受 by 李斌
http://mp.weixin.qq.com/mp/appmsg/show?__biz=MjM5ODUxNTQwMA==&appmsgid=10000507&itemidx=2&am ...
- ThinkPhp学习08
原文:ThinkPhp学习08 一.普通查询方式 a.字符串 $arr=$m->where("sex=0 and username='gege'")->find(); ...
- 更好的自动ssh登录
更好的自动ssh登录 解决~/.ssh/known_hosts 过期问题. bash + expect bash:ssh.sh #!/bin/bash help(){ echo "usage ...
- Java回调理解 (step by step)
在网上搜索了很多篇关于java回调函数的文章,自己也来试了一下写了这篇博客,可能有些地方理解不到位,烦请各位大大指正. 在计算机程序设计中.回调函数.或简称回调.是指通过函数參数传递到其他代码的,某一 ...
- [Android]ADT Run时候报错:The connection to adb is down, and a severe error has occured
The connection to adb is down, and a severe error has occured. 之 ..\sdk\platform-tools\adb.exe and c ...
- iframe动态创建及释放内存
近期參与一个项目的开发,因为项目是基于浏览器的胖client(RIA)应用程序,页面中大量调用iframe.后期測试发现浏览器内存一直居高不下,并且打开iframe页面越多内存占用越大.在IE系列浏览 ...