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. Android 使用AIDL调用外部服务

    好处:多个应用程序之间建立共同的服务机制,通过AIDL在不同应用程序之间达到数据的共享和数据相互操作, 本文包括: 1 .创建AIDL 服务端.2 .创建AIDL 客户端. 3.客户端调用服务端提供的 ...

  2. 转: Ext拖拽分析

    整个Ext架构中组件是其重要的组成部分,除了少部分(如树的结点)的界面表现元素不是在这样的一个体系中,大部分的页面表现元素都被绑定在这个体系之中,下面从这个体系的最底层即在这个继承体系的最高层进行研究 ...

  3. golang自动导入postgresql脚本

    直接代码 package main import ( "fmt" "golang-objective-go/dataFoundation/dataConvert" ...

  4. Smartclient发布的几个异常问题

    1.不能下载.config文件 如果是iis6右键网站属性--->主目录--->配置 1.右键虚拟目录,在虚拟目录-配置-映射,应用程序扩展中删除.config 项目 2.IIS属性--& ...

  5. Android Please ensure that adb is correctly located at问题解决

    转载于:http://breezylee.iteye.com/blog/2032588 遇到问题描述: 运行android程序控制台输出 [2012-07-18 16:18:26 - ] The co ...

  6. angularjs中ng-route和ui-router简单用法的代码比较

    1.使用ng-route: app.js中的写法: var app=angular.module('birthdayApp',['ngRoute']); app.config(function($ro ...

  7. 黄聪:Emeditor 编辑器常用的正则表达式

    Emeditor 目前来说是我个人感觉非常不错的一款记事本软件, 其中查找替换功能由于支持正则表达式而显得非常强大. <tr[^>]*> 匹配:<tr xxxxxxxxxxxx ...

  8. Redis数据库?-Redis的Virtual Memory介绍(转)

    众所周知,Redis是一个内存数据库,和Memcached类似,所有数据存在内存中,当然,Redis有rdb和appendonlyfile两个落地文件,可以对断电停机等故障下的数据恢复做一些保证.但是 ...

  9. Jedis 连接redis超时

    redis默认不允许远程连接 用vi打开Redis服务器的配置文件redis.conf ~ sudo vi /etc/redis/redis.conf #注释bind #bind 127.0.0.1 ...

  10. Scala第四章学习笔记(面向对象编程)

    延迟构造 DelayedInit特质是为编译器提供的标记性的特质.整个构造器被包装成一个函数并传递给delayedInit方法. trait DelayedInit { def deayedInit( ...