HDU 5875 H - Function 用单调栈水过了
http://acm.hdu.edu.cn/showproblem.php?pid=5875
单调栈,预处理to[i]表示第一个比a[i]小的数字,一直跳就可以。
这题是数据水而已。
这里学习下单调栈。
构造一个单调递增的栈,并且记录元素大小的同时记录它的id。
每次进来一个小的元素的话,就出栈,同时出栈的这个元素的to[id] = i了,因为这个元素是当时最大的。然后这个a[i]是第一个能让它出栈的,所以就是它了。后面的同理。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1e5 + ;
int a[maxn];
int to[maxn];
struct node {
int id;
int val;
}stack[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
scanf("%d", &a[i]);
}
int top = ;
a[n + ] = -;
stack[top].val = a[];
stack[top].id = ;
for (int i = ; i <= n + ; ++i) {
while (top >= && a[i] <= stack[top].val) {
to[stack[top].id] = i;
top--;
}
++top;
stack[top].val = a[i];
stack[top].id = i;
}
// for (int i = 1; i <= n; ++i) {
// printf("%d ", to[i]);
// }
// printf("\n");
int m;
scanf("%d", &m);
for (int i = ; i <= m; ++i) {
int L, R;
scanf("%d%d", &L, &R);
int ans = a[L];
int t = to[L];
while (t <= R) {
ans %= a[t];
if (ans == ) break;
t = to[t];
}
printf("%d\n", ans);
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
int t;
scanf("%d", &t);
while (t--) work();
return ;
}
HDU 5875 H - Function 用单调栈水过了的更多相关文章
- HDU 4923 Room and Moor (单调栈)
题意: 给你一个A数列,让你求一个单调递增的B数列(0<=bi<=1),使得sum{(ai-bi)^2}最小. 思路: 很明显,如果A = 0...01...1,那么bi=ai即可. 可以 ...
- hdu 5696 区间的价值 单调栈+rmq
区间的价值 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem D ...
- hdu 4923 Room and Moor (单调栈+思维)
题意: 给一个0和1组成的序列a,要构造一个相同长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得 sum((ai-bi)^2)最小. 算法: 首先a序列開始的连续0和末尾的连续1是能 ...
- HDU - 5033: Building(单调栈 ,求一排高楼中人看楼的最大仰角)
pro:现在在X轴上有N个摩天大楼,以及Q个人,人和大楼的坐标各不相同,保证每个人左边和右边都有楼,问每个人能看到天空的角度大小. sol:不难想到就是维护凸包,此题就是让你模拟斜率优化,此处没有斜率 ...
- bzoj 1657 [Usaco2006 Mar]Mooo 奶牛的歌声——单调栈水题
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 #include<iostream> #include<cstdio ...
- bzoj 1657 Mooo 奶牛的歌声 —— 单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1657 单调栈水题. 代码如下: #include<iostream> #incl ...
- bzoj1012最大数maxnumber——单调栈
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1012 单调栈水题:用了一下lower_bound二分. 代码如下: #include< ...
- HDU 5875 Function (线段树+gcd / 单调栈)
题意:给你一串数a再给你一些区间(lef,rig),求出a[lef]%a[lef+1]...%a[rig] 题解:我们可以发现数字a对数字b取模时:如果a<b,则等于原数,否则a会变小至少一半. ...
- hdu 5875(单调栈)
Function Time Limit: 7000/3500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
随机推荐
- stack_2.由两个栈组成队列
思路: 用两个栈($stack_a, $stack_b),当push的时候,压入$stack_a, 让pop的时候,先把$stack_a元素依次全部倒入$stack_b中,再对$stack_b进行po ...
- MongoDB 分片的原理、搭建、应用 !
MongoDB 分片的原理.搭建.应用 一.概念: 分片(sharding)是指将数据库拆分,将其分散在不同的机器上的过程.将数据分散到不同的机器上,不需要功能强大的服务器就可以存储更多的数据和处 ...
- bzoj 1119 [POI2009] SLO & bzoj 1697 牛排序 —— 置换+贪心
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1119 https://www.lydsy.com/JudgeOnline/problem.p ...
- Unix Timestamp
class Foundation_API DateTime /// This class represents an instant in time, expressed /// in years, ...
- CentOS下安装配置Samba服务器
0 环境介绍 VMWARE12下安装的CENTOS7虚拟机.宿主机为WIN7. 1 离线安装 费了九牛二虎之力,下载各种依赖,还是有问题,转向在线安装. 2 在线安装 虚拟机采用默认的配置: 其次,网 ...
- 错误:(26, 13) Failed to resolve: com.android.support:appcompat-v7:27.+
小编也是初学安卓,今天配置环境的时候遇到这个问题了,搞了半天终于找到了问题 在build.gradle中添加 allprojects { repositories { jcenter() maven ...
- (转)apache2.2.x+tomcat7.0.x集群+…
apache http server下载地址 http://httpd.apache.org/download.cgi#apache22 这里下载的是httpd-2.2.21-win32-x86-op ...
- 来自word2013发布的测试文档
SWIG 是一个非常优秀的开源工具,支持您将 C/C++ 代码与任何主流脚本语言相集成. 此外,它向更广泛的受众公开了基本代码,改善了可测试性,让您的 Ruby 代码库某部分能快速写出高性能的 C/C ...
- 鼠标右键添加cmd
给鼠标右键添加 cmd https://jingyan.baidu.com/article/3f16e003c408142591c103b2.html 有一些软件,最好不要装到Program File ...
- 微信小程序开发之拼接json数组字符串
直接上代码 : var imageitem; var imageitemstring=''; for(var i=0;i< that.data.fbimages.length;i ...