题意:一个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. 【转】MFC消息处理(一)

    原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182120 1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生 ...

  2. MYSQL中写SQL语句,取到表中按ID降序排列(最新纪录排在第一行)

    'select * from bugdata where id>0 order by id desc'

  3. 2.运行成功的Demo(Python+Appium)

    1.打开Appium运行 2.在Pycharm输入代码如下所示: from appium import webdriver desired_caps = {} #初始化 desired_caps['p ...

  4. Python download a image (or a file)

    http://stackoverflow.com/questions/13137817/how-to-download-image-using-requests import shutil impor ...

  5. 黄聪:V2010中C#实现友好的等待任务完成时,出现的多线程悬浮窗体

    实现效果如下: 项目已经打包后,大家直接下载吧:[HCWaitForm.rar]

  6. 嵌入式linux开发:杂七杂八的话

    1. 编译器:常用的有Sourcery,linaro等.当然芯片厂家一般也会提供可用的编译器. 2. 将配置和编译时的输出O到其他位置:make O=some_dir 这样做的好处是保持源代码树的干净 ...

  7. Bootstrap-CSS

    ylbtech-Bootstrap-Plugin: 1.返回顶部 1.   2. 2.返回顶部   3.返回顶部   4.返回顶部   5.返回顶部 1.   2.   11.返回顶部   作者:yl ...

  8. nohup 后台运行命令

    在Linux上部署zipkin,在SSH客户端执行java -jar zipkin-server-1.21.0-exec.jar,启动成功,在关闭SSH客户端后,运行的程序也同时终止了,怎样才能保证在 ...

  9. RabbitMQ 信道(channel)挂掉,但连接仍然存在,同时出现错误:Received remote Channel.Close (406): PRECONDITION_FAILED - unknown delivery tag x 的问题

    该问题经过一番试验,发现是消费者(consumer)程序逻辑错误导致:在消息处理的回调函数中多次ack或nack. 开启Python日志,并在回调函数中两次ack得到如下信息: F:\software ...

  10. 「小程序JAVA实战」小程序开源搜索组件(53)

    转自:https://idig8.com/2018/09/22/xiaochengxujavashizhanxiaochengxukaiyuansousuozujian52/ 上次说了可以在视频中通过 ...