HDU 5875 H - Function 用单调栈水过了

单调栈,预处理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 + 20;
int a[maxn];
int to[maxn];
struct node {
    int id;
    int val;
}stack[maxn];
void work() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }
    int top = 1;
    a[n + 1] = -1;
    stack[top].val = a[1];
    stack[top].id = 1;
    for (int i = 2; i <= n + 1; ++i) {
        while (top >= 1 && 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 = 1; 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 == 0) 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 0;
}

时间: 2024-12-14 18:12:42

HDU 5875 H - Function 用单调栈水过了的相关文章

hdu 4923 Room and Moor (单调栈+思维)

题意: 给一个0和1组成的序列a,要构造一个同样长度的序列b.b要满足非严格单调,且 值为0到1的实数.最后使得  sum((ai-bi)^2)最小. 算法: 首先a序列开始的连续0和末尾的连续1是可以不考虑的.因为只要b序列对应开头为0. 末尾为1,既不影响单调性又能使对应的(ai-bi)^2=0. 然后, 先找111100.11100.10这样以1开始以0结束的序列块.每一块对应的b值相等且均为 这一块的平均值,即1的个数/0和1的总个数. 但是要满足b的单调性,则我们用栈来维护,如果后面一

hdu 5696 区间的价值 单调栈+rmq

区间的价值 Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description 我们定义“区间的价值”为一段区间的最大值*最小值. 一个区间左端点在L,右端点在R,那么该区间的长度为(R−L+1). 现在聪明的杰西想要知道,对于长度为k的区间,最大价值的区间价值是多少. 当然,由于这个问题过于简单. 我们肯定得加强一下. 我们想要知道的是,对于长度为1∼n

【HDU 5875】Function

传送门 题目描述 The shorter, the simpler. With this problem, you should be convinced of this truth.  You are given an array A of N postive integers, and M queries in the form (l,r). A function F(l,r) (1≤l≤r≤N) is defined as: qwq You job is to calculate F(l,

单调栈 BZOJ1345 [Baltic2007]序列问题Sequence

1345: [Baltic2007]序列问题Sequence Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 899  Solved: 471[Submit][Status][Discuss] Description 对于一个给定的序列a1, -, an,我们对它进行一个操作reduce(i),该操作将数列中的元素ai和ai+1用一个元素max(ai,ai+1)替代,这样得到一个比原来序列短的新序列.这一操作的代价是max(ai,ai+1).进行n-

bzoj1012最大数maxnumber——单调栈

题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1012 单调栈水题:用了一下lower_bound二分. 代码如下: #include<iostream> #include<cstdio> #include<algorithm> using namespace std; int m,d,len,a[200005],sta[200005],top,lst; char s[5]; int main() { scanf

hdu 5875(单调栈)

Function Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1866    Accepted Submission(s): 674 Problem Description The shorter, the simpler. With this problem, you should be convinced of this tr

HDU 4923 Room and Moor (多校第六场C题) 单调栈

Problem Description PM Room defines a sequence A = {A1, A2,..., AN}, each of which is either 0 or 1. In order to beat him, programmer Moor has to construct another sequence B = {B1, B2,... , BN} of the same length, which satisfies that: Input The inp

HDU 2870 Largest Submatrix (单调栈)

http://acm.hdu.edu.cn/showproblem.php?pid=2870 Largest Submatrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1569    Accepted Submission(s): 748 Problem Description Now here is a matrix wit

hdu - 5033 - Building(单调栈)

题意:N 幢楼排成一列(1<=N<=10^5),各楼有横坐标 xi(1<=xi<=10^7) 以及高度 hi(1<=hi<=10^7),在各楼之间的Q个位置(1<=Q<=10^5),问这些位置可以仰望天空的夹角是多少度. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5033 -->>将楼和人的位置一起按 x 排序.. 从左往右扫,单调栈维护斜率小的.. 从右往左扫,单调栈维护斜率大的.. #inc