CSP-J2021题解(个人作业)

· · 个人记录

感受:

T1:之前用暴力AC过,现在数据改了,少了10分

T2:浪费太久时间,暴力骗分!

T3:看到第一眼,觉得很逊,但真抠细节(对这道题亲切地问候)

T4当我看到这道题时,已经晚了......

T1:

这道题第一眼是暴力,但n取10^9,必TLE,所以我们要想别的办法.总共取出的k个糖果最终会被n个小朋友瓜分,剩下的糖果数就是答案.可以用k\%n表示.而k\%n的最大值是n-1,所以理论上答案是n-1.但n-1可能不在l~r的范围内,取不到.比如样例二,这就是这道题的第2种情况.这里我们要知道一个规律:在一个单调递增的区间里,ai%n的值也是递增的 所以这种情况下k的最大值是r\%n.上代码

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,l,r;
signed main()
{
    cin>>n>>l>>r;
    if(n-1-l%n+l<=r)cout<<n-1;
    else cout<<r%n;
    return 0;
}

还会有小朋友问,万一r是n的倍数,r\%n为0怎么办?放心,如果r是n的倍数,一定是第一种情况.

T2:

这道题用暴力能得52分,别问我为什么知道.但要得满分就不能用暴力+sort的思路.要在执行操作一时就排好序,这样执行操作二时就不需要重新排序(有点记忆化的思想),题目中也说了,执行操作一的情况最多5000次,说明我们最多排序5000次,绝对不会超时.具体看代码!

#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,q,d[8005];//存排序完各数的位置编号 
struct node
{
    int w,i;//w表示值,i表示编号 
}a[8005];
bool cmp(node a,node b)
{
    if(a.w!=b.w)return a.w<b.w;
    else return a.i<b.i;//不要忘记特判 
}
signed main()
{
    cin>>n>>q;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].w;
        a[i].i=i;
    }
    sort(a+1,a+1+n,cmp);//第一次排序 
    for(int i=1;i<=n;i++)d[a[i].i]=i;
    while(q--)
    {
        int b,x,y;
        cin>>b>>x;
        if(b==1)//第一种情况 
        {
            cin>>y;
            a[d[x]].w=y;
            //这里排序不能用sort不然会超时,浪费时间 。只需处理更改的数 
            for(int j=2;j<=n;j++)
            {
                if(cmp(a[j],a[j-1]))swap(a[j],a[j-1]);
            }//判断是否能往前挪 
            for(int j=n;j>1;j--)
            {
                if(cmp(a[j],a[j-1]))swap(a[j],a[j-1]);
            }//判断是否能往后移 
            for(int i=1;i<=n;i++)d[a[i].i]=i;//更新 
        }
        else//操作二直接输出,飞快! 
        {
            cout<<d[x]<<endl;
        }
    }
    return 0;
}

T3

这道题在比赛时没有用到更好的方法,不知道该用什么方法,直到老师教了我两个很好用的函数.

sscanf:对字符数组进行分解操作.

sprintf:对字符数组进行组合操作.

有了这两个函数,在用上map,这道题就差不多解决了.

#include <bits/stdc++.h>
using namespace std;
int n;
map<string,int>m;
bool dz(char s[])
{
    int a,b,c,d,e;
    int num=sscanf(s,"%d.%d.%d.%d:%d",&a,&b,&c,&d,&e);
    if(num!=5)return 0;
    if(a<0||a>255)return 0;
    if(b<0||b>255)return 0;
    if(c<0||c>255)return 0;
    if(d<0||d>255)return 0;
    if(e<0||e>65535)return 0;
    char t[10005];
    sprintf(t,"%d.%d.%d.%d:%d",a,b,c,d,e);
    for(int i=0;i<strlen(s);i++)
    {
        if(s[i]!=t[i])return 0;
    }
    return 1;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        char op[10005],ad[10005];
        cin>>op>>ad;
        string g;
        for(int i=0;i<strlen(ad);i++)
        {
            g+=ad[i];
        }
        if(op[0]=='S')
        {
            if(dz(ad)==0)
            {
                cout<<"ERR\n";
            }
            else if(m.count(g)!=0)
            {
                cout<<"FAIL\n";
            }
            else
            {
                cout<<"OK\n";
                m[g]=i;
            }
        }
        else
        {
            if(dz(ad)==0)
            {
                cout<<"ERR\n";
            }
            else if(m.count(g)==0)
            {
                cout<<"FAIL\n";
            }
            else
            {
                cout<<m[ad]<<endl;;
            }
        }
    }
    return 0;
}

T4

这道题纯暴力能过70分,暴力很简单,但要拿全分要用链表,但我用的是结构体和队列.

献上70分暴力代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int x=0,y=0;//苹果、桔子的数组和 编号 
}a[200005];
int n,ans,vis[200005];
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].x;
        a[i].y=i;
    }
    int last=-1;
    while(ans!=n)
    {
        for(int i=1;i<=n;i++)
        {
                if(a[i].x!=last&&vis[a[i].y]==0)
                {
                    cout<<a[i].y<<' ';
                    ans++;
                    vis[a[i].y]=1;
                    last=a[i].x;
                }
        }
        cout<<endl; 
        last=-1;
    }

    return 0;
}

然后是AC代码

#include<bits/stdc++.h>
using namespace std;
struct node
{
    int l,r,pos;//左指针、右指针和值 
};
queue<node>q,temp;
bool vis[200005];
int n;
int main()
{
    cin>>n;
    int l,r,last;
    l=r=last=-1;
    for(int i=1;i<=n;i++)
    {
        int x;
        cin>>x;
        if(x!=last)
        {
            if(last!=-1)q.push((node){l,r,last});//存入队列 
            l=i,r=i;
        }
        else r++;
        last=x;
    }
    q.push((node){l,r,last});

    while(!q.empty())
    {
        int last=-1;
        int l,r;
        while(!q.empty())
        {
            node now=q.front();
            q.pop();
            if(now.pos!=last)
            {
                if(last!=-1&&l<=r) temp.push((node){l,r,last});
                while(vis[now.l]) now.l++;
                vis[now.l]=true;//标记 
                cout<<now.l<<' ';
                now.l++;
                l=now.l,r=now.r;//更新 
            }
            else r=now.r;
            last=now.pos;
        }
        cout<<endl;
        if(l<=r) temp.push((node){l,r,last});//总有一组要单独入队 
        while(!temp.empty())
        {
            node add=temp.front();
            temp.pop();
            q.push(add);
        }
    }

    return 0;
}