409 总结

· · 个人记录

11.30 东塘 409 教室 - 第一次课【小田】

T854973 异或

题目传送门

时间 \approx 30min

分数 =0

😭😭😭

题目都没提交就不放了

正确思路

题目大意

对于每次询问 x,在序列中找到长度最短的区间,使得区间异或和 \ge x。

思路

询问的次数很多但是 n 的范围是很小的,所以差不多要预处理,预处理出每个区间长度的最大区间异或和,然后对于每次询问直接循环 n 次找出每个区间长度对应的最大值,看是否大于等于 x,这样可以做到 O(n^2+nm)。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e3+5;
int a[N],n,m,sum[N],maxn[N];
signed main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]^a[i];
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=i;j<=n;j++)
        {
            maxn[j-i+1]=max(maxn[j-i+1],sum[j]^sum[i-1]);
        }
    }
    while(m--)
    {
        int x;
        cin>>x;
        int f=0;
        for(int i=1;i<=n;i++)
        {
            if(maxn[i]>=x)
            {
                cout<<i<<'\n';
                f=1;
                break;
            }
        }
        if(!f)
        {
            cout<<-1<<'\n';
        }

    }
    return 0;
}

T855429 报数

题目传送门

原题目传送门

时间 \approx 30 \min

分数 = 30

🙂🙂🙂

暴力

暴力

预处理暴力

正确思路

题目大意

一个正整数 x 不能报出,当且仅当包含数字 7 的数字 y,使得 y \mid x。

对于每次询问,给出一个 x,若 x 不可报出,输出−1,否则输出 x 的下一个可以报出的数字。

思路

我们可以用筛法来进行,一个 vis 来存数字 i 是否是可以报的,一个 sum 来存数字 i 的下一个可以报的数字是谁,一个数字如果是不满足条件的,那么它的所有倍数都不满足条件,所有可以用类似的方式来进行标记。

这样大约是能做到 O(x \log x)

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
int vis[N],sum[N],t;
vector<int>a;
bool check(int x)
{
    while(x)
    {
        if(x%10==7)
        {
            return 1;
        }
        x/=10;
    }
    return 0;
}
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        if(!check(i)){
            continue;
        }
        if(vis[i])
        {
            continue;
        }
        for(int j=i;j<=n;j+=i)
        {
            vis[j]=1;
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            a.push_back(i);
        }
    }
    for(int i=0;i<a.size()-1;i++)
    {
        sum[a[i]]=a[i+1];
    }
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    init((int)1e7+5);
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        if(vis[x]==1)
        {
            cout<<-1<<'\n';
        }
        else
        {
            cout<<sum[x]<<'\n';
        }
    }
    return 0;
}

T687115 消息

题目传送门

时间 \approx 40 \min

分数 = 30

🙂🙂🙂

暴力

也是我写的。很简单,有一个数组来存储他在哪个软件上,哪一个 cnt 来存到哪里了,操作二就判断整个数组如果是 x 那么就标记为已读,才做三就循环到 x 标记。

正确思路

题目大意

- `1 x`,第 $x$ 个应用弹出一条新消息,所有消息按 $1,2,3,...$ 编号。 - `2 x`,读第 x 个应用的所有消息。 - `3 x`,读全局前 x 条消息。 #### 思路 把无 3 操作和无 2 操作结合起来。 操作 3 的阅读会影响到 $cnt_i$ 的值,我们这里需要知道两个关键信息: - 一条消息属于哪一个应用 - 一条消息是否已读我们考虑维护一个 **$last_i$ 表示第 i 个应用上次处理到的最大消息编号**我们找到这条消息所属的应用,然后判断大小来检测已读,我们只用记录消息变量 $idx$ 执行操作二的时候让 $last[i]=idx$ 即可。 时间复杂度:$O(n+q)

空间复杂度:O(n)

正确代码

#include<bits/stdc++.h>
using namespace std;
const int N=3e5+10;
int n,m,idx=0,sum=0,p=0,cnt[N],bel[N],last[N]; 
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    while(m--)
    {
        int op,x;
        cin>>op>>x;
        if(op==1)
        {
            cnt[x]++;
            sum++;
            idx++;
            bel[idx]=x;
        }
        else if(op==2)
        {
            sum-=cnt[x];
            cnt[x]=0;
            last[x]=idx;
        }
        else
        {
            while(p<x)
            {
                p++;
                int k=bel[p];
                if(p>last[k])
                {
                    sum--;
                    last[k]=p;
                    cnt[k]--;
                }
            }
        }
        cout<<sum<<'\n';

    }

    return 0;
}

T855429 报数

题目传送门

原题目传送门

时间 \approx 30 \min

分数 = 0

正确思路

题目大意

每秒在所有蚯蚓里找到最长的一只,设长度为x,将其切为\lfloor px \rfloor和x - \lfloor px \rfloor两段,p = \frac{u}{v},切出的长度为0的段也保留,同时其余所有蚯蚓的长度增加q。 求:

思路

我们先想到优先队列来优化考虑不真的让每个蚯蚓长度增加,而是维护一个全局的偏移量 sum,蚯蚓实际的长度都需要加上 sum,那么只需要让 sum + q 就可以表示所有蚯蚓的长度都增加了q。但是最新的两只蚯蚓长度是不变的,所以它们的长度在压入优先队列中时需要减去 sum 来抵消掉,这样就做到了O(1)时间的整体加法。

这样子就可以做到 O(m \times \log n),但是应为常数问题会T几个点。

#include<bits/stdc++.h>
#define int long long
using namespace std;

/*
1. 优先队列 
2. 如何快速的让其他的蚯蚓都加 q 

    定义一个偏移量offset,把蚯蚓的长度视作全部都要 + offset 
*/

int n, m, q, u, v, t, offset = 0;
priority_queue<int> a;

bool cmp(int a1, int a2) {
    return a1 > a2;
}

signed main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin >> n >> m >> q >> u >> v >> t; 
    for (int i=1; i<=n; i++) {
        int x;
        cin >> x;
        a.push(x);
    }

    for (int i=1; i<=m; i++) {
        /*
            1. 找出最大数字
            2. 其他的数字都加q
            3. 新的计算出来的数字加入数组中 
        */
        int x = a.top() + offset; 
        a.pop();
        if (i % t == 0) cout << x << " ";
        offset += q;  // 所有蚯蚓都加q
        int b = x * u / v;
        a.push(b - offset);
        a.push(x - b - offset); 
    } 
    cout << '\n';
    for (int i=1; i<=n+m; i++) {
        if (i % t == 0) cout << a.top() + offset << " ";
        a.pop();
    }

    return 0;
}

我们可以把优先队列分解成三个队列

这样就可以做到 O(1) 时间取出最大值了。

正确代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e5+5;
int n,m,q,u,v,t,offset=0;
int a[N];
queue<int>a1,a2,a3;
bool cmp(int a1,int a2)
{
    return a1>a2;
}
int getmax() 
{
    int x=LLONG_MIN,y=LLONG_MIN,z=LLONG_MIN;
    if(a1.size())x=a1.front();
    if(a2.size())y=a2.front();
    if(a3.size())z=a3.front();
    if(x>=y and x>=z)
    {
        a1.pop();
        return x;
    }
    if(y>=z)
    {
        a2.pop();
        return y;
    }
    a3.pop();
    return z;
}

signed main() {
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m>>q>>u>>v>>t; 
    for (int i=1;i<=n;i++) 
    {
        cin>>a[i];
    }
    sort(a+1, a+1+n, cmp);
    for(int i=1;i<=n;i++)
    {
        a1.push(a[i]);
    } 

    for(int i=1;i<=m;i++) 
    {
        int x=getmax()+offset; 

        if(i%t==0) 
        {
            cout<<x<<' ';
        }
        offset+=q; 
        int b=x*u/v; 
        a2.push(b-offset);
        a3.push(x-b-offset);
    } 
    cout << '\n';
    for (int i=1;i<=n+m;i++) 
    {
        int x=getmax()+offset;
        if(i%t==0)
        {
            cout<<x<<' ';
        } 
    }

    return 0;
}