hihoCoder挑战赛29
多打打不同的比赛,找经验啊
题目4 : 不上升序列
描述
给定一个长度为 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=;i<n;i++){
cin>>c;
a.push(c);
if(a.top()<c){
k+=c-a.top();
a.pop();
a.push(c);
}
}
cout<<k;
return ;
}变成相反数,不用重载写起来舒服的
#include <bits/stdc++.h>
using namespace std;
long long c,n,k;
priority_queue<int>a;
int main()
{
cin>>n;
for(int i=; 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 ;
}大佬们都讲是原题,找了下这个题
time limit per test1 second
memory limit per test64 megabytes
inputstandard input
outputstandard 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.
InputThe 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.
OutputOutput one integer — minimum number of steps required to achieve the goal.
Examplesinput5
3 2 -1 2 11output4
input5
2 1 1 1 1output1
CF的这道题代码如下#include <bits/stdc++.h>
using namespace std;
long long c,n,k;
priority_queue<int>a;
int main(){
cin>>n;
for(int i=;i<n;i++){
cin>>c;
a.push(c);
if(a.top()>c){
k+=a.top()-c;
a.pop();
a.push(c);
}
}
cout<<k;
return ;
}
hihoCoder挑战赛29的更多相关文章
- hihoCoder挑战赛23
hihoCoder挑战赛23 A.Emulator 题意 给一张图,有\(N(N \le 300)\)个点, 给出任意两点之间的最短路. 求最多可以去掉多少条边,使得任意两点的最短路长度不变. 思路 ...
- [题解]hihoCoder挑战赛18——题目1 神奇字符串
题目地址:http://hihocoder.com/problemset/problem/1264 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 我们说两个字符串是非常 ...
- hihocoder挑战赛26
某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...
- hihoCoder挑战赛25
萌新第一次打hihoCoder的比赛有点慌 T1 T1并不是特别难想到dp就好做了 显而易见的是一个01背包问题 Code: #include <cstdio> #include < ...
- hihoCoder挑战赛11.题目4 : 高等理论计算机科学(LCA)
clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以... 题目链接:http://hihocoder.com/problemset/problem/1167 ...
- hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)
官方题解:https://media.hihocoder.com/contests/challenge34/tutorials-previewed.pdf 题目链接:http://hihocoder. ...
- 【hihoCoder挑战赛28 A】异或排序
[题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...
- hihoCoder挑战赛1 毁灭者问题
题目链接:http://hihocoder.com/problemset/problem/1034 数据结构题,由于每个魔法单位有着不同的回复速度和上限,所以不能根据吸收时间点进行查询和更新.但是如果 ...
- hihocoder挑战赛13A
#1191 : 小W与网格 描述 给定一个n*m的网格,左上角(1, 1),右下角(n, m). 小w在(i, j),他会从"上左下右"四个方向中选定两个不同但正交的方向,然后他只 ...
随机推荐
- JAVA程序员必须要学习的知识
Java是热门的语言之一,TIOBE编程语排名Java排名第二,仅在C语言之后.Java可以用来开发web应用和桌面应用,更重要的是Java具有跨平台性:write once, run everywh ...
- HDU 5097 Page Rank (模拟)
题目背景是以前用来对网页进行排名的Page Rank算法,是早期Google的革命性发明. 背后的原理是矩阵和图论.这个数学模型是由Google的创始人拉里·佩奇和谢尔盖·布林发现的. 如果一个网页被 ...
- Android(java)学习笔记124:利用Service在后台播放背景音乐
1. 在android应用程序里,有一种没有UI的类(android.app.Service)——Service.简单来说,Service是一个 background process(背景程序),通过 ...
- python递归与非递归实现斐波那契数列
1.题目描述 大家都知道斐波那契数列,现在要求输入一个整数n,请你输出斐波那契数列的第n项(从0开始,第0项为0). 递归实现: class Solution(): def Fibnacci(self ...
- linux 使用wget下载https连接地址cannot verify github.com's certificate
使用linux的wget下载时候会出现网站没有证书警告的问题, 例如下载git时,可以使用wget https://github.com/git/git/archive/v2.3.0.zip --no ...
- 【转】VxWorks信号量分析
Wind内核中有二进制信号量.计数信号量和互斥信号量三种类型,为了是运用程序具有可移植性,还提供了POSIX(可移植操作系统接口)信号量 .在VxWorks中,信号量是实现任务同步的主要手段,也是解决 ...
- 八皇后问题(DFS)
题目描述: 要在国际象棋棋盘中放八个皇后,使任意两个皇后都不能互相吃,皇后能吃同一行.同一列,同一对角线上(两个方向的对角线)的任意棋子.现在给一个整数n(n<=92),输出前n种的摆法. 输入 ...
- Mysql中反引号和单引号的区别
反引号,一般在ESC键的下方. 它是为了区分MYSQL的保留字与普通字符而引入的符号.举个例子:SELECT `select` FROM `test` WHERE select='字段值'在test表 ...
- C#基础-数组-ArrayList
数组ArrayList using System.Collections; //表示引入集合的命名空间 数组ArrayList容量本身是不固定的,根据存储的数据动态变化 // 声明一个ArrayLis ...
- json转换为字典
str---dict ata_dict=json.loads(data)