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. openstack奠基篇:devstack (liberty)于centos 7安装

    openstack是什么,能做什么,我就不说了,他的优势和伟大,可以想想AWS的云服务平台.学习和研究openstack(IaaS),个人的习惯是有一个可以操作的平台,然后结合代码看看详细逻辑,这个过 ...

  2. 剑指offer系列51---扑克牌顺子

    [题目]抽五张扑克牌,判断五张扑克牌是不是顺子,大小王可看做任何数,0代替. package com.exe10.offer; import java.util.Arrays; /** * [题目]抽 ...

  3. CFLAGS,CPPFLAGS,CXXFLAGS in Makefile

    CC 与 CXX:      这是 C 与 C++ 编译器命令.默认值一般是 “gcc” 与 “g++”. CPPFLAGS will be given to the C preprocessor   ...

  4. 如何添加WebService调用时的用户认证

    场景: 当把发布好的WebService地址或WSDL提供给调用方时,需要对方先进行身份的认证通过后才允许接口的进步访问.而不是公开的谁都可以调用. 解决: 1.在IIS中设置对应网站的目录访问权限. ...

  5. TMDS协议

    1 概述 1.1    连接结构 图1 TMDS连接结构 数据流中包含了像素和控制数据,发送器在任何给定的输入时钟周期,到底是编码像素数据还是控制数据取决于数据使能信号DE,DE有效时,指示像素数 据 ...

  6. [linux basic 基础]----同步信号量

    直接使用一个共享变量,来是两个线程之间进行切换是非常笨拙而且没有效率的:信号量--互斥量--这两者是相互通过对方来实现的:比如,如果想控制某一时刻只有一个线程可以访问一些共享内存,使用互斥量要自然一些 ...

  7. 通过共享用户ID来实现多个应用程序使用同一个进程(一些情况的测试)

    从很多方面来看,每个Android 应用程序都存在于它自己的世界之中:• 默认情况下,每个应用程序均运行于它自己的Linux 进程中.当应用程序中的任意代码开始执行时,Android 启动一个进程,而 ...

  8. Linux IO调度器相关算法介绍(转)

    IO调度器(IO Scheduler)是操作系统用来决定块设备上IO操作提交顺序的方法.存在的目的有两个,一是提高IO吞吐量,二是降低IO响应时间.然而IO吞吐量和IO响应时间往往是矛盾的,为了尽量平 ...

  9. [CSS]图片与文字对齐问题

    摘自:张鑫旭-鑫空间-鑫生活[http://www.zhangxinxu.com] 图片与文字默认是居底对齐了.所以当图片与文字在一起的时候往往都是不对齐的.尤其图片较小时就更加明显了,我看到很多人使 ...

  10. jsch ssh服务器调用Linux命令或脚本的小问题

    代码如下: public static boolean execshell(String command, String user, String passwd, String host) throw ...