【HIHOCODER 1529】 不上升序列
描述
给定一个长度为 n 的非负整数序列 a[1..n]。
你每次可以花费 1 的代价给某个 a[i] 加1或者减1。
求最少需要多少代价能将这个序列变成一个不上升序列。
输入
第一行一个正整数 n。
接下来 n 行每行一个非负整数,第 i 行表示 a[i]。
1 ≤ n ≤ 500000
0 < a[i] ≤ 109
输出
一个非负整数,表示答案。
样例解释
[5,3,4,5] -> [5,4,4,4]
样例输入
4
5
3
4
5
样例输出
2
原题是CODEFORCES 713 div1 C题:
题链
#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a){
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=1000005;
char ch[N];
int main(){
priority_queue<int,vector<int>,greater<int> >que;
int n=read();
ll ans=0;
for(int i=0;i<n;i++){
int x=read();que.push(x);
if(que.top()<x){
ans+=x-que.top();
que.pop();que.push(x);
}
}
Out(ans);
return 0;
}
【HIHOCODER 1529】 不上升序列的更多相关文章
- hihocoder #1529 : 不上升序列
Description 给定一个长度为 n 的非负整数序列 a[1..n]. 你每次可以花费 1 的代价给某个 a[i] 加1或者减1. 求最少需要多少代价能将这个序列变成一个不上升序列. Solut ...
- hihocoder Challenge 29 A.序列的值
我现在就感觉我这人现在真的没有dp的意识 其实真写起来也不难,但是把就是练的少思维跟不上,dp从根本上就是一种状态的提炼和聚集. 按照题解的意思来,表示二进制第i位的值为j(0,1)的组合有多少,然后 ...
- 【HIHOCODER 1526】 序列的值(二进制DP)
时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 给定一个长度为 n 的序列 a[1..n],定义函数 f(b[1..m]) 的值为在 [0,m-1] 内满足如下条件的 i ...
- hihocoder Challenge 29 D. 不上升序列
这场爆零比赛题目还是要补的 这道题据说是出烂掉的原题,我找了下 CF13.C/ CF371 div1 C,一模一样 我服这群原题大佬 为 当时,使 不严格递增的最小步数,那么 Otherwise 显然 ...
- hihocoder 1526 序列的值
题面在这里! 好久没做题啦,养生一下qwq 推一推贡献就会发现这TM就是一个水题啊! #include<bits/stdc++.h> #define ll long long using ...
- 编程之美2015 资格赛 hihocoder 题目2: 回文字符序列
思路:暴力搜,用BFS的方式,生成每一种可能,再对每一种可能进行判断是否回文,进行统计.严重超时!计算一个25个字符的,大概要20多秒! #include <iostream> #incl ...
- 【hihoCoder】1041. 国庆出游
问题:详见http://hihocoder.com/problemset/problem/1041 有n个城市,城市编号为1-n,城市间有n-1条路(所以,城市路网是一棵树).给定一个序列S,要求判断 ...
- 【hihocoder 1039 字符串消除】模拟
题目链接:http://hihocoder.com/problemset/problem/1039 题意:给定一个只由{A, B, C}组成的字符串s,长度为n, 故包含n+1个空隙:现要求在某个空隙 ...
- 【hihocoder 1258 Osu! Master】
2015北京区域赛现场赛签到题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
随机推荐
- nginx和uwsgi的区别和作用
Django+uwsgi+nginx nginx和uwsgi的区别和作用: 1, nginx是对外的服务器,外部浏览器通过url访问nginx, uwsgi是对内的服务器,主要用来处理动态请求. 2, ...
- Java EE学习笔记(六)
初识MyBatis 1.MyBatis的定义 1).MyBatis(前身是iBatis)是一个支持普通SQL查询.存储过程以及高级映射的持久层框架. 2).MyBatis框架也被称之为ORM(Obje ...
- java如果去除中间空格,就不能用trim()这个方法了
public class Hello { public static void main(String[] args){ String text = "12v 4 6 5 55"; ...
- 530 Minimum Absolute Difference in BST 二叉搜索树的最小绝对差
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值.示例 :输入: 1 \ 3 / 2输出:1解释:最小绝对差为1,其中 2 和 1 的差的绝对值为 ...
- Solr的SolrCloud与Master-slave主从模式对比
第一印象 SolrCloud是Solr4.0引入的,主要应对与商业场景.它很像master-slave,却能自动化的完成以前需要手动完成的操作.利用ZooKeeper这个工具去监控整个Solr集群,以 ...
- JAVA吃货联盟项目。
1基础通过数组.选择结构.循环等实 现吃货联盟订餐系统. 1.我要订餐: 2.查看餐袋: 3.签收订单: 4.删除订单: 5.我要点赞: 6.退出系统: public class Doy001 { p ...
- bootstrap基本组件
bootstrap分页 <nav> <ul class="pagination"> <li><a href=&q ...
- Number of 1 BitsWrite a function that takes an unsigned integer and returns the number of ’1' bits i
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...
- Android 仿微信朋友圈添加图片
github地址(欢迎下载Demo) https://github.com/zhouxu88/WXCircleAddPic 老习惯,先上图,着急用的朋友,直接带走Demo,先拿来用吧,毕竟老板催的紧, ...
- 压力测试工具segie的使用
压力测试工具segie的使用 使用文档参考地址:https://www.joedog.org/siege-manual/ siege4地址:http://download.joedog.org/sie ...