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 ...
随机推荐
- UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-3: ordinal not in range(128)
py文件直接在cmd窗口用python命令执行时正常:代码逐句在ipython中也正常:但是, 在wingIDE中运行报错“UnicodeEncodeError: 'ascii' codec can' ...
- 为什么修改头文件make不重新编译
make是根据依赖文件的时间戳来决定要不要重新编译的.在: object: deplist # actions 中,可以把头文件加进deplist,这样修改头文件后,make就会重新编译了. 单纯地修 ...
- laravel 5.4 运行 make:auth 报错
Laravel 5.4 migrate时报错: Specified key was too long error 问题根源 MySQL支持的utf8编码最大字符长度为3字节,如果遇到4字节的宽字符就会 ...
- 蓝桥杯训练 2n皇后问题
给定一个n*n的棋盘,棋盘中有一些位置不能放皇后.现在要向棋盘中放入n个黑皇后和n个白皇后,使任意的两个黑皇后都不在同一行.同一列或同一条对角线上,任意的两个白皇后都不在同一行.同一列或同一条对角线上 ...
- 【Lintcode】033.N-Queens II
题目: Follow up for N-Queens problem. Now, instead outputting board configurations, return the total n ...
- 实现PIX需要参考的标准资料
•初步了解PIX V2和V3:"IHE_ITI_TF_Rev8-0_Vol1_FT_2011-08-19"中第5章和第23章 •了解PIX V2相关事务: "IHE_IT ...
- angular.foreach 格式
angular有自己的生命周期.循环给一个 angular监听的变量复值时.最好还是用angular自带的循环方法.“angular.foreach” 格式: var objs =[{a:1},{a: ...
- Java正则表达式之Matcher介绍
Matcher方法如下: Matcher方法如下: Matcher appendReplacement(StringBuffer sb, String replacement) 将当前匹配子串替换为指 ...
- 7.10实习培训日志-markdown Git
父模块github地址 一. markdown 1. markdown列表 html是一种发布的格式,markdown是一种书写的格式 区块引用 列表 图片 表格 html 标题 记笔记 写博客 2. ...
- 【原】spring+springmvc+mybatis整合
整合框架的代码结构: 最全约束: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...