CSP Certification - third training (2019.9.6)

D title train tickets

Here Insert Picture Description
Here Insert Picture Description

First to a wrong demonstration, 75 points Code:

#include <bits/stdc++.h>
using namespace std;
int n,cnt,tmps,rest[110],vis[110];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=20;i++)
        rest[i]=5;//每排剩余5张票
    for(int i=1;i<=n;i++)
    {
        cin>>cnt;
        for(int j=1;j<=20;j++)//第j排
        {
            if(rest[j]>=cnt)//第j排剩余的票数大于等于cnt,则可以让cnt个座位相邻
            {
                tmps=0;//记录出票数
                for(int k=(j-1)*5+1;k<=j*5;k++)//遍历第j排座位,k为座位编号
                {
                    if(vis[k]==0)//第k个座位还未被购买
                    {
                        vis[k]=1;
                        tmps++;
                        if(tmps<cnt)
                            printf("%d ",k);
                        else if(tmps==cnt)//已经出了cnt张票了,结束
                        {printf("%d\n",k);rest[j]=rest[j]-cnt;break;}
                    }
                }
                break;
            }
        }
    }
    return 0;
}

Why only 75 points it?

Because not consider this situation: the current is greater than the number of votes to be purchased for each row of the remaining votes, this does not guarantee a ticket to the same row of adjacent seats. For example, each row are now only three seats, and to a purchase of 5 tickets, then only the first 2 tickets discharge, the second discharge three tickets, five tickets are not adjacent .

100 AC Code:

#include <bits/stdc++.h>
using namespace std;
int n,cnt,tmps,flag,rest[110],vis[110];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=20;i++)
        rest[i]=5;//每排剩余5张票
    for(int i=1;i<=n;i++)
    {
        cin>>cnt;
        flag=0;
        for(int j=1;j<=20;j++)//第j排
        {
            if(rest[j]>=cnt)//第j排剩余的票数大于等于cnt,则可以让cnt个座位相邻
            {
                flag=1;//标记:可以做到cnt个座位相邻
                tmps=0;//记录出票数
                for(int k=(j-1)*5+1;k<=j*5;k++)//遍历第j排座位,k为座位编号
                {
                    if(vis[k]==0)//第k个座位还未被购买
                    {
                        vis[k]=1;
                        tmps++;
                        if(tmps<cnt)
                            printf("%d ",k);
                        else if(tmps==cnt)//已经出了cnt张票了,结束
                        {printf("%d\n",k);rest[j]=rest[j]-cnt;break;}
                    }
                }
                break;
            }
        }
        if(flag==0)//无法做到cnt个座位相邻
        {
            tmps=0;
            for(int i=1;i<=100;i++)
            {
                if(vis[i]==0)
                {
                    vis[i]=1;
                    tmps++;
                    rest[(i-1)/5+1]--;
                    if(tmps<cnt)
                        printf("%d ",i);
                    else if(tmps==cnt)
                    {printf("%d\n",i);break;}
                }
            }
        }
    }
    return 0;
}

Public key box B title

Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
This problem honest, a little more difficult ah ...

Looked at the problem solution online written by others, it is to write a multi-dimensional structure of the sort. My idea is to traverse all the time, if we have time to traverse the back or removed according to the subject requirements for operation. Careful thought for a moment, the time complexity is O (n * n * s), you can limit the data to 1e10, and even gave me the AC? Well, I only thought of violence simulation ...

If you want to see the sort of multidimensional problem-solving, you can take a look at this article: https://blog.csdn.net/L_xqqzldh/article/details/87938066

False 100 AC Code:

#include <bits/stdc++.h>
using namespace std;
bool vis[1010],is_out[10110],is_in[10110];
vector<int>out[10110],in[10110];
int n,m,mx,t1,t2,d,num,a[1010];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        a[i]=i;
    for(int i=1;i<=m;i++)
    {
        cin>>num>>t1>>d;
        t2=t1+d;
        out[t1].push_back(num);//保存t1时刻取出钥匙的编号(可能取出多个)
        in[t2].push_back(num);//保存t2时刻放回钥匙的编号(可能放回多个)
        is_out[t1]=1;//t1时刻有取出
        is_in[t2]=1;//t2时刻有放回
        mx=max(mx,t2);//mx为最大时刻
    }
    memset(vis,1,sizeof(vis));//开始时所有挂钩均被占用
    for(int i=1;i<=mx;i++)//遍历所有时刻
    {
        if(is_in[i]==1)//i时刻有放回(先全部放回再取出)
        {
            sort(in[i].begin(),in[i].end());//放回时要按从小到大放回
            for(int j=0;j<in[i].size();j++)
            {
                for(int k=1;k<=n;k++)
                {
                    if(vis[k]==0)//第k个挂钩没被占用
                    {
                        a[k]=in[i][j];//in[i][j]表示i时刻放回的第j把钥匙的编号
                        vis[k]=1;
                        break;
                    }
                }
            }
        }
        if(is_out[i]==1)//i时刻有取出
        {
            for(int j=0;j<out[i].size();j++)
            {
                for(int k=1;k<=n;k++)
                {
                    if(a[k]==out[i][j])//out[i][j]表示i时刻取出的第j把钥匙的编号,找到了编号为out[i][j]的钥匙,将其取出
                    {vis[k]=0;break;}
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
        i==n?printf("%d\n",a[i]):printf("%d ",a[i]);
    return 0;
}

A problem soy sauce

Here Insert Picture Description
Here Insert Picture Description
100 AC Code:

#include <bits/stdc++.h>
using namespace std;
int n,s,t1,t2,t3,ans;
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    s=n/10;
    t1=s/5;
    t2=(s-t1*5)/3;
    t3=s-t1*5-t2*3;
    ans=7*t1+4*t2+t3;
    printf("%d\n",ans);
    return 0;
}

Problem C maximum fluctuation

Here Insert Picture Description
100 AC Code:

#include <bits/stdc++.h>
using namespace std;
int n,mx,a[1010];
int main()
{
    ios::sync_with_stdio(false);
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    mx=0;
    for(int i=2;i<=n;i++)
        mx=max(mx,abs(a[i]-a[i-1]));
    printf("%d\n",mx);
    return 0;
}
Published 72 original articles · won praise 91 · views 30000 +

Guess you like

Origin blog.csdn.net/ljw_study_in_CSDN/article/details/100592066