10.16-甜菜测试-410(李俊辰)

· · 个人记录

T1:B4550 [语言月赛202606]贴纸整理

错误原因:

vis数组开小了,是用来计算a数组中有没有重复的

错误代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1000+5;
int n,m,q,a[N][N],vis[N],cnt;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    while(q--){
        int op;
        cin>>op;
        if(op==1){
            int x,y,h,w,f=0;
            cin>>x>>y>>h>>w;
            for(int i=x;i<=x+h-1;i++){
                for(int j=y;j<=y+w-1;j++){
                    if(a[i][j]!=0){
                        f=1;
                        break;
                    }
                }
            }
            if(x+h-1>n || x+h-1<1 || y+w-1>m || y+w-1<1 || f==1){
                cout<<"Error"<<endl;
                continue;
            }
            cnt++;
            for(int i=x;i<=x+h-1;i++){
                for(int j=y;j<=y+w-1;j++){
                    a[i][j]=cnt;
                }
            }
            cout<<cnt<<endl;
        }
        else if(op==2){
            int id,f=0;
            cin>>id;
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(a[i][j]==id){
                        f=1;
                        a[i][j]=0;
                    }
                } 
            }
            if(f==1){
                cout<<"Remove"<<endl;
            }
            else{
                cout<<"Error"<<endl;
            }
        }
        else if(op==3){
            int x,y;
            cin>>x>>y;
            cout<<a[x][y]<<endl;
        }
    }
    int zs=0,sum=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[a[i][j]]==0 && a[i][j]!=0){
                zs++;
                vis[a[i][j]]=1;
            }
            if(a[i][j]!=0){
                sum++;
            }
        }
    }
    cout<<zs<<" "<<sum;
    return 0;
}

思路:

由于这道题目的数据过小,所以可以直接暴力。

判断每种可能的情况,最后输出剩余的贴纸种数和被覆盖过得贴纸数量即可

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1000+5;
int n,m,q,a[N][N],vis[N],cnt;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m>>q;
    while(q--){
        int op;
        cin>>op;
        if(op==1){  // 可能一 
            int x,y,h,w,f=0;
            cin>>x>>y>>h>>w;
            // 判断再次贴上的贴纸会不会重复 
            for(int i=x;i<=x+h-1;i++){
                for(int j=y;j<=y+w-1;j++){
                    if(a[i][j]!=0){
                        f=1;
                        break;
                    }
                }
            }
            // 如果出界并会重复 
            if(x+h-1>n || x+h-1<1 || y+w-1>m || y+w-1<1 || f==1){
                cout<<"Error"<<endl;
                continue;
            }
            // 给每个贴纸贴到的地方打上编号 
            cnt++;
            for(int i=x;i<=x+h-1;i++){
                for(int j=y;j<=y+w-1;j++){
                    a[i][j]=cnt;
                }
            }
            cout<<cnt<<endl;
        }
        else if(op==2){  // 可能二 
            int id,f=0;
            cin>>id;
            // 给这个贴纸所有贴上的地方清零 
            for(int i=1;i<=n;i++){
                for(int j=1;j<=m;j++){
                    if(a[i][j]==id){
                        f=1;
                        a[i][j]=0;
                    }
                } 
            }
            // 判断这个编号为id的贴纸以前是否出现过 
            if(f==1){
                cout<<"Remove"<<endl;
            } 
            else{
                cout<<"Error"<<endl;
            }
        }
        else if(op==3){  // 可能三 
            int x,y;
            cin>>x>>y;
            cout<<a[x][y]<<endl;  // 输出盖住该格子的贴纸编号
        }
    }
    int zs=0,sum=0;  // zs为最后剩余的贴纸种数,sum为有几个格子被贴上了贴纸 
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            if(vis[a[i][j]]==0 && a[i][j]!=0){  // 如果zs没有加过这种编号的贴纸并且不是没动过的0 
                zs++;
                vis[a[i][j]]=1;  // 标记 
            }
            if(a[i][j]!=0){  // 如果不是没动过的0 
                sum++;
            }
        }
    }
    cout<<zs<<" "<<sum;
    return 0;
}

T2:P17538 音符方阵

错误思路:

只需要在第一行和第一列上都输出1,其他的都输出2。

错误代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=2000+5;
int t;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            cout<<1<<" ";
        }
        cout<<endl;
        for(int i=2;i<=n;i++){
            cout<<1<<" ";
            for(int j=2;j<=n;j++){
                cout<<2<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

正确思路:

这道题目是一道找规律的题

首先就是把从左上角到右下角呈一个依次加一的数列,然后一个嵌套循环,如果j是i的倍数,就a[i][j]和a[j][i]都变成i,否则变成1,,最后输出

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=2000+5;
int t,a[N][N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                a[i][j]=0;
            }
        }
        // 开始构造矩阵,只要遍历上半部分,因为矩阵是对称的
        for(int i=1;i<=n;i++){
            for(int j=i;j<=n;j++){
                // 如果列号 j 是行号 i 的倍数
                if(j%i==0){ 
                    // 所以a[i][j]和a[j][i]都赋值为i,由于是a[i][j]和a[j][i],因为这个矩阵是对称的 
                    a[i][j]=a[j][i]=i;
                }
                else{
                    // 若否倍数关系,默认为1 
                    a[i][j]=a[j][i]=1;
                }
            }
        }
        // 最后输出 
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout<<a[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    return 0;
}

T3:P16684 圆圈

错误思路:

就骗了点分

错误代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1000+5;
int n,m,a[N][N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    while(n--){
        int x,y,r;
        cin>>x>>y>>r;
        a[x][y]++;
    }
    cin>>m;
    while(m--){
        int x,y;
        cin>>x>>y;
        if(a[x][y]%2==1){
            cout<<"Yes"<<endl;
        }
        else{
            cout<<"No"<<endl;
        }
    } 
    return 0;
}

正确思路:

由于时间限时2秒(足足两秒!),所以可以直接暴力

对于每一个圆,可以直接枚举所有点,如果圆心到这个点之间的距离小于半径,就代表这个点在这个圆里

最后判断如果这个点有颜色就输出 Yes ,没颜色输出 No

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1000+5;
int n,m,cnt[N][N];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        int x,y,r;
        cin>>x>>y>>r;
        for(int xx=1;xx<=1000;xx++){  // 这两个范围开到1000,是因为这道题的最大可能就是1000 
            for(int yy=1;yy<=1000;yy++){
                // 判断点 (xx, yy) 是否在当前圆内或边界上
                // 公式: (x-xx)*(x-xx)+(y-yy)*(y-yy)<=r*r
                if((x-xx)*(x-xx)+(y-yy)*(y-yy)<=r*r){
                    // 如果在圆内,则该点的覆盖层数加1
                    cnt[xx][yy]++;
                }
            }
        }
    }
    cin>>m;
    while(m--){
        int x,y;
        cin>>x>>y;
        // 如果这个点有奇数的圆圈包含 
        if(cnt[x][y]%2==1){
            cout<<"Yes"<<endl;
        }
        // 如果这个点有偶数的圆圈包含 
        else{
            cout<<"No"<<endl;
        }
    }
    return 0;
} 

T4:P16683 盆栽

错误思路:

就骗了点分

错误代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=1000+5;
int n,m;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    if(n==5 && m==2){
        cout<<11<<" "<<19<<endl<<7<<" "<<27;
    }
    else{
        cout<<0;
    }
    return 0;
}

正确思路:

每一次操作,区间里的每一个数都会变成 a_i异或k ,收获的美丽值也就是 a_i异或k-a_i ,并设这个数为 b_i 。经过观察发现这就是求 b数组 的最小字段和和最大字段和。然后再用 dp 求解。

先算出如果不浇水时的总分,然后看看浇了魔法水后,哪一段连续的花能让总分变得最多或最少,最后把原来的总分加上这个变化的量即可。

正确代码:

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N=5000+5;
int n,m,a[N],b[N],dp[N],dp1[N],ans;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        ans+=a[i];  // 计算美丽度综合
    }
    while(m--){
        int k;
        cin>>k;
        // 取最大值和取最小值时 b[i]>0 表示异或后变美了,b[i]<0 表示变丑了
        for(int i=1;i<=n;i++){
            dp[i]=dp1[i]=0;  // 清零 
            b[i]=(a[i]^k)-a[i];  // 
        }
        int maxx=INT_MIN,minn=INT_MAX;
        for(int i=1;i<=n;i++){
            // 要么只选当前这个 b[i],要么接上前面的最大子段 dp[i-1]
            dp[i]=max(b[i],dp[i-1]+b[i]);
            // dp[i]: 要么只选当前这个 b[i],要么接上前面的最大子段 dp1[i-1]
            dp1[i]=min(b[i],dp1[i-1]+b[i]);
            // 更新最大值和最小值 
            maxx=max(maxx,dp[i]);
            minn=min(minn,dp1[i]);
        }
        cout<<ans+minn<<" "<<ans+maxx<<endl;  // 最后输出答案 
    }
    return 0;
}