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. C#.NET ,微信退款证书

    微信退款时遇到:基础连接已经关闭 连接被意外关闭. 服务器环境:WIN SERVER 2008 R2.  WINDOWS服务承载的WCF服务,基于.NET FRAMEWORK 3.5. 第一笔交易的退 ...

  2. c# .net sha256 16进制 64位 签名

    public static string GetSHA256hash(string input, string _input_charset) { byte[] clearBytes = Encodi ...

  3. visual studio 编译时 出现 Files 的值 乱码

    参考了:http://blog.163.com/jiang_tao_2010/blog/static/121126890201031031337332/ 最近在做程序时,在生成解决方法过程中,电脑出现 ...

  4. Oracle中in和exists的选择

    在ORACLE 11G大行其道的今天,还有很多人受早期版本的影响,记住一些既定的规则,   1.子查询结果集小,用IN   2.外表小,子查询表大,用EXISTS 摘自:http://blog.chi ...

  5. [linux basic 基础]----线程的属性

    在信号量和互斥量例子中,我们都是在程序推出之前利用pthread_join对线程进行再次同步:如果想让thread想创建它的线程返回数据我需要这么做:问题:我们有时候既不需要第二个线程向main线程返 ...

  6. PHP中的中文截取乱码问题_gb2312_utf-8

    一.字符串编码为gb2312,一个中文占俩字节 public static function chinesesubstr($str, $start, $len) { // $str指字符串,$star ...

  7. 黄聪:wordpress中remove_action、add_action、 do_action()的hook钩子都有哪些

    原文地址:http://codex.wordpress.org/Plugin_API/Action_Reference muplugins_loaded After must-use plugins ...

  8. eclipse调试总结(转)

    Debug视图 认识debug视图,红色部分框为线程堆栈视图,黄色部分框为表达式.断点.变量视图,蓝色部分为代码视图.  线程堆栈视图 分别介绍一下这几个按钮的含义: 1.表示当前实现继续运行直到下一 ...

  9. /proc/sys/net/ipv4/

    /proc/sys/net/ipv4/icmp_timeexceed_rate这个在traceroute时导致著名的"Solaris middle star".这个文件控制发送IC ...

  10. do break的妙用

    #include <stdio.h> #include <malloc.h> int func(int n) { //资源的统一申请 ; ; int* p = (int*)ma ...