CodeChef Protecting The Poison【贪心】

来源:互联网 发布:威海市南海新区知乎 编辑:程序博客网 时间:2024/06/08 19:44

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 100006using 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;}