题目

2019.9.7,icpc徐州网络赛的E题 XKC's basketball team ,计蒜客上还可以做。

链接:https://nanti.jisuanke.com/t/41387

Input

The first line contains two integers n and m ( 2 ≤ n ≤ 5∗10^5 ,0 ≤ m ≤ 10^9) .

The following  line contain n integers W1​..Wn​(0 ≤ Wi​ ≤ 10^9) .

Output

A row of n integers separated by spaces , representing the anger of every member .

样例输入

6 13 4 5 6 2 10

样例输出

4 3 2 1 0 -1

题意:

对于数组中的每个元素来说,如果它加上m之后,右边仍然有比它大的数(有多个的话,取距离最远的),这个距离就是我们要的结果,不存在取 -1。所以问题就是,取得数组中每个元素右边最后一个比它大的元素。

思路:

看了 “O(n) 取得数组中每个元素右边第一个比它大的元素” 的思路想到的。

  • 建立一个Node (value, index);value是原数组的值 + m,index是对应的索引。(具体的value是什么,视问题而定)
  • 建立壹 Node 的 value 值升序的优先级队列。也就是小根堆。
  • i = n - 1,开始向前遍历

不断用当前堆顶的value值与arr[i]进行比较,如果堆顶元素小于arr[i],则找到需要找的元素。因为堆顶是最小的元素,如果堆顶大于arr[i],则i--,继续重复当前步骤。

  • 直到遍历完成,堆中大概率还会剩下几个(加+m之后,原数组中没有元素比它们大了),这些元素对应的 res 都是 -1 。
 #include <iostream>
#include <queue>
#include <stdio.h>
using namespace std; int arr[];
int anger[]; struct Node {
int value, index;
Node(int v, int i) :value(v), index(i) {};
bool operator< (const Node& n)const {
return n.value < value;
}
};
priority_queue <Node>pq; int main() {
int n, m;
cin >> n >> m;
for (int i = ; i < n; i++) {
scanf("%d", &arr[i]);
anger[i] = -;
pq.push(Node(arr[i] + m, i));
} for (int i = n - ; i >= ; i--) {
while (!pq.empty()) {
int top = pq.top().value;
if (arr[i] >= top) {
int index = pq.top().index;
pq.pop();
if (i > index) {
anger[index] = i - index - ;
}
else anger[index] = -;
}
else break;
}
} for (int i = ; i < n; i++) {
printf("%d", anger[i]);
if (i != n - )printf(" ");
} return ;
}

O(n) 取得数组中每个元素右边最后一个比它大的元素的更多相关文章

  1. O(n) 取得数组中每个元素右边第一个比它大的元素

    题目: 给定一个整型数组,数组元素随机无序的,要求打印出所有元素右边第一个大于该元素的值. 如数组A=[6,8,9,2,3,5,6] 输出[8,9,-1,3,5,6,-1] 思路: 我们用栈来保存未找 ...

  2. 剑指offer1: 组类型——二维数组中的查找(给定一个数字,查找是否在该数组中)

    1. 思路: 缩小范围 2. 方法: (1)要查找的数字等于数组中的数字,结束查找过程: (2)要查找的数字小于数组中的数字,去除该数字右边的数字,在剩下的数字里查找: (3)要查找的数字大于数组中的 ...

  3. smarty中ifelse、foreach以及获取数组中键值名的一个实例

    <{if empty($history)}> <tr> <td colspan="6">Not any records!</td> ...

  4. 返回数组中指定的一列,将键值作为元素键名array_column

    array_column() 函数 从记录集中取出 last_name 列: <?php // 表示由数据库返回的可能记录集的数组 $a = array( array( 'id' => 5 ...

  5. smarty中判断一个变量是否存在于一个数组中或是否存在于一个字符串中?

    smarty支持php的系统函数可以直接使用{if in_array($str, $arr) || strpos($str, $string)} yes {else} no{/if}

  6. js在数组中查找是否存在某一个数值

    目前想到的方法有这么几个 1.indexOf()  -> ES5 const array = ['apple', 'banance', 'orange'] array.indexOf('appl ...

  7. JS判断数组中的对象的每一个值不能为空

    方法一:使用every()函数,此函数不怎么常用,想要了解更多请自查 //表格 evaluateData为表格的数据 <el-table id="out-table3" :d ...

  8. leetcode 496下一个更大的元素I

    单调递减栈来做,time O(n),spaceO(n)需要一个哈希map class Solution { public: vector<int> nextGreaterElement(v ...

  9. 【Offer】[53-3] 【数组中数值和下标相等的元素】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 假设一个单调递增的数组里的每个元素都是整数并且是唯一的.请编程实现一个函数,找出数组中任意一个数值等于其下标的元素.例如,在数组{-3, ...

随机推荐

  1. qtableview 鼠标划过单元格弹出标签显示单元格内容

    QStandardItem *item = new QStandardItem(show_content); infoTableModel->setItem(1, 1, item); item- ...

  2. Centos7安装部署MongoDB教程

    安装方式: RPM包安装 安装步骤: 一.下载RPM包 下图是需要注意的事项.其一选择MongoDB的社区版本,默认是企业版本.其二,选中版本后,在下方会出现下载地址,直接复制下载即可  二.安装并查 ...

  3. python/shell代码片段

    查看某模块路径 Bash pip show --files selenium 文件编码转换 Bash convmv -f GBK -t UTF-8 --notest -r ydcz_1/ 查找当前目录 ...

  4. Android 横竖屏切换处理

    最近在做一个平板项目,有横竖屏切换的问题,写一下处理的方法. 第一种:禁止横竖屏切换. 对于单独的Activity,使用下面的方式直接配置: <activity android:name=&qu ...

  5. PHP二维码添加logo的方法

    PHP二维码添加logo的方法<pre> public function createqcode($text,$id){ include '/phpqrcode/phpqrcode.php ...

  6. git 删除本地分支,删除远程分支

    本地分支 git branch -d 分支名 远程分支 git push origin --delete 分支名 查看所有分支 git branch -a

  7. CentOS7-Docker 安装 Gitlab

    官方教程 https://docs.gitlab.com/omnibus/docker/ 搜索镜像 [root@master ~]# docker search gitlab 拉取镜像 [root@m ...

  8. LInux因为缺失网关出现Name or service not known的解决方法

    笔者使用的VMware和CentOS 7.0.在安装完镜像包后,便开始配置静态ip.命令如下 vi /etc/sysconfig/network-scripts/ifcfg-ens33 将BOOTPR ...

  9. 嵌入式02 STM32 实验07 串口通信

    STM32串口通信(F1系列包含3个USART和2个UART) 一.单片机与PC机串行通信研究目的和意义: 单片机自诞生以来以其性能稳定,价格低廉.功能强大.在智能仪器.工业装备以及日用电子消费产品中 ...

  10. 一个基于tcp的socket简单对话小例子

    首先我们需要写连个py文件,一个server,一个client. import socket sk = socket.socket() # sk.bind(('ip',port)) sk.bind(( ...