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

题目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=;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 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
5
3 2 -1 2 11
output
4
input
5
2 1 1 1 1
output
1
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的更多相关文章

  1. hihoCoder挑战赛23

    hihoCoder挑战赛23 A.Emulator 题意 给一张图,有\(N(N \le 300)\)个点, 给出任意两点之间的最短路. 求最多可以去掉多少条边,使得任意两点的最短路长度不变. 思路 ...

  2. [题解]hihoCoder挑战赛18——题目1 神奇字符串

    题目地址:http://hihocoder.com/problemset/problem/1264 时间限制:20000ms 单点时限:1000ms 内存限制:256MB 描述 我们说两个字符串是非常 ...

  3. hihocoder挑战赛26

    某蒟蒻成功的·写出了T1并rank16...小岛的题目真难... 传送门:http://hihocoder.com/contest/challenge26 T1 如果你想要暴力枚举的话显然是不行的 如 ...

  4. hihoCoder挑战赛25

    萌新第一次打hihoCoder的比赛有点慌 T1 T1并不是特别难想到dp就好做了 显而易见的是一个01背包问题 Code: #include <cstdio> #include < ...

  5. hihoCoder挑战赛11.题目4 : 高等理论计算机科学(LCA)

    clj在某场hihoCoder比赛中的一道题,表示clj的数学题实在6,这道图论貌似还算可以... 题目链接:http://hihocoder.com/problemset/problem/1167 ...

  6. hihoCoder挑战赛34 B题(快速求第k轮冒泡排序的结果)

    官方题解:https://media.hihocoder.com/contests/challenge34/tutorials-previewed.pdf 题目链接:http://hihocoder. ...

  7. 【hihoCoder挑战赛28 A】异或排序

    [题目链接]:http://hihocoder.com/problemset/problem/1509 [题意] [题解] 每次找到相邻两个数的二进制形式中; 不同的最高位; 显然S在这一位必然是确定 ...

  8. hihoCoder挑战赛1 毁灭者问题

    题目链接:http://hihocoder.com/problemset/problem/1034 数据结构题,由于每个魔法单位有着不同的回复速度和上限,所以不能根据吸收时间点进行查询和更新.但是如果 ...

  9. hihocoder挑战赛13A

    #1191 : 小W与网格 描述 给定一个n*m的网格,左上角(1, 1),右下角(n, m). 小w在(i, j),他会从"上左下右"四个方向中选定两个不同但正交的方向,然后他只 ...

随机推荐

  1. django之母版的继承

    模板继承示例 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  2. js当前日期

    function CurentTime()    {         var now = new Date();               var year = now.getFullYear(); ...

  3. python3发送邮件01(简单例子,不带附件)

    # -*- coding:utf-8 -*-import smtplibfrom email.header import Headerfrom email.mime.text import MIMET ...

  4. Web端 年月日下拉表 密码判断 按钮判断是否提交

    生日: <asp:DropDownList ID="selYear" runat="server"></asp:DropDownList> ...

  5. codevs 4093 EZ的间谍网络

    时间限制: 10 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题目描述 Description 由于外国间谍的大量渗入,学校安全正处于高度的危机之中.YJY决定挺身而作出反抗 ...

  6. connect() to 192.168.30.71:8082 failed (99: Cannot assign requested address) while connecting to upstream, client: 114.80.182.136, server: localhost, request: "GET /home/senior HTTP/1.1", upstream: "

    connect() to 192.168.30.71:8082 failed (99: Cannot assign requested address) while connecting to ups ...

  7. 给广大码农分享福利:一个业界良心的github仓库,中文计算机资料

    我今天查资料时无意发现的,https://github.com/CyC2018/CS-Notes 这个仓库包含了下列几个维度的计算机学习资料: 深受国内程序员喜爱,已经有超过3万多star了. 1. ...

  8. Spring Mybatis PageHelper 设置使用

    PageHelper是一个Mybatis的分页插件, 负责将已经写好的sql语句, 进行分页加工. 设置 现在使用的是PageHelper 5.0 版本 : 在build.gradle先引用jar包: ...

  9. appium---AndroidSdk安装

    AndroidSDK指的是Android专属的软件开发工具包,被软件开发工程师用于为特定的软件包.软件框架.硬件平台.操作系统等建立应用软件的开发工具的集合.Android又是采用java语言进行开发 ...

  10. ARC中__weak;__strong;__unsafe_unretained;修饰词

    测试代码: // Human.h代码 @interface Human : NSObject @property (nonatomic, weak) Cat *pinkCat; @property ( ...