题目链接:

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

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

代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #define M 100100
  4. using namespace std;
  5.  
  6. int num[M],Max[M],Min[M];
  7.  
  8. int max(int a,int b)
  9. {
  10. return a>b?a:b;
  11. }
  12. int main()
  13. {
  14. int T,n,D;
  15. long long ans;
  16. cin>>T;
  17. while(T--){
  18. ans=;
  19. cin>>n>>D;
  20. for(int i=;i<n;i++) cin>>num[i];
  21.  
  22. Max[]=num[];
  23. for(int i=;i<n;i++)
  24. {
  25. if(Max[i-]-num[i]>D)
  26. Max[i]=Max[i-]-D;
  27. else Max[i]=num[i];
  28. }
  29.  
  30. Min[n-]=num[n-];
  31. for(int i=n-;i>=;i--)
  32. {
  33. if(Min[i+]-num[i]>D)
  34. Min[i]=Min[i+]-D;
  35. else Min[i]=num[i];
  36. }
  37.  
  38. for(int i=;i<n;i++) ans+=(max(Min[i],Max[i])-num[i]);
  39. cout<<ans<<endl;
  40. }
  41. return ;
  42. }

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. sql注入原理及解决方案

    sql注入原理 sql注入原理就是用户输入动态的构造了意外sql语句,造成了意外结果,是攻击者有机可乘 SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的 ...

  2. logging模块基础

    很多程序都有记录日志的需求,日志不仅可以保存访问记录,也可以有错误,警告等信息输出. python的logging模块提供了标准的日志接口,可以通过logging存储各种格式的日志.logging模块 ...

  3. Android CursorAdapter的使用

    CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...

  4. Keil简介

    最早接触Keil是学习开发8051系列的单片机.Keil C51是Keil公司出品的51系列兼容单片机C语言软件开发系统,与汇编相比,C语言在功能上.结构性.可读性.可维护性上有明显的优势,因而易学易 ...

  5. SQLServer · 最佳实践 · SQL Server 2012 使用OFFSET分页遇到的问题

    1. 背景 最近有一个客户遇到一个奇怪的问题,以前使用ROW_NUMBER来分页结果是正确的,但是替换为SQL SERVER 2012的OFFSET...FETCH NEXT来分页出现了问题,因此,这 ...

  6. Web服务器安全设置

    Web服务器安全方面一直重视程度不够,是各种网站经常被黑的主要原因.下面笔者总结了一下关于怎样保证Web服务器安全的措施,希望能给那些服务器尚存在漏洞的用户提供一些帮助. 本文主要以Windows s ...

  7. COGS 2685. 迷妹

    ★   输入文件:fans.in   输出文件:fans.out   简单对比时间限制:1 s   内存限制:256 MB [题目描述] 小钟.小皓和小曦都是著名偶像派OI选手,他们都有很多迷妹. 现 ...

  8. java生成饼图svg

    package com.tellhow.svg; import java.io.File;import java.io.FileOutputStream; /** *  * @author 风絮NO. ...

  9. zipkin 服务追踪

    服务追踪,就是对请求接口的追踪并保存. 在测试的过程中我们会发现,有时候,程序刚刚启动后,刷新几次,并不能看到任何数据,原因就是我们的spring-cloud-sleuth收集信息是有一定的比率的,默 ...

  10. uva820 Internet Bandwidth

    就是模板... #include<cstdio> #include<cstring> #include<vector> #include<queue> ...