题目链接:

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1350

这题目因为每一个数都跟相邻的数有关,所以可以从左到右和从右到左一次扫一遍即可

代码:

 #include<iostream>
#include<cstdio>
#define M 100100
using namespace std; int num[M],Max[M],Min[M]; int max(int a,int b)
{
return a>b?a:b;
}
int main()
{
int T,n,D;
long long ans;
cin>>T;
while(T--){
ans=;
cin>>n>>D;
for(int i=;i<n;i++) cin>>num[i]; Max[]=num[];
for(int i=;i<n;i++)
{
if(Max[i-]-num[i]>D)
Max[i]=Max[i-]-D;
else Max[i]=num[i];
} Min[n-]=num[n-];
for(int i=n-;i>=;i--)
{
if(Min[i+]-num[i]>D)
Min[i]=Min[i+]-D;
else Min[i]=num[i];
} for(int i=;i<n;i++) ans+=(max(Min[i],Max[i])-num[i]);
cout<<ans<<endl;
}
return ;
}

CSU1350 To Add which?的更多相关文章

  1. AutoMapper:Unmapped members were found. Review the types and members below. Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type

    异常处理汇总-后端系列 http://www.cnblogs.com/dunitian/p/4523006.html 应用场景:ViewModel==>Mode映射的时候出错 AutoMappe ...

  2. EntityFramework Core 1.1 Add、Attach、Update、Remove方法如何高效使用详解

    前言 我比较喜欢安静,大概和我喜欢研究和琢磨技术原因相关吧,刚好到了元旦节,这几天可以好好学习下EF Core,同时在项目当中用到EF Core,借此机会给予比较深入的理解,这里我们只讲解和EF 6. ...

  3. ASP.NET Core: You must add a reference to assembly mscorlib, version=4.0.0.0

    ASP.NET Core 引用外部程序包的时候,有时会出现下面的错误: The type 'Object' is defined in an assembly that is not referenc ...

  4. [转]NopCommerce How to add a menu item into the administration area from a plugin

    本文转自:http://docs.nopcommerce.com/display/nc/How+to+code+my+own+shipping+rate+computation+method Go t ...

  5. [deviceone开发]-动态添加组件add方法的示例

    一.简介 这个示例详细介绍ALayout的add方法的使用(原理也适用于Linearlayout),以及add上去的新ui和已有的ui如何数据交换,初学者推荐.二.效果图 三.相关下载 https:/ ...

  6. [LeetCode] Add Two Numbers II 两个数字相加之二

    You are given two linked lists representing two non-negative numbers. The most significant digit com ...

  7. [LeetCode] Add Strings 字符串相加

    Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2. ...

  8. [LeetCode] Expression Add Operators 表达式增加操作符

    Given a string that contains only digits 0-9 and a target value, return all possibilities to add ope ...

  9. [LeetCode] Add Digits 加数字

    Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...

随机推荐

  1. qconbeijing2014

    http://2014.qconbeijing.com/videoslides.html   周一 周二 周三 周四 周五 周六 2014年5月19日 Deep Dive into Amazon's ...

  2. Redis学习笔记(二)字符串进阶

    1.增减操作 字符串可以储存字节串.整数.浮点数三种类型的值,如果值无法被解释为十进制的整数或浮点数,那么对此值进行增减操作会返回错误,如果对一个不存在的或者保存了空串的键进行增减操作,Redis将当 ...

  3. vue 自定义组件 v-model双向绑定、 父子组件同步通信【转】

    父子组件通信,都是单项的,很多时候需要双向通信.方法如下: 1.父组件使用:msg.sync="aa"  子组件使用$emit('update:msg', 'msg改变后的值xxx ...

  4. docker之启动创建容器流程

    libcontainer的工作流程 execdriver的run方法通过docker daemon提交一份command信息创建了一份可供libcontainer解读的容器配置container,继而 ...

  5. magic_quotes_runtime 和 magic_quotes_sybase 的作用

    如果启用了 magic_quotes_runtime,大多数返回任何形式外部数据的函数,包括数据库和文本段将会用反斜线转义引号. 如果启用了magic_quotes_sybase,单引号会被单引号转义 ...

  6. Java代理模式精讲之静态代理,动态代理,CGLib代理

    代理(Proxy)是一种设计模式,通俗的讲就是通过别人达到自己不可告人的目的(玩笑). 如图: 代理模式的关键点是:代理对象与目标对象.代理对象是对目标对象的扩展,并会调用目标对象 这三个代理模式,就 ...

  7. 最短路 || POJ 1797 Heavy Transportation

    Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...

  8. 组管理命令--groupadd.groupmod.groupdel.gpasswd

    添加用户组 格式 groupadd [参数] 组名 参数选项 -g GID:指定新组的GID,默认值是已有的最大的GID加1.-r:建立一个系统专用组,与-g不同时使用时,则分配一个1-499的GID ...

  9. linux 如何查看硬盘大小,内存大小等系统信息及硬件信息

    一.linux CPU大小[root@idc ~]# cat /proc/cpuinfo |grep "model name" && cat /proc/cpuin ...

  10. IDM下载器使用方法详解:百度网盘下载,视频会员一网打尽!

    一. IDM的设置 [01]IDM插件与各大浏览器的集成 默认情况下,在成功安装IDM后,直接点击这里的选项,会弹出[常规设置],一般情况下直接保持默认的配置即可,如果你使用的是比较小众的浏览器,你可 ...