多打打不同的比赛,找经验啊

题目4 : 不上升序列

时间限制:40000ms
单点时限:2000ms
内存限制:256MB

描述

给定一个长度为 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

hiho的代码
#include <bits/stdc++.h>
using namespace std;
long long c,n,k;
priority_queue<int,vector<int>, greater<int>  >a;
int main(){cin>>n;for(int i=0;i<n;i++){cin>>c;a.push(c);if(a.top()<c){k+=c-a.top();a.pop();a.push(c);}}cout<<k;return 0;
}

变成相反数,不用重载写起来舒服的

#include <bits/stdc++.h>
using namespace std;
long long c,n,k;
priority_queue<int>a;
int main()
{cin>>n;for(int i=0; i<n; i++){cin>>c;c=-c,a.push(c);if(a.top()>c)k+=a.top()-c,a.pop(),a.push(c);}cout<<k;return 0;
}

大佬们都讲是原题,找了下这个题

C. Sequence
time limit per test

1 second

memory limit per test

64 megabytes

input

standard input

output

standard output

Little Petya likes to play very much. And most of all he likes to play the following game:

He is given a sequence of N integer numbers. At each step it is allowed to increase the value of any number by 1 or to decrease it by 1. The goal of the game is to make the sequence non-decreasing with the smallest number of steps. Petya is not good at math, so he asks for your help.

The sequence a is called non-decreasing if a1 ≤ a2 ≤ ... ≤ aN holds, where N is the length of the sequence.

Input

The first line of the input contains single integer N (1 ≤ N ≤ 5000) — the length of the initial sequence. The following N lines contain one integer each — elements of the sequence. These numbers do not exceed 109 by absolute value.

Output

Output one integer — minimum number of steps required to achieve the goal.

Examples
input
53 2 -1 2 11

output
4

input
52 1 1 1 1

output
1CF的这道题代码如下
#include <bits/stdc++.h>
using namespace std;
long long c,n,k;
priority_queue<int>a;
int main(){cin>>n;for(int i=0;i<n;i++){cin>>c;a.push(c);if(a.top()>c){k+=a.top()-c;a.pop();a.push(c);}}cout<<k;return 0;
}

转载于:https://www.cnblogs.com/BobHuang/p/7077984.html

hihoCoder挑战赛29相关推荐

  1. hihoCoder挑战赛A题——顺子

    hihoCoder挑战赛A题--顺子 题目链接:http://hihocoder.com/problemset/problem/1177 注意点 1.思路:输入四个数字分成下面几种情况: 1)有重复- ...

  2. hihoCoder挑战赛12 顺子(hihocoder 1177)

    题目链接:http://hihocoder.com/problemset/problem/1177 题面: #1177 : 顺子 时间限制: 6000ms 单点时限: 1000ms 内存限制: 256 ...

  3. Wannafly挑战赛29题解

    这套题目非常有意思啊23333--话说为啥没有上条先生的呢-- 传送门 \(A\) 御坂美琴 蠢了--首先先判总共加起来等不等于\(n\),不是的话就不行 然后dfs记录\(n\)不断分下去能分成哪些 ...

  4. hihoCoder挑战赛16 A—— 王胖浩与三角形

    思路:开始没有思路,想到了用三边乱搞(每条边按照比例增加)然而样例都无法通过.后来想到了海伦公式sqrt((a+b+c)(a+b-c)(b+c-a)(a+c-b))/4,那么这样以来就是让这个三角形趋 ...

  5. hihoCoder挑战赛25

    官方题解 A. Rikka with Tree 树dp \(dp(i,j)\)表示子数 \(i\)得到 \(j\)个联通块的最小操作次数. 代码 B. Rikka with Tree II 直观的做法 ...

  6. hihoCoder挑战赛14 -1223

    描述 给定n个关于X的不等式,问最多有多少个成立. 每个不等式为如下的形式之一: X < C X <= C X = C X > C X >= C 输入 第一行一个整数n. 以下 ...

  7. hihoCoder挑战赛16 王胖浩与三角形

    题目1 : 王胖浩与三角形 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 王胖浩有一个三角形,三边长为a,b,c.他有特殊的能力,能增加三条边的边长,增加的总长度不能超过 ...

  8. 牛客网Wannafly挑战赛29 B - 白井黑子 乱搞

    Description kuroko 作为常盘台唯一的空间系能力者,在每年例行的能力测试中可绝对不能让 misaka 失望哦,但是由于她的等级只是 level 4「大能力者」,在能力测试中会遇到不少困 ...

  9. hihoCoder挑战赛12 永恒游戏 模拟

    时间限制: 20000ms 单点时限: 1000ms 内存限制: 256MB 描述 很久很久以前,当Rowdark还是个善良的魔法师时,他在一个n个点m条边的无向图上玩一个游戏. 开始时他在一些点上放 ...

最新文章

  1. 通过醉品商城改版反思网站改版的那些事
  2. linux重命名的命令行操作,Linux rename命令批量重命名的方法
  3. sparksql(1)——Dataframe
  4. cnil在python_在Python中匹配组
  5. vrp 节约算法 c++_数据结构和算法(Golang实现)(8.1)基础知识-前言
  6. BitTorrent协议规范(BitTorrent Protocol Specification)系列之元信息文件结构(Metainfo File Structure)-第二部分...
  7. python安装 pip_多版本Python安装pip及pip版本管理终极教程
  8. “APP杀手”?微信小程序瞬间原形毕露
  9. iOS - Swift NSRect 位置和尺寸
  10. Pytorch基础(二) 初始自动微分
  11. 6 月 Android 系统份额:7.1 发布半年仅占比 0.6 %
  12. Jquery监听value的变化
  13. mysql 获得年月函数_mysql之日期函数
  14. amazon aws 亚马逊云服务概述
  15. 自旋锁使用场景和实现分析(转载)
  16. linux缺页异常,操作系统缺页异常
  17. 后台如何暴漏IP用于前后端测试
  18. T 分布与高斯分布的差异
  19. 转 RMAN-20005: target database name is ambiguous
  20. 【Unity】场景异步加载、卸载

热门文章

  1. 运维工程师必备之负载 均衡集群及LVS详解
  2. Dremel - Interactive Analysis of WebScale Datasets
  3. qii, sopc, nios ii 10.1 IP核设计,LED验证死活不行,感觉很神奇
  4. 北京的CCIE考试面试变成中文了
  5. SpringMVC 表单验证
  6. JWT简介json web token bear token
  7. Spring中ClassPathXmlApplicationContext类的简单使用
  8. iOS开发网络数据之AFNetworking使用
  9. Happy WS 之 控制台 -- Happy Console (一)
  10. 回首2013 展望2014