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),他会从"上左下右"四个方向中选定两个不同但正交的方向,然后他只 ...
随机推荐
- 一键部署基于GitLab的自托管Git项目仓库
https://market.azure.cn/Vhd/Show?vhdId=9851&version=11921 产品详情 产品介绍GitLab https://about.gitlab.c ...
- android app 压力测试工具-monkey tool
一.什么是Monkey? Monkey测试是Android自动化测试的一种手段,Monkey测试本身非常简单,就是模拟用户的按键输入,触摸屏输入,手势输入等,看设备多长时间会出异常. Monkey是A ...
- 51nod 1191 消灭兔子
题目来源: 2013腾讯马拉松赛第三场 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N只兔子,每只有一个血量B[i],需要用箭杀死免子.有M种不同类型的箭可以 ...
- java 访问docker的环境
1. 配置环境 新增 ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock root@ros ...
- Android中一个关于ListView的奇怪问题
今天在做项目的时候发现了一个比较奇怪的问题,是关于ListView的,即ListView的android:height属性会影响程序中ListView的getView()方法的调用次数,如果设置Lis ...
- 虚拟机VMware Workstation Pro装Mac遇到的一些问题【总结】
1. 问题:VM找不到Apple Mac X(M)? 解决方法:在网上找unlocker20*下载: 电脑装一个python3版本以下的版本(装python,主要是编译.因为下载的插件是python写 ...
- Android内核剖析(1)
Linux的启动过程 开机上电执行bootloader,将内核的前n条指令加载到系统内存中------>系统内核的初始化----------->启动应用程序. bootloader的位置装 ...
- GWT-2.5.1离线安装
GWT官方离线包下载地址 http://dl.google.com/eclipse/plugin/3.7/zips/gpe-e37-latest-updatesite.zip 以下是GWTDesign ...
- CPP-基础:有关调用约定
在C语言中,假设咱们有这样的一个函数:int function(int a,int b) 调历时只有用result = function(1,2)的方法就能利用这个函数.然而,当高档语言被编译成计算机 ...
- Rop实战之利用VirtualProtect绕过DEP
CVE-2011-0065 Firefox mChannel UAF漏洞 为了实现任意代码执行,需要在mChannel对象释放后,用可控数据“占坑”填充它,因此,可在onChannelRedirect ...