中南大学第一届长沙地区程序设计邀请赛 To Add Which?
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 N, D (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?的更多相关文章
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
- Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again
Minieye杯第十五届华中科技大学程序设计邀请赛现场同步赛 I Matrix Again https://ac.nowcoder.com/acm/contest/700/I 时间限制:C/C++ 1 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛D Grid(简单构造)
链接:https://ac.nowcoder.com/acm/contest/560/D来源:牛客网 题目描述 Give you a rectangular gird which is h cells ...
- H-Modify Minieye杯第十五届华中科技大学程序设计邀请赛现场赛
题面见 https://ac.nowcoder.com/acm/contest/700#question 题目大意是有n个单词,有k条替换规则(单向替换),每个单词会有一个元音度(单词里元音的个数)和 ...
- Minieye杯第十五届华中科技大学程序设计邀请赛网络赛 部分题目
链接:https://pan.baidu.com/s/12gSzPHEgSNbT5Dl2QqDNpA 提取码:fw39 复制这段内容后打开百度网盘手机App,操作更方便哦 D Grid #inc ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- 江西财经大学第一届程序设计竞赛 G题 小Q的口袋校园
链接:https://www.nowcoder.com/acm/contest/115/G来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...
- 电信学院第一届新生程序设计竞赛题解及std
首先非常感谢各位同学的参加,还有出题验题同学的辛勤付出 昨天想偷懒就是不想再把我C++11的style改没了,大家看不懂的可以百度一下哦,懒得再写gcc了,毕竟代码是通的 //代表的是行注释,所以那个 ...
- “九韶杯”河科院程序设计协会第一届程序设计竞赛 D数列重组 next_permutation
"九韶杯"河科院程序设计协会第一届程序设计竞赛 D数列重组 next_permutation 题目 原题链接: https://ac.nowcoder.com/acm/conte ...
随机推荐
- Jaxb笔记
摘自: http://www.blogjava.net/eagle-daiq/archive/2012/01/30/369016.html 最近项目原因,研究了下jaxb.jaxb是Java api ...
- SSH_框架整合6--修改Edit员工信息
SSH_框架整合6--修改Edit员工信息 1 加上修改Edit键 (1)emp-list.jsp <td> <a href="emp-input?id=${id }&qu ...
- LitDB笔记
首先,LitDB存储的不是json,而是bson.这里就有问题了,json是可以无限扩展的,但是bson不行.所有insert的时候需要使用实体类而不是hashtable.但是它又提供了一个叫做Bso ...
- 【转】java 自动装箱与拆箱
java 自动装箱与拆箱 这个是jdk1.5以后才引入的新的内容,作为秉承发表是最好的记忆,毅然决定还是用一篇博客来代替我的记忆: java语言规范中说道:在许多情况下包装与解包装是由编译器自行完成的 ...
- winform实现word转换为PDF(.doc)
注意:实现word转换为PDF文件,本人安装Office为2013; word以后缀为.doc为例实现文件类型转换,具体方式如下所示: 实现步骤: 1.添加命名空间引用——using Microsof ...
- smartgit document Rebase
The Rebase command allows you to apply commits from one branch to another. Rebase can be viewed as m ...
- VC++、MFC、COM和ATL的区别
今天看到的,感觉不错.转载了 一.什么是MFC 微软基础类(Microsoft Foundation Classes),实际上是微软提供的,用于在C++环境下编写应用程序的一个框架和引擎,VC++是W ...
- CF 274B Zero Tree 树形DP
A tree is a graph with n vertices and exactly n - 1 edges; this graph should meet the following cond ...
- listview当选中某一个item时设置背景色其他的不变
listview当选中某一个item时设置背景色其他的不变: 可以使用listview.setOnFoucsChangeListener(listener) ; /** * listview获得焦点和 ...
- db2删除数据库
1.断开所有连接 db2 stop application all force 2.停止数据库 db2stop 3.删除数据库(系统管理员权限下)db2 drop 数据库name