暑假第23节课总结

· · 算法·理论

考试情况

模拟赛分数 改题用时
T1 100 0 \ min
T2 10 8\ min
T3 0 15\ min
T4 0 23\ min

题目总结

## T2「 B4100 [CSP-X2023 山东] 赚钱 」

简要题意:

按编号 1→n 依次旅游(不走回头路)。必须在某城市 x 买入一个纪念品、在后某城市
y > x 卖出(必须交易一次),求最大利润(可能为负)。

错误原因

没思路,就输出-1骗分。

错误代码

#include<bits/stdc++.h>
using namespace std;
int main()
{
    cout<<"-1";
    return 0;
}

解题思路:

采用‌“后缀最大值预处理 + 枚举买入点”‌的策略,从后往前遍历计算出每个位置及其之后的最高价格 maxx[i],再从前向后枚举每个城市作为买入点,利用 maxx[i+1] - a[i] 直接获取该点最佳卖出利润。

正确代码

#include<bits/stdc++.h>
using namespace std;
int a[200005],maxx[200005];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=n;i>=1;i--)
    {
        maxx[i]=max(maxx[i+1],a[i]);
    }
    int cnt=-2e9;
    for(int i=1;i<n;i++)
    {
        cnt=max(cnt,maxx[i+1]-a[i]);
    }
    cout<<cnt;
    return 0;
}

T3「P2058 [NOIP 2016 普及组] 海港 」

错误原因

模拟错了

错误代码

#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int st[300005];
int main()
{
    int T;
    cin>>T;
    int last=0;
    int cnt=0;
    while(T--)
    {
        cnt=0;
        int t,n;
        cin>>t>>n;
        if(t-86400>last)
        {
            while(q.size())
            {
                st[q.front()]--;
                q.pop();
            }
        }


        int x;
        for(int i=1;i<=n;i++)
        {
            q.push(x);
            st[x]++;

        }
        for(int i=0;i<q.size();i++)
        {
            if(st[x]==1)
            {
                cnt++; 
            }
        }

        cout<<cnt<<endl;
        last=t;
    }
    return 0;
}

正确思路

新船到达:乘客国籍逐一 记录有多少个国家来的人的数组mp[那个国家]++ ,船信息(时间、人数、乘客列表)入队。然后从队头赶走所有 t ≤ 当前时间-86400 的船,逐乘客mp[那个国家]--、归零则删掉那个国家。输出剩下的国家数量。

正确代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int t,n;
}a[100010];
vector<int>x[100010];
int hh=1,tt=1;
map<int,int>mp;
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int t,n;
        cin>>t>>n;
        a[tt].t=t;
        a[tt].n=n;
        for(int i=1;i<=n;i++)
        {
            int xx;
            cin>>xx;
            mp[xx]++;
            x[tt].push_back(xx);
        }
        tt++;

        while(hh<tt)
        {
            int i=hh;
            if(a[i].t<=t-86400)
            {
                for(int j=0;j<a[i].n;j++)
                {
                    mp[x[i][j]]--;
                    if(mp[x[i][j]]==0)
                    {
                        mp.erase(x[i][j]);
                    }
                }
            }
            else
            {
                break;
            }
            hh++;
        }
        cout<<mp.size()<<endl;
    }
    return 0;
}

T4「P7912 [CSP-J 2021] 小熊的果篮」(60分)

错误原因

考试当时没有想出来思路

错误代码

无

正确思路

先把1~n个元素放入队列中,如果自己和上一个不一样那就是个块最左边的需要删除,并输出。其他的就把它放入队尾等下一轮,等把所有元素枚举完后,就换行。

正确代码

#include<bits/stdc++.h>
using namespace std;
queue<int>q;
int cnt[200010];
int main()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>cnt[i];
        q.push(i);
    }
    while(q.size())
    {
        int last=1-cnt[q.front()];
        int len=q.size();
        for(int i=1;i<=len;i++)
        {
            if(cnt[q.front()]!=last)
            {
                last=cnt[q.front()];
                cout<<q.front()<<" ";
                q.pop();
            }
            else
            {
                q.push(q.front());
                q.pop();
            }
        }
        cout<<'\n';
    }
    return 0;
}

End

发挥失常了,下次继续。

乾坤未定,你我皆是黑马。