CodeChef Protecting The Poison【贪心】

在一个N×N的网格中,中央K×K区域存放着珍贵的毒药,蛇们需要阻止从外部射入的箭偷走毒药。通过将蛇的位置投影到水平和垂直方向,并采用贪心算法来确定最少数量的蛇可以保证毒药的安全。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

The kingdom of the snakes is an N**x**N grid. Their most-valued possession is a huge collection of poison, which is stored in the central K**x**K grid. It is guaranteed that both N and K are odd. What we mean by ‘central’ is this: suppose in the N**x**N grid, (i, j) refers to the cell in the i-th row and j-th column and (1,1) refers to the top-left corner and (N,N) refers to the bottom-right corner. Then the poison is stored in the K**x**K square whose top-left corner is ( (N - K)/2 + 1, (N - K)/2 + 1 ).

But there are thieves who want to steal the poison. They cannot enter the N**x**N grid, but they can shoot arrows from outside. These arrows travel across a row (from left to right, or right to left), or across a column (top to bottom or bottom to top) in a straight line. If the arrow enters the K**x**K grid, some of the poison will stick to the arrow, and if it exits the N**x**N grid after that, the thieves will have successfully stolen some of the poison.

As the King of the snakes, you want to thwart these attempts. You know that the arrows will break and stop if they hit a snake’s scaly skin, but won’t hurt the snakes. There are some snakes already guarding the poison. Each snake occupies some consecutive cells in a straight line inside the N**x**N grid. That is, they are either part of a row, or part of a column. Note that there can be intersections between the snakes. A configuration of snakes is ‘safe’, if the thieves cannot steal poison. That is, no matter which row or column they shoot arrows from, either the arrow should hit a snake and stop (this can happen even after it has entered and exited the K**x**K grid), or it shouldn’t ever enter the K**x**K grid.

The King has other duties for the snakes, and hence wants to remove as many snakes as possible from this configuration, such that the remaining configuration is still ‘safe’. Help him find the minimum number of snakes he needs to leave behind to protect the poison.

题解

把蛇投影到水平面和竖直面上,然后直接贪心就可以了。

代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 100006
using namespace std;
inline char nc(){
    static char buf[100000],*i=buf,*j=buf;
    return i==j&&(j=(i=buf)+fread(buf,1,100000,stdin),i==j)?EOF:*i++;
}
inline int _read(){
    char ch=nc();int sum=0,p=1;
    while(ch=='-'&&(!(ch>='0'&&ch<='9')))ch=nc();
    if(ch=='-')p=-1,ch=nc();
    while(ch>='0'&&ch<='9')sum=sum*10+ch-48,ch=nc();
    return sum*p;
}
int tet,S,T,ans,n,K,m;
struct data{
    int l,r;
    bool operator <(const data&c)const{return l<c.l||(l==c.l&&r>c.r);}
}a[maxn],b[maxn];
int main(){
    freopen("snakes.in","r",stdin);
    freopen("snakes.out","w",stdout);
    tet=_read();
    while(tet--){
        n=_read();K=_read();m=_read();
        for(int i=1;i<=m;i++){
            data x,y;
            x.l=_read(),y.l=_read(),x.r=_read(),y.r=_read();
            if(x.l>x.r)swap(x.l,x.r);if(y.l>y.r)swap(y.l,y.r);
            a[i]=y;b[i]=x;
        }
        S=n-K+2>>1;T=n-S+1;
        sort(a+1,a+1+m);sort(b+1,b+1+m);
        int p=0;
        for(int i=1;i<=m;i++) if(a[i].l<=S&&a[i].r>a[p].r)p=i;
        int lst=a[p].r;
        int i=p+1;ans=1;
        while(i<=m){
            if(lst>=T)break;
            int j=i,Max=0;
            while(j<=m&&a[j].l<=lst+1)Max=max(Max,a[j++].r);
            if(Max>=lst+1)lst=Max,ans++;else break;
            i=j;
        }
        if(lst<T){
            printf("-1\n");
            continue;
        }
        p=0;
        for(i=1;i<=m;i++) if(b[i].l<=S&&b[i].r>b[p].r)p=i;
        lst=b[p].r;
        i=p+1;ans++;
        while(i<=m){
            if(lst>=T)break;
            int j=i,Max=0;
            while(j<=m&&b[j].l<=lst+1)Max=max(Max,b[j++].r);
            if(Max>=lst+1)lst=Max,ans++;else break;
            i=j;
        }
        if(lst<T){
            printf("-1\n");
            continue;
        }
        printf("%d\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值