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),他会从"上左下右"四个方向中选定两个不同但正交的方向,然后他只 ...
随机推荐
- 转 winfrom如何通过http来进行通信,并且通过传递json格式的数据可接受json格式的数据
string username = this.textBox1.Text; string password = this.textBox2.Text; string AA = HttpUtility. ...
- CSS中padding、margin两个重要属性的详细介绍及举例说明
http://www.x6x8.com/IT/199.html 本文将讲述HTML和CSS的关键—盒子模型(Box model). 理解Box model的关键便是margin和padding属性, ...
- JavaScript_10_错误
Try...catch... throw <!DOCTYPE html> <html> <head> <title></title> < ...
- SPOJ BALNUM Balanced Numbers 平衡数(数位DP,状压)
题意: 平衡树定义为“一个整数的某个数位若是奇数,则该奇数必定出现偶数次:偶数位则必须出现奇数次”,比如 222,数位为偶数2,共出现3次,是奇数次,所以合法.给一个区间[L,R],问有多少个平衡数? ...
- Java 文件操作-RandomAccessFile
1. RandomAccessFile Java提供了一个可以对文件随机访问的操作,访问包括读和写操作.该类名为RandomAccessFile.该类的读写是基于指针的操作. 1)文件访问模式 ...
- 一、git提交代码步骤
git add . //添加到本地缓存区 git commit -m '备注 ' //提交代码到本地仓库 git push //将本地代码推送到develop分支上 git checkout m ...
- js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了
js引入的数组 会被页面缓存,如需要被强制性不缓存,请用function return 就ok了
- BCB:使用CppWebBrowser判断网页加载完成
void __fastcall TForm1::CppWebBrowser1DocumentComplete(TObject *Sender, LPDISPATCH pDisp, Variant *U ...
- javascript设计模式(张容铭)学习笔记 - 外观模式绑定事件
有一个需求要为document对象绑定click事件来是想隐藏提示框的交互功能,于是小白写了如下代码: document.onclick = function(e) { e.preventDefaul ...
- Sorted Union-freecodecamp算法题目
Sorted Union 1.要求 写一个 function,传入两个或两个以上的数组,返回一个以给定的原始数组排序的不包含重复值的新数组. 换句话说,所有数组中的所有值都应该以原始顺序被包含在内,但 ...