1350: To Add Which?

Submit Page    Summary    Time Limit: 1 Sec     Memory Limit: 128 Mb     Submitted: 303     Solved: 134


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

题目意思:
给你一个长度为n的序列
每次操作可以使得序列中某个数加1
问你最少操作多少次
可以使得序列相邻的元素的差值的绝对值都是d
做法:
做过一个非常非常类似这个题目的题
是开数组写的
没有用优先队列
因为那个时候还不会用优先队列!!!!!
GG
大数不会变大,变化的是小数。这个是解题的核心
贪心的思想
每次取出序列最大的数,扩展左右2个点。
比如样例 1 7 3 5 9
最大是9 扩展左边(无右边) 5+2变成7 刚好相差2  改变后的序列:1 7 3 7 9
现在9不用看了,弹出队列了
看最大的7(两个7,就看后面那个吧),现在第二个7左边的3扩展 3+2=5 扩展后的序列 1 7 5 7 9
现在7弹出了还有 1 7 5
看最大的7
扩展7的左边:1+4扩展成5 和7刚好相差2
再扩展7右边的5,发现不用扩展了
现在点都扩展了
在扩展过程中
记录花费
就是结果
注意的地方:
扩展过的点不用再扩展了
左端点只有扩展右边
右端点只要扩展左边
其余双向扩展
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<algorithm>
#include<memory.h>
#include<memory>
#include<queue>
using namespace std;
#define max_v 1000005
typedef long long LL;
int t,n,d;
LL sum;
int a[max_v];
struct node
{
int v,id;
friend bool operator<(node a,node b)
{
return a.v<b.v;
}
};
priority_queue<node>q;
int main()
{
scanf("%d",&t);
while(t--)
{
sum=; node p,p1; while(!q.empty()) q.pop(); scanf("%d%d",&n,&d); for(int i=;i<n;i++)
{
scanf("%d",&a[i]);
p.id=i;
p.v=a[i];
q.push(p);
} while(!q.empty())
{
p=q.top();
q.pop(); if(p.v==a[p.id])
{
if(p.id>=)
{
if(p.v-a[p.id-]>d)
{
sum+=(p.v-d-a[p.id-]);
a[p.id-]=p.v-d;
p1.v=a[p.id-];
p1.id=p.id-;
q.push(p1);
}
}
if(p.id<n-)
{
if(p.v-a[p.id+]>d)
{
sum+=(p.v-d-a[p.id+]);
a[p.id+]=p.v-d;
p1.v=a[p.id+];
p1.id=p.id+;
q.push(p1);
}
}
}
}
printf("%lld\n",sum);
}
return ;
}
/*
题目意思:
给你一个长度为n的序列
每次操作可以使得序列中某个数加1
问你最少操作多少次
可以使得序列相邻的元素的差值的绝对值都是d 做法:
做过一个非常非常类似这个题目的题
是开数组写的
没有用优先队列
因为那个时候还不会用优先队列!!!!!
GG
大数不会变大,变化的是小数。这个是解题的核心
贪心的思想
每次取出序列最大的数,扩展左右2个点。
比如样例 1 7 3 5 9
最大是9 扩展左边(无右边) 5+2变成7 刚好相差2 改变后的序列:1 7 3 7 9
现在9不用看了,弹出队列了
看最大的7(两个7,就看后面那个吧),现在第二个7左边的3扩展 3+2=5 扩展后的序列 1 7 5 7 9
现在7弹出了还有 1 7 5
看最大的7
扩展7的左边:1+4扩展成5 和7刚好相差2
再扩展7右边的5,发现不用扩展了
现在点都扩展了
在扩展过程中
记录花费
就是结果
注意的地方:
扩展过的点不用再扩展了
左端点只有扩展右边
右端点只要扩展左边
其余双向扩展 */

数组模拟:

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define max_v 100005
typedef long long LL;
LL a[max_v];
LL maxx[max_v];
LL minx[max_v];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
LL d;
scanf("%d %lld",&n,&d);
for(int i=;i<n;i++)
scanf("%lld",&a[i]); maxx[]=a[];
for(int i=;i<n;i++)
{
if(maxx[i-]-a[i]>d)
maxx[i]=maxx[i-]-d;
else
maxx[i]=a[i];
} minx[n-]=a[n-];
for(int i=n-;i>=;i--)
{
if(minx[i+]-a[i]>d)
minx[i]=minx[i+]-d;
else
minx[i]=a[i];
} LL ans=;
for(int i=;i<n;i++)
ans+=(max(maxx[i],minx[i])-a[i]);
printf("%lld\n",ans);
}
return ;
}
 

1350: To Add Which? (优先队列+贪心 或者 数组模拟)的更多相关文章

  1. 最高的奖励 - 优先队列&贪心 / 并查集

    题目地址:http://www.51cpc.com/web/problem.php?id=1587 Summarize: 优先队列&贪心: 1. 按价值最高排序,价值相同则按完成时间越晚为先: ...

  2. POJ2431 优先队列+贪心 - biaobiao88

    以下代码可对结构体数组中的元素进行排序,也差不多算是一个小小的模板了吧 #include<iostream> #include<algorithm> using namespa ...

  3. 【bzoj4278】[ONTAK2015]Tasowanie 贪心+后缀数组

    题目描述 给定两个数字串A和B,通过将A和B进行二路归并得到一个新的数字串T,请找到字典序最小的T. 输入 第一行包含一个正整数n(1<=n<=200000),表示A串的长度. 第二行包含 ...

  4. 【bzoj1692】[Usaco2007 Dec]队列变换 贪心+后缀数组

    题目描述 FJ打算带他的N(1 <= N <= 30,000)头奶牛去参加一年一度的“全美农场主大奖赛”.在这场比赛中,每个参赛者都必须让他的奶牛排成一列,然后领她们从裁判席前依次走过. ...

  5. hdu3438 Buy and Resell(优先队列+贪心)

    Buy and Resell Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  6. POJ - 3476 A Game with Colored Balls---优先队列+链表(用数组模拟)

    题目链接: https://cn.vjudge.net/problem/POJ-3476 题目大意: 一串长度为N的彩球,编号为1-N,每个球的颜色为R,G,B,给出它们的颜色,然后进行如下操作: 每 ...

  7. POJ 1330 Tarjan LCA、ST表(其实可以数组模拟)

    题意:给你一棵树,求两个点的最近公共祖先. 思路:因为只有一组询问,直接数组模拟好了. (写得比较乱) 原题请戳这里 #include <cstdio> #include <bits ...

  8. 数组模拟循环队列(java实现)

    1.front变量的含义:front就指向队列的第一个元素,也就是说arr[front]就是队列的第一个元素front的初始值=0. 2.rear变量的含义:rear指向队列的最后一个元素的后一个位置 ...

  9. 【数组模拟的链表or复杂模拟】PAT-L2-002. 链表去重

    L2-002. 链表去重 给定一个带整数键值的单链表L,本题要求你编写程序,删除那些键值的绝对值有重复的结点.即对任意键值K,只有键值或其绝对值等于K的第一个结点可以被保留.同时,所有被删除的结点必须 ...

随机推荐

  1. JS 中的布尔运算符 && 和 ||

    布尔运算符 && 和 ||的返回结果不一定是布尔值!由此来展开一定的研究及理解. 1.首先先介绍下常见的数据类型转化为bool后的值. (常用地方)在if表达式中,javascript ...

  2. Eclipse 导入 Android studio Exception Ljava/lang/UnsatisfiedLinkEror

    android studio compile fileTree(dir: 'libs', include: ['*.jar']) 没有加载so文件 main 下加入 jniLibs---so文件即可 ...

  3. LAMP创建

    httpd yum install httpd -y systemctl status httpd systemctl start httpd systemctl stop firewalld Mar ...

  4. vue使用百度地图

    1.在百度地图申请密钥:http://lbsyun.baidu.com/ 将 <script type="text/javascript" src="http:// ...

  5. Pig autocomplete 自动补全

    在pig的grunt环境下,按TAB键可以自动补全命令,用户可以添加自己的补全信息. 在conf目录下创建autocomplete文件,添加如下内容: hdfs://vm1:8020/   在grun ...

  6. nodejs lodash的一些函数

    1   _.compact用法   _.compact([0, 1, false, 2, '', 3,'mm']); var test = _.compact([-1,0, 1, false, 2, ...

  7. <![CDATA[文本内容]]>

    DTD中的属性类型 全名:character data 在标记CDATA下,所有的标记.实体引用都被忽略,而被XML处理程序一视同仁地当做字符数据看待, CDATA的形式如下: <[CDATA[ ...

  8. 用于mask遮罩效果的图片配合resizableImage使用

    用于mask遮罩效果的图片配合resizableImage使用 效果: 作为素材用的图片: 源码: // // ViewController.m // Rect // // Created by Yo ...

  9. Linux tree命令详解

    tree: 查看目录结构 tree常见命令参数 usage: tree [-adfghilnpqrstuvxACDFNS] [-H baseHREF] [-T title ] [-L level [- ...

  10. [2018HN省队集训D8T3] 水果拼盘

    [2018HN省队集训D8T3] 水果拼盘 题意 给定 \(n\) 个集合, 每个集合包含 \([1,m]\) 中的一些整数, 在这些集合中随机选取 \(k\) 个集合, 求这 \(k\) 个集合的并 ...