1350: To Add Which?

Time Limit: 1 Sec  Memory Limit: 128 MB

Description

There is an integer sequence with N integers. You can use 1 unit of cost to increase any integer in the sequence by 1.
    Could you tell us the least units of cost to achieve that, the absolute value of difference between any two adjacent integers is not more than D?

Input

The first line has one integer T, means there are T test cases.
    For each test case, the first line has two integers ND (1 <= N <= 105, 0 <= D < 109), which have the same meaning as above. The next line has N integers describing the sequence. Every integer in this sequence is in range [0, 109).
    The size of the input file will not exceed 5MB.

Output

For each test case, print an integer in one line, indicates the desired answer.

Sample Input

3
5 2
1 3 5 3 5
5 1
1 2 3 5 6
5 2
1 7 3 5 9

Sample Output

0
3
8

HINT

 

Source

中南大学第一届长沙地区程序设计邀请赛

贪心的思想,大数不会变大,变化的是小数。

每次取出最大的数,扩展左右2个点。

#include <iostream>
#include <string>
#include <string.h>
#include <map>
#include <stdio.h>
#include <algorithm>
#include <queue>
#include <vector>
#include <math.h>
#include <set>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std ;
typedef long long LL ;
const int Max_N = ;
int N ,D ;
int num[Max_N] ;
LL sum ; struct Node{
int id ;
int num ;
Node(){} ;
Node(int i ,int n):id(i),num(n){} ;
friend bool operator < (const Node A ,const Node B){
return A.num < B.num ;
}
}; int gan(int id , int x){
if(id> N || id < )
return ;
if(fabs(num[id] - x) <= D)
return ;
sum += (LL)(x - D - num[id]) ;
num[id] = x - D ;
return x - D ;
} LL gao(){
sum = ;
scanf("%d%d",&N,&D) ;
int n_num ;
priority_queue<Node> que ;
for(int i = ; i <= N ; i++){
scanf("%d",&num[i]) ;
que.push(Node(i,num[i])) ;
}
while(!que.empty()){
Node now = que.top() ;
que.pop() ;
if(now.num != num[now.id])
continue ;
n_num = gan(now.id - ,now.num) ;
if(n_num)
que.push(Node(now.id - ,n_num)) ;
n_num = gan(now.id + ,now.num) ;
if(n_num)
que.push(Node(now.id + ,n_num)) ;
}
return sum ;
} int main(){
int T ;
scanf("%d",&T) ;
while(T--){
cout<<gao()<<endl ;
}
return ;
}

中南大学第一届长沙地区程序设计邀请赛 To Add Which?的更多相关文章

  1. 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm

    1352: New Sorting Algorithm Time Limit: 1 Sec  Memory Limit: 128 MB Description We are trying to use ...

  2. Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again

    Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...

  3. Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)

    链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...

  4. H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛

    题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...

  5. Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 部分题目

    链接:https://pan.baidu.com/s/12gSzPHEgSNbT5Dl2QqDNpA 提取码:fw39 复制这段内容后打开百度网盘手机App,操作更方便哦 D    Grid #inc ...

  6. 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛

    比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...

  7. 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园

    链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  8. 电信学院第一届新生程序设计竞赛题解及std

    首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...

  9. “九韶杯”河科院程序设计协会第一届程序设计竞赛 D数列重组 next_permutation

    "九韶杯"河科院程序设计协会第一届程序设计竞赛 D数列重组  next_permutation 题目 原题链接: https://ac.nowcoder.com/acm/conte ...

随机推荐

  1. 【oracle】 linux 下oracle 启动监听错误

    Message 1070 not found; No message file for product=network, facility=TNSTNS-12545: Message 12545 no ...

  2. IOS开发-视频,音频,录音简单总结

    /***** * 1. 视频播放 * *  @格式:mp4 mov m4v m2v 3gp 3g2 * *  @系统框架使用:#import <MediaPlayer/MediaPlayer.h ...

  3. oracle中一个字符串包含另一个字符串中的所有字符

    oracle中一个字符串包含另一个字符串中的所有字符 --解决监理报告中所勾选的标段信息,与该用户所管理的标段字符串不匹配的问题. select * from a where instr(a,b)&g ...

  4. mysql 学习笔记(一)

    查询:show databases; show status; show tables; desc  table-name: 更改root密码:方法一:mysqladmin -uroot -poldp ...

  5. 调试压缩后的Javascript文件:在 Chrome 和 Safari ,选择“脚本”标签,找到相应的文件,然后点击“{}”图标(pretty print,在面板底部)

  6. Blockchain概述--转

    编者按:著名投资人 Fred Wilson 的同事 Joel Monegro 近日参加了纽约比特币 workshop HackBit聚会,其间他们讨论了比特币式的思维方式对未来十年世界的影响,而这种影 ...

  7. PLSQL_基础系列08_操作符标LPAD / TRUNC / DECODE / TRIM / INSTR(案例)

    2014-12-09 Created By BaoXinjian

  8. NodeJS 学习资料

    1. npm 除Node本身提供的API外,现在有不少第三方模块可极大的提高开发效率,npm就是Node的软件包管理器,可以用它安装所需软件包并发布自己为nodejs写的软件包. npm安装node扩 ...

  9. GC之一--GC 的算法分析、垃圾收集器、内存分配策略介绍

    一.概述 垃圾收集 Garbage Collection 通常被称为“GC”,它诞生于1960年 MIT 的 Lisp 语言,经过半个多世纪,目前已经十分成熟了. jvm 中,程序计数器.虚拟机栈.本 ...

  10. Linux磁盘系统基础知识(转载)

    From:http://www.liusuping.com/ubuntu-linux/linux-disk-basic.html 在Linux系统下对于IDE硬盘,每块盘有一个设备名:对应于主板的四个 ...