8.6晚 st表+树状数组

· · 个人记录

P3865 【模板】ST 表 && RMQ 问题

错误原因:模版写错了+没用快读快写。

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int lg[N],dp[N][25],a[N];
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
void pre_rmq()
{
    for(int i=1;i<=n;i++)
    {
        dp[i][0]=a[i];
    }
    lg[1]=0;
    for(int i=2;i<=n;i++)
        lg[i]=lg[i>>1]+1;
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
            dp[i][j]=max(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
    return ;
}
int main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    n=read(),m=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    pre_rmq();
    while(m--)
    {
        int x=read(),y=read();
        int j=lg[y-x+1];
        cout<<max(dp[x][j],dp[y-(1<<j)+1][j])<<"\n";
    }
    return 0;
}

P2412 查单词

错误原因:pre_rmq模版写错了

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
int n,m;
int lg[N];
string dp[N][20];
string mx(string a,string b)
{
    string u=a,v=b;
    for(int i=0;i<a.size();i++)
        if(int(u[i])>='a'&&int(u[i])<='z')
            u[i]-=32;
    for(int i=0;i<b.size();i++)
        if(int(v[i])>='a'&&int(v[i])<='z')
            v[i]-=32;
    if(u>v)
        return a;
    return b;
}
void pre_rmq()
{
    for(int j=1;(1<<j)<=n;j++)
        for(int i=1;(1<<j)+i-1<=n;i++)
            dp[i][j]=mx(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
    return ;
}
int main()
{
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    cin>>n>>m;
    lg[0]=-1;
    for(int i=1;i<=n;i++)
    {
        char a[105];
        cin>>a;
        dp[i][0]=a;
        lg[i]=lg[i>>1]+1;
    }
    pre_rmq();
    while(m--)
    {
        int x,y;
        cin>>x>>y;
        int j=lg[y-x+1];
        cout<<mx(dp[x][j],dp[y-(1<<j)+1][j])<<"\n";
    }
    return 0;
}

P5149 会议座位

这道题目可以转换成找逆序对,不过是从数字变成字符。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=1e5+5;
int n;
int c[N],b[N];
struct node
{
    int id;
    string x;   
}a[N];
int lowbit(int x)
{
    return x&-x;
}
int get_sum(int x)
{
    int res=0;
    while(x)
    {
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
void modify(int x,int val)
{
    while(x<=n)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
    return ;
}
map<string,int> mp;
signed main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].x;
        mp[a[i].x]=i;
    }
    string s;
    for(int i=1;i<=n;i++)
    {
        cin>>s;
        b[i]=mp[s];
    }
    int idx=0,ans=0;
    for(int i=1;i<=n;i++)
    {
        modify(b[i],1);
        ans+=i-get_sum(b[i]-1)-1;
    }
    cout<<ans;
    return 0;
}

P1714 切蛋糕

题目大意:

题目要:求在一个长度为n的数列中,找到一个连续子段(长度不超过m),使得子段的和最大。形式化地说,即找到[l, r](满足r-l+1 ≤ m),最大化sum(p[l..r])。

核心思路

可以用用前缀和+单调队列的组合:

  1. 前缀和:将子段和转化为前缀和的差值
  2. 单调队列:维护一个队列存储前缀和的索引,确保队列中前缀和的值单调递增,从而快速找到最优的前缀和,使得sum[r] - sum[l-1]最大。
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=5e5+10;
int n,m;
int a[N],sum[N],ans=-1e9;
deque<int> q;
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    q.push_back(0);
    for(int r=1;r<=n;r++)
    {
        while(!q.empty()&&sum[r]<=sum[q.back()])
            q.pop_back();
        if(r-q.front()+1>m+1)
            q.pop_front();
        ans=max(ans,sum[r]-sum[q.front()]);
        q.push_back(r);
    }
    cout<<ans;
    return 0;
}

P4378 [USACO18OPEN] Out of Sorts S

题目分析:

题目要求计算Bessie实现的冒泡排序中"moo"指令的输出次数。该冒泡排序的逻辑是:每轮遍历数组交换相邻逆序对,直到数组有序,每轮开始时输出一次"moo"。因此,"moo"的次数等于冒泡排序需要的总轮数。

核心思路:

排序中,每轮最多让每个元素向左移动1位。对于元素a[i],设其在排序后数组中的位置为k:

因此,总轮数由最大的i - k 决定。最终轮数为该最大值+1。

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=100005;
int n;
int a[N],c[N],b[N];
int lowbit(int x)
{
    return x&-x;
}
int get_sum(int x)
{
    int res=0;
    while(x)
    {
        res+=c[x];
        x-=lowbit(x);
    }
    return res;
}
void modify(int x,int val)
{
    while(x<=n)
    {
        c[x]+=val;
        x+=lowbit(x);
    }
    return ;
}
map<int,int> mp;
signed main()
{
    //freopen(".in","r",stdin);
    //freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        b[i]=a[i];
    }
    int ans=0;
    sort(b+1,b+n+1);
    int idx=unique(b+1,b+n+1)-b-1;
    for(int i=1;i<=n;i++)
    {
        int x=lower_bound(b+1,b+idx+1,a[i])-b;
        ans=max(ans,get_sum(idx)-get_sum(x));
        modify(x,1);
    }
    cout<<ans+1;
    return 0;
}