poj 3264

http://poj.org/problem?id=3264

洛谷 P2880

https://www.luogu.org/problemnew/show/P2880

题目描述

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

一个农夫有N头牛,每头牛的高度不同,我们需要找出最高的牛和最低的牛的高度差。

输入输出格式

输入格式:

Line 1: Two space-separated integers, N and Q.

Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i

Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

输出格式:

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

输入输出样例

输入样例#1: 复制

6 3
1
7
3
4
2
5
1 5
4 6
2 2
输出样例#1: 复制

6
3
0 RMQ的裸题,直接用ST算法AC就可以。
跑一遍最大值再来一遍最小值。
AC Code:
#include<cstdio>
#include<algorithm>
using namespace std;
int n,q;
int lg[];
int f[][],p[][];
int main()
{
scanf("%d%d",&n,&q);
for(int i=;i<=n;i++)
{
scanf("%d",&f[i][]);
p[i][]=f[i][];
}
for(int i=;i<=n;i++)
lg[i]=lg[i>>]+;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
{
f[i][j]=max(f[i][j-],f[i+(<<(j-))][j-]);
p[i][j]=min(p[i][j-],p[i+(<<(j-))][j-]);
}
for(int i=;i<=q;i++)
{
int x,y;
scanf("%d%d",&x,&y);
int k=lg[y-x+];
printf("%d\n",max(f[x][k],f[y-(<<k)+][k])-min(p[x][k],p[y-(<<k)+][k]));
}
return ;
}

USACO Balanced Lineup的更多相关文章

  1. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  2. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  3. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  4. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  5. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  6. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  7. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

  8. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  9. [POJ] 3264 Balanced Lineup [ST算法]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

随机推荐

  1. Python之爬虫-校花网

    Python之爬虫-校花网 #!/usr/bin/env python # -*- coding:utf-8 -*- import re import requests # 拿到校花网主页的内容 re ...

  2. windows server 2008配置多用户远程连接

    打开开始菜单->管理工具->远程桌面服务->远程桌面会话主机配置 右键限制每个用户只能进行一个会话->常规->勾掉限制每个与用户只能进行一个会话 右键远程桌面授权模式-& ...

  3. Spring Cloud和Spring Boot的版本问题

    很多人在使用springboot和springcloud,但是对于这两者之间的版本关系不是很清楚,特别是在面临升级的时候不知道该如何操作.本文简要摘录的官方文档的部分内容作为依据,供广大同行参考. 问 ...

  4. 运维相关指标数据采集并ES入仓 - 运维笔记

    为了进行数字化IT治理,需要对一些应用进程相关指标进行采集并入库.收集到的应用指标数据最好要进行ES入仓,入到Kafka里面,并通过Kibana可视化展示. 需要进行采集的应用进程相关指标如下: ES ...

  5. mysql 写计数器需要注意的问题

    MySql计数器,如网站点击数,如何实现高性能高并发的计数器功能 由于并发的时候 不能同时写入一行数据 所以要分开写<pre>先创建表CREATE TABLE `article_view` ...

  6. Budgie Desktop in Linux 无法使用触摸板右键功能

    问题描述 Budgie桌面在Linux环境下很好用,不过最近发现触摸板的右键功能是不可用的 解决方法 经查阅Gnome官方文档(Budgie有很多功能基于Gnome)发现是Gnome官方把这个功能去掉 ...

  7. 排障利器之远程调试与监控 --jmx & remote debug

    监控和调试功能是应用必备的属性之一,其手段也是多种多样. 一般地,我们可以通过:线上日志, zabbix, grafana, cat 等待系统做一问题留底,有问题及时报警,从而达到监控效果. 而对于应 ...

  8. c# "As" 与 "Is"效率 (原发布csdn 2017-10-07 11:49:18)

    十一长假就要过去了,今年假期没有回家,一个人闲着无聊就在看C#语言规范5.0中文版.昨天看了 is运算符和 as运算符,平时项目中也有用到这两种符号,对于其效率也没有进行比较过,趁着假期有空,先看下效 ...

  9. 2.将视图添加到 ASP.NET Core MVC 应用

    在本部分中,将修改 HelloWorldController 类,进而使用 Razor 视图文件来顺利封装为客户端生成 HTML 响应的过程. 当前,Index 方法返回带有在控制器类中硬编码的消息的 ...

  10. C#开发自动照片(图片)裁剪(缩放)工具

    1.需求分析 用winform窗体程序,开发一个能够自动.批量对图片进行缩放和裁剪的程序. 原本想直接从网上找类型的工具直接用,但是无奈现在网上能找到的工具,要么不能用,要么就是很 恶心的下载完后还有 ...