Pass-Muraille(贪心+模拟)

传送门

题解:这是个贪心题,一列一列找,找到不符合的那一列找出这一列中对应右边最长的哪一行即可。

#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;

const int maxn=150;

int mapp[maxn][maxn];

int main()
{
    int t;
    scanf("%d",&t);
    while(t--){
        memset(mapp,0,sizeof(mapp));
        int n,k;
        scanf("%d%d",&n,&k);
        int y1,x1,y2,x2,maxx=-1,maxy=-1;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d%d",&y1,&x1,&y2,&x2);
            maxx=max(maxx,x2);
            maxy=max(maxy,max(y1,y2));
            if(y1<y2){
                for(int j=y1;j<=y2;j++){
                    mapp[x1][j]=i;
                }
            }else{
                for(int j=y2;j<=y1;j++){
                    mapp[x1][j]=i;
                }
            }
        }
//        for(int i=0;i<=maxx;i++){
//            for(int j=0;j<=maxy;j++){
//                printf("%d ",mapp[i][j]);
//            }
//            printf("\n");
//        }
//        cout<<endl;
        int temp1,ans=0;
        for(int i=0;i<=maxy;i++){
            temp1=0;
            for(int j=0;j<=maxx;j++){
                if(mapp[j][i]){
                   temp1++;
                }
            }
            //cout<<temp1<<endl;
            int temp2=temp1-k;
            if(temp2>0){
                ans+=temp2;
                while(temp2--){
                    int tempcnt=0,tempr,tempmax=-1;
                    for(int k=0;k<=maxx;k++){
                        tempcnt=0;
                        if(mapp[k][i]>0){
                            for(int l=i+1;l<=maxy;l++){//此处l=i+1有个坑
                                if(mapp[k][l]==mapp[k][i]){
                                    tempcnt++;
                                }else{
                                    break;
                                }
                            }
                        }
                        if(tempcnt>tempmax){
                            tempmax=tempcnt;
                            tempr=k;
                        }
                    }
                    //cout<<tempr<<endl;
                    for(int m=i;m<=i+tempmax;m++){
                        mapp[tempr][m]=0;
                    }
                }
            }
        }
//        for(int i=0;i<=maxx;i++){
//            for(int j=0;j<=maxy;j++){
//                cout<<mapp[i][j]<<" ";
//            }
//            cout<<endl;
//        }
//        cout<<endl;
        printf("%d\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值