题意:一个n*n的房子,有很多灯,每个格子只能被上下方向照一次、左右方向照一次,每个灯可以选择上下或是左右照,照明长度以自身位置为中心,占用2*r+1个格子。问能否安排一种方案,使所有格子满足条件。

析:典型的Two-Sat,对于行来说,如果两个能够交叉,那么他们不能都是左右,对于列也是一样。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 50;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} struct TwoSat{
int n;
vector<int> G[maxn<<1];
bool mark[maxn<<1];
int S[maxn<<1], c; bool dfs(int x){
if(mark[x^1]) return false;
if(mark[x]) return true;
mark[x] = true;
S[c++] = x;
for(int i = 0; i < G[x].sz; ++i)
if(!dfs(G[x][i])) return false;
return true;
} void add_clause(int x, int xval, int y, int yval){
x = x * 2 + xval;
y = y * 2 + yval;
G[x^1].pb(y);
G[y^1].pb(x);
} bool solve(){
for(int i = 0; i < n*2; i += 2){
if(!mark[i] && !mark[i+1]){
c = 0;
if(!dfs(i)){
while(c > 0) mark[S[--c]] = false;
if(!dfs(i+1)) return false;
}
}
}
return true;
}
};
map<P, int> mp;
vector<int> row[1005], col[1005];
TwoSat twosat; int main(){
int r, l;
scanf("%d %d %d", &n, &r, &l);
int cnt = 0;
for(int i = 0; i < l; ++i){
int x, y;
scanf("%d %d", &x, &y);
--x; --y;
mp[P(x, y)] = cnt++;
row[x].pb(y);
col[y].pb(x);
}
for(int i = 0; i < n; ++i){
sort(row[i].begin(), row[i].end());
sort(col[i].begin(), col[i].end());
}
twosat.n = cnt;
for(int i = 0; i < n; ++i){
for(int j = 0; j < row[i].sz; ++j){
int k = j + 1;
while(k < row[i].sz && row[i][j] + r >= row[i][k] - r){
twosat.add_clause(mp[P(i, row[i][j])], 0, mp[P(i, row[i][k])], 0);
++k;
}
}
for(int j = 0; j < col[i].sz; ++j){
int k = j + 1;
while(k < col[i].sz && col[i][j] + r >= col[i][k] - r){
twosat.add_clause(mp[P(col[i][j], i)], 1, mp[P(col[i][k], i)], 1);
++k;
}
}
} puts(twosat.solve() ? "YES" : "NO");
return 0;
}

  

Gym 101201F Illumination (Two-Sat)的更多相关文章

  1. 【2-sat】Gym - 101201F - Illumination

    题意:平面上l盏灯,每盏灯可以照亮横向的2*r+1个格子或者纵向的2*r+1个格子,让你确定每盏灯的方向,使得每个格子只被同一行的不超过一盏灯照亮,并且只被同一列的不超过一盏灯照亮.输出是否有解. 显 ...

  2. codeforces gym #101987K -TV ShowGame(2-SAT)

    题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...

  3. ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力

     Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS     Memory Limit:65536KB     64bit IO Fo ...

  4. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  5. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  6. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  7. 多边形碰撞 -- SAT方法

    检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...

  8. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  9. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

随机推荐

  1. 打造基于jQuery的日期选择控件

    终于把jQuery拼写正确了哈,哈哈javascript也是区分大小写的,所以确实不能写错,今天我来和大家分享的是日期选择控件的实现,功能也许不够强大,但是能够满足需求. 我之前也写过(正确的说是改过 ...

  2. C#/.NET主线程与子线程之间的关系

    以前一直没有在程序中写过总结,再翻开程序时却不知所云,所以我决定写总结        一般 一个应用程序就对应一个进程,一个进程可有一个或多个线程,而一般有一个主线程.       有的博客上说“至少 ...

  3. FastAdmin 的 captcha 是如何来的?

    FastAdmin 的 captcha 是如何来的? 如何打开 FastAdmin 的验证吗? 文件位置 C:\www\fastadmin\application\config.php 改 login ...

  4. 黄聪:WordPress 多站点建站教程(四):获取子站点相关信息(站点的注册时间,修改时间,总文章数,URL等)

    1.获取子站点blogs表里面的内容信息 $blog_details = get_blog_details(1); echo 'Blog '.$blog_details->blog_id.' i ...

  5. mysql 使用inet_aton和inet_ntoa处理ip地址数据

    mysql 使用inet_aton和inet_ntoa处理ip地址数据 mysql提供了两个方法来处理ip地址 inet_aton 把ip转为无符号整型(4-8位) inet_ntoa 把整型的ip转 ...

  6. ROS注册级别LEVEL0-6,原来使用GRE通道是不要钱滴

    GRE通道是没有个数限制的.如果只做一个分公司的PPTP,L2TP,等等,也是不用钱滴. 跑OSPF就不行了,必须要给钱.

  7. pythonNet day07

    信号 一个进程向另外一个进程通过信号的方式传递某种讯息,接收方在接收到信号后作出相应的处理 查看信号:kill -l kill -signum PID 给PID的进程发送一个信号 信号名称:信号的名字 ...

  8. Oracle数据库物理结构

    Oracle数据库物理结构 oracle的数据,实际上是以文件的形式来保存的,文件中出了保存用户的数据之外,还需要保存管理数据和日志数据等等.作为一个DBA,必须需要知道自己的数据分别保存在什么位置上 ...

  9. php 文件缓存类

    //文件缓存类 class FileCache { private $cacheTime = 3600; //默认缓存时间 秒 private $cacheDir = './filecache'; / ...

  10. IO在block级别的过程分析

    btt User Guide在百度找了3天没找到,bing也不行,结果google第一页第5个结果就是. 可恶的GFW http://www.fis.unipr.it/doc/blktrace-1.0 ...