CodeChef Protecting The Poison【贪心】

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
阅读下面材料,在空格处填入适当的内容(1个单词)或使用括号中单词的正确形式。 If including inter-state sections, those not protecting the northern border of China, the oldest _____56____(exist) section of the Great Wall of China was the Qi State "Great Wall". It stretches for over 500 kilometers (300 miles) from the Yellow River at Jinan eastwards to the East China Sea, almost ___57____(divide) Shandong Province in half. The "Great Wall" of the Qi State was ___58____(initial) built around 650 BC, and expended during the Warring States Period (475–221 BC). Before the Qi State Wall was built, natural barriers, i.e. rivers and mountain ranges, formed ____59_____ only defensible boundaries between territories as barriers ___60____ enemies. The State of Qi built its Great-Wall-esque military barrier along its southern border to prevent attacks from the State of Lu and the State of Chu. However, rapid development and ____61____(construct) have brought many new problems and challenges in protecting the wall. ____62____ is necessary to provide a solid legal guarantee for its conservation. To tackle the challenges, Shandong Province has passed a regulation protecting the structure____63____will take effect on Jan. 1. This year, Shandong has added 860 patrol posts in seven cities along the Qi wall, mainly recruiting farmers living nearby. Guo Jialian has been patrolling the section of the wall in Guangli village for three months. "I need to check ____64______ there is any damage to the wall that is caused by people digging earth from it. Awareness of protecting the Qi wall _____65_____ (enhance) in recent years. We all know the wall is a cultural relic," says Guo, adding that he frequently sees tourists coming to visit the ancient structure.
最新发布
02-06

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值