Gym 101201F Illumination (Two-Sat)
题意:一个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)的更多相关文章
- 【2-sat】Gym - 101201F - Illumination
题意:平面上l盏灯,每盏灯可以照亮横向的2*r+1个格子或者纵向的2*r+1个格子,让你确定每盏灯的方向,使得每个格子只被同一行的不超过一盏灯照亮,并且只被同一列的不超过一盏灯照亮.输出是否有解. 显 ...
- codeforces gym #101987K -TV ShowGame(2-SAT)
题目链接: https://codeforces.com/gym/101987 题意: 有长度为$n$的只包含$B,R$的字符串 有m种关系,每个关系说出三个位置的确切字符 这三个位置的字符最多有一个 ...
- 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 ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- 多边形碰撞 -- SAT方法
检测凸多边形碰撞的一种简单的方法是SAT(Separating Axis Theorem),即分离轴定理. 原理:将多边形投影到一条向量上,看这两个多边形的投影是否重叠.如果不重叠,则认为这两个多边形 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
随机推荐
- 解决Mybatis没有代码提示
MyBatis xml文件中代码自动提示 工具/原料 eclipse,maven 方法/步骤 1 一.获得mybatis-3-config.dtd.mybatis-3-mapper.dtd 这 ...
- Javascript 的数据是什么数据类型?
Javascript 中的数据都是以 64 位浮点 float 存储的. 所有语言对浮点的精度是很难确定的. 如下代码可以实验到问题. <script> var a = 0.4; var ...
- spark on es 多索引查询
核心接口 trait SparkOnEsService { val conf = new SparkConf // conf.setMaster("local[10]") val ...
- 解决 eclipse tomcat cannot create a server using the selected type
解决的方法 1.退出eclipse: 2.打开 [工程目录下]/.metadata/.plugins/org.eclipse.core.runtime/.settings目录: 3.删除org.ecl ...
- php的静态变量的实现
1.静态变量的结构 php脚本编译之后会生成执行opcode组成的opcode_array,执行每个zend_op_array都会生成一个单独的zend_execute_data结构. php的局部变 ...
- JAVA面向对象编程课程设计——web版斗地主
一.团队课程设计博客链接 JAVA面向对象编程课程设计--网络版单机斗地主 二.个人负责模块或任务说明 实体类的设计 斗地主规则的实现 人机自动出牌的算法 实现数据库的DAO模式 三.自己的代码提交记 ...
- Windows 10 修改系统环境变量后,CMD生效,Powershell未生效
Windows 10 修改系统环境变量后 1.CMD重新打开,新环境变量已经生效: 2.Shift+右键Powershell重新打开,新环境变量没有生效: 3.貌似服务(Apache24)中使用的环境 ...
- [转]C# int.ToString()
原文链接:https://msdn.microsoft.com/zh-cn/library/dwhawy9k 原文链接:https://msdn.microsoft.com/zh-cn/library ...
- Fiddler 抓包工具总结-bpafter
转摘https://www.cnblogs.com/shy1766IT/p/5199334.html Fiddler 抓包工具总结 名称 含义 # 抓取HTTP Request的顺序,从1开始,以 ...
- istio 配置https gateway
沒有親手實驗,参考官方文档: https://istio.io/docs/tasks/traffic-management/secure-ingress/