【洛谷八连测R5】whzzt-Conscience

 

题目描述

题目难度不一定按照题目顺序递增

在 n * m 的网格上有一些点。在第 i 行第 j 列放置摄像头可以观察到所有满足 x=i,y≥j 或 x≥i,y=j 的点。求最少需要放多少个摄像头才能观察到所有给定的点,注意摄像头必须放在点上,且要保证任意一个摄像头均不能观察到另一个摄像头。保证输入中的点不会重复。

输入输出格式

输入格式:

第一行包含一个整数 T ,表示数据组数。每组数据的格式如下:
第一行包含三个正整数 n,m,q,表示网格的大小和给定点的个数。
接下来 q 行,每行两个整数 xi,yi,表示第 i 个点的坐标。

输出格式:

对每组数据输出一行,表示最少放置摄像头的点数,如果无解输出-1。

说明

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=100000+5;
inline void read(int &x){
    x=0; char ch=getchar();
    while(ch<'0'||ch>'9') ch=getchar();
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
}
int T,n,m,q,ans;
bool c1[maxn],c2[maxn];
struct node{
    int x,y;
    inline bool operator < (const node &j) const {
        return x==j.x? y<j.y:x<j.x;
    }
}e[maxn];
int main(){
    read(T);
    while(T--){
        read(n);read(m);read(q); ans=0;
        for(int i=1;i<=q;++i)
            read(e[i].x),read(e[i].y);
        sort(e+1,e+q+1);
        memset(c1,0,sizeof(c1));
        memset(c2,0,sizeof(c2));
        for(int i=1;i<=q;++i){
            if(c1[e[i].x]||c2[e[i].y]) continue;
            ans++; c1[e[i].x]=c2[e[i].y]=1;
        }
        printf("%d
",ans);
    }
    return 0;
}
欢迎转载,转载请注明出处!
原文地址:https://www.cnblogs.com/huihao/p/7748341.html