CSU1350 To Add which?

题目链接:

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=0;
19         cin>>n>>D;
20         for(int i=0;i<n;i++) cin>>num[i];
21
22         Max[0]=num[0];
23         for(int i=1;i<n;i++)
24         {
25             if(Max[i-1]-num[i]>D)
26                 Max[i]=Max[i-1]-D;
27             else Max[i]=num[i];
28         }
29
30         Min[n-1]=num[n-1];
31         for(int i=n-2;i>=0;i--)
32         {
33             if(Min[i+1]-num[i]>D)
34                 Min[i]=Min[i+1]-D;
35             else Min[i]=num[i];
36         }
37
38         for(int i=0;i<n;i++) ans+=(max(Min[i],Max[i])-num[i]);
39         cout<<ans<<endl;
40     }
41     return 0;
42 }
时间: 2024-11-08 16:53:16

CSU1350 To Add which?的相关文章

设置IIS,使其只能接收国内的用户访问(IP限制)

IP明细参考 先找到国内所有的IP http://ipblock.chacuo.net/view/c_CN 执行脚本 IIS白名单设置 powershell #国内IP白名单 Import-Module WebAdministration $webSite = 'TEST.WEBSITE' function Func { param ( $ipAddr, $ipMask ) # Add new IP CIDR entry to restrictions to website Test Add-W

backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.

昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec>   <ctx>yMaint.ShrinkLog</ctx>   <inf>Log Shrink</inf>   <Sql> --  ======================================================================== -- Shrink of log file E:\SQ

2、Add Two Numbers

1.Add Two Numbers--这是leedcode的第二题: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input:

FragmentTransaction的add(),replace(),以及show(),hide()

最近在做一个Android的电商App,之前一直使用FragmentTransaction的add(),hide()和show()来控制主页的显示与隐藏.最近发现一个问题,因为show()和hide() 来控制显示隐藏的话是不走Fragment的onResume方法的,而如果使用replace()的话就是全部Fragment都走onResume()方法.这就无法满足我一部分Fragment点击刷新而另一部分不刷新的要求.最后发现,通过定义两个FragmentManager和FragmentTra

[LeetCode In C++] 2. Add Two Numbers

题目: You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 -&

There is no Action mapped for namespace [/user] and action name [user!add] associated with context p

使用struts2.3进行动态方法调用时出现: There is no Action mapped for namespace [/user] and action name [user!add] associated with context path错误,原因是 (1)DMI可能导致安全问题 (2)DMI与通配符方法功能有重叠,因此该版本Struts2默认关闭DMI,需要在struts.xml中加一句 <constant name="struts.enable.DynamicMetho

Add Again(重复元素排序)

Add Again Input: Standard Input Output: Standard Output Summation of sequence of integers is always a common problem in Computer Science. Rather than computing blindly, some intelligent techniques make the task simpler. Here you have to find the summ

ArrayList之add(E e)学习笔记

/**  * Appends the specified element to the end of this list.  *  * @param e element to be appended to this list  * @return <tt>true</tt> (as specified by {@link Collection#add})  */ public boolean add(E e) {     ensureCapacityInternal(size + 

LeetCode --- 2. Add Two Numbers

题目链接:Add Two Numbers You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3