洛谷P3865 ST表

传送门啦

思路:

$ f[i][j] $ 表示从 $ i $ 开始,包含 $ 1<<j $ 个元素的区间的区间最大值;

转移方程: $ f[i][j]=max_(f[i][j-1],f[i+(1<<j-1)][j-1] $ ;

查询 $ (l,r) $ :

$ p=log_2(r-l+1) $ ;

$ max(l,r)=max(f[l][p],f[r-(1<<p)+1][p]) $ ;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath> 
#define re register
using namespace std ;

int n , m , a[100005] , l , r ;
int f[10000010][21] ;

inline int read () {
    int f = 1 , x = 0 ; 
    char ch = getchar () ;
    while(ch > '9' || ch < '0') {if(ch == '-') f = -1 ; ch = getchar () ;}
    while(ch >= '0' && ch <= '9') {x = (x << 1) + (x << 3) + ch - '0' ; ch = getchar () ;}
    return x * f ;
}

inline void st(int x) {
    for(re int i = 1 ; i <= 21 ; ++ i) 
        for(re int j = 1 ; j + (1 << i) <= x + 1 ; ++ j)
            f[j][i] = max(f[j][i - 1] , f[j + (1 << (i -1))][i - 1]) ;
}

inline int query(int l , int r) {
    int k = log2(r - l + 1) ;
    return max(f[l][k] , f[r - (1 << k) + 1][k]) ;
}

int main () {
    n = read () ; m = read () ;
    for(re int i = 1 ; i <= n ; ++ i) {
        f[i][0] = read () ;
    }
    st(n) ;
    for(re int i = 1 ; i <= m ; ++ i) {
        l = read () ; r = read () ;
        printf("%d\n" , query(l , r)) ;
    }
    return 0 ;
}

转载于:https://www.cnblogs.com/Stephen-F/p/10415769.html

洛谷 P7384 是一道与字符串处理相关的题目,题目要求对输入的字符串进行特定的处理操作,并输出处理后的结果。以下是与该题相关的内容及题解思路: ### 题目大意 输入一个只包含小写字母的字符串,按照如下规则进行处理: 1. 如果字符串中存在长度为 2 的回文子串(即两个相邻字符相同),则将这两个字符删除。 2. 删除后,新的字符串可能会形成新的回文子串,需要继续删除,直到无法再删除为止。 3. 输出最终处理后的字符串。 ### 解题思路 这道题的核心是模拟删除过程。由于删除字符后可能会形成新的相邻重复字符,因此不能仅仅遍历一次字符串。可以使用栈来模拟这一过程[^5]: 1. 遍历字符串中的每一个字符。 2. 每次将当前字符压入栈中。 3. 检查栈顶的两个字符是否相同,如果相同,则将这两个字符弹出。 4. 最后,栈中剩余的字符即为处理后的字符串。 ### 示例代码 ```cpp #include <iostream> #include <stack> using namespace std; int main() { string s; cin >> s; stack<char> st; for (char c : s) { st.push(c); if (st.size() >= 2) { char top1 = st.top(); st.pop(); char top2 = st.top(); st.pop(); if (top1 != top2) { st.push(top2); st.push(top1); } } } string result; while (!st.empty()) { result = st.top() + result; st.pop(); } cout << result << endl; return 0; } ``` ### 时间复杂度分析 - 每个字符最多入栈和出栈一次,因此时间复杂度为 $O(n)$,其中 $n$ 是字符串的长度。 - 空间复杂度也为 $O(n)$,因为栈最多存储所有字符。 ### 注意事项 - 输入的字符串可能为空,需要处理边界情况。 - 输出的字符串必须是处理后的最终结果,不能包含任何相邻的重复字符。 - 使用栈结构可以高效地处理这种需要反复操作的问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值