CSP 25th 202203-2 Travel Plan C/C++ Full Score Answers

CSP 25th 202203-2 Travel Plan C/C++ Full Score Answers

Click me for the collection of answers to CSP real questions over the years (link)

Below is the 100-point code, difference array
insert image description here

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

int can[200010]={
    
    0};//某时间做核酸能满足的出行计划数量
int ans[200010]={
    
    0};
int chaxun[100010]={
    
    0};
int main()
{
    
    
    int n,m,k,i,j;
    cin>>n>>m>>k;
    for(i=0;i<n;i++)
    {
    
    
        int clock,need;
        scanf("%d %d",&clock,&need);
        if(clock-k>0)
        {
    
    
            int l=clock-k-need+1;
            int r=clock-k+1;
            can[max(l,0)]++;
            can[r]--;
        }
    }
    ans[0] = can[0];
    for(i=1;i<=200010;i++)
        ans[i] = ans[i-1] + can[i];
    for(i=0;i<m;i++)
        scanf("%d",&chaxun[i]);
    for(i=0;i<m;i++)
        printf("%d\n",ans[chaxun[i]]);

    return 0;
}

Below is the 100-point code, an array of ordinary records
insert image description here

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

int can[200010]={
    
    0};//某时间做核酸能满足的出行计划数量
int chaxun[200010]={
    
    0};
int main()
{
    
    
    int n,m,k,i,j;
    cin>>n>>m>>k;
    for(i=0;i<n;i++)
    {
    
    
        int clock,need;
        scanf("%d %d",&clock,&need);
        if(clock-k>0)
            for(j=clock-k;j>=0 && j>clock-k-need;j--)
                can[j]++;
    }
    for(i=0;i<m;i++)
        scanf("%d",&chaxun[i]);
    for(i=0;i<m;i++)
        printf("%d\n",can[chaxun[i]]);

    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_47964723/article/details/126986194