其实是一道模拟题,并查集用来优化。还可以的一道题目。

 /* 4775 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 const int maxn = ;
map<pii, int> id;
map<pii, bool> visit;
map<pii, int>::iterator iter;
int X[maxn], Y[maxn];
int sz[maxn], fa[maxn];
int dir[][] = {
-,, ,, ,-, ,
};
int n; int find(int x) {
if (x == fa[x])
return x; return fa[x] = find(fa[x]);
} bool judge(int x, int y) {
return x<= || y<=;
} void remove(int bx, int by, int uid) {
queue<pii> Q;
int x, y;
pii p(bx, by), pp; visit.clr();
Q.push(p);
visit[p] = true; while (!Q.empty()) {
p = Q.front();
Q.pop(); // remove from the id
iter = id.find(p);
fa[iter->sec] = iter->sec;
sz[iter->sec] = ;
id.erase(iter); rep(j, , ) {
x = p.fir + dir[j][];
y = p.sec + dir[j][];
if (judge(x, y))
continue; pp.fir = x;
pp.sec = y;
if (visit[pp])
continue; iter = id.find(pp);
if (iter == id.end())
continue; if ((iter->sec&) ^ uid) {
int f = find(iter->sec);
++sz[f];
} else {
visit[pp] = true;
Q.push(pp);
}
}
}
} void solve() {
rep(i, , n+)
fa[i] = i;
memset(sz, , sizeof(sz));
id.clr(); int vac, x, y;
int xx, yy;
pii p; rep(i, , n+) {
p.fir = x = X[i];
p.sec = y = Y[i];
id[p] = i;
vac = ;
rep(j, , ) {
xx = x + dir[j][];
yy = y + dir[j][];
if (judge(xx, yy))
continue; iter = id.find(mp(xx, yy));
if (iter == id.end()) {
++vac;
} else {
int f = find(iter->sec);
--sz[f];
}
}
sz[i] = vac;
rep(j, , ) {
xx = x + dir[j][];
yy = y + dir[j][];
if (judge(xx, yy))
continue; iter = id.find(mp(xx, yy));
if (iter == id.end())
continue; if ((iter->sec & ) ^ (i & )) {
// delete enemy
int fx = find(iter->sec);
if (sz[fx] == ) {
remove(xx, yy, iter->sec&);
} } else {
// link self
int fx = find(iter->sec);
int fy = find(i);
if (fx != fy) {
fa[fx] = fy;
sz[fy] += sz[fx];
}
}
} int f = find(i);
if (sz[f] == ) {
remove(x, y, i&);
}
} int ansa, ansb; ansa = ansb = ;
visit.clr();
per(i, , n+) {
p.fir = X[i];
p.sec = Y[i];
iter = id.find(p);
if (iter==id.end() || visit[p])
continue; visit[p] = true;
if (iter->sec & )
++ansa;
else
++ansb;
} printf("%d %d\n", ansa, ansb);
} int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif int t; scanf("%d", &t);
while (t--) {
scanf("%d", &n);
rep(i, , n+)
scanf("%d %d", &X[i], &Y[i]);
solve();
} #ifndef ONLINE_JUDGE
printf("time = %d.\n", (int)clock());
#endif return ;
}

数据发生器。

 from random import randint, shuffle
import shutil
import string def GenDataIn():
with open("data.in", "w") as fout:
t = 10
fout.write("%d\n" % t)
bound = 10**2
for tt in xrange(t):
n = randint(1000, 5000)
fout.write("%d\n" % n)
d = dict()
for i in xrange(n):
while True:
x = randint(1, bound)
y = randint(1, bound)
tpl = (x, y)
if tpl not in d:
d[tpl] = True
break
fout.write("%d %d\n" % (x, y)) def MovDataIn():
desFileName = "F:\eclipse_prj\workspace\hdoj\data.in"
shutil.copyfile("data.in", desFileName) if __name__ == "__main__":
GenDataIn()
MovDataIn()

【HDOJ】4775 Infinite Go的更多相关文章

  1. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  2. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  3. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  4. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  5. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  6. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  7. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  8. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

  9. 【HDOJ】【1512】Monkey King

    数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...

随机推荐

  1. linux 神器之wget

    1.什么是Wget? 首页,它是网络命令中最基本的.最好用的命令之一; 文字接口网页浏览器的好工具. 它(GNU Wget)是一个非交互从网上下载的自由工具(功能).它支持http.ftp.https ...

  2. php微信支付(仅Jsapi支付)详细步骤.----仅适合第一次做微信开发的程序员

    本人最近做了微信支付开发,是第一次接触.其中走了很多弯路,遇到的问题也很多.为了让和我一样的新人不再遇到类似的问题,我把我的开发步骤和问题写出来,以供参考. 开发时间是2016/8/2,所以微信支付的 ...

  3. Django的templates模版

    前面介绍的都是django.http.HttpResponse 把内容直接返回到网页上,这次介绍通过渲染模版的方法来显示内容 步骤: 1.创建一个项目(略) 2.创建一个app(略) 3.添加项目到s ...

  4. [python] 字符串引用

    %s str %d 整数 %f 浮点数 print('$%.03f' % 30.1777) >>>$30.178  #四舍五入 print( '%-5s %s %10s' % ('J ...

  5. 在iframe结构中,如何使弹出层位于所有框架的上方(-)

    在做后台管理页面的时候,经常用到iframe.虽说这东西有些过时,也不利于SEO,但是后台就是后台,不需要考虑那么多东西,综合来说,用iframe还是很适合后台管理界面的. 但是在遇到弹出层时,出现了 ...

  6. shell curl

    最近突然发现了一个有趣的问题:怎样判断日期是工作日还是节假日.(http://www.cnblogs.com/ZXdeveloper/p/4018886.html) 顺便发现了一个有用的网址:http ...

  7. Arcgis 10.1中空间连接功能

    空间链接的作用:将面上的所有点的值加起来取平均值.赋值给面属性.(我们可以定义右击——定义合并规则 连接要素的字段映射参数中指定的合并规则仅适用于连接要素中的属性,且仅适用于多个要素与目标要素匹配 ( ...

  8. 2017年iOS应用将强制使用HTTPS安全加密-b

    6月14日,WWDC 2016苹果开发者大会上,苹果在讲解全新的iOS10中提到了数据安全这一方面,并且苹果宣布iOS应用将从2017年1月起启用名为App Transport Security的安全 ...

  9. 使用Docker解决同一服务器运行不同版本PHP方案。

    前言: 最近公司有两个站点,分别是两种系统进行二次开发,基于LNMP架构的网站.一般想PHP这种非编译型语言想要对外出售源码都会进行加密,加密方法有很多种,大部分都是使用Zend Guard来进行加密 ...

  10. bzoj 1228: [SDOI2009]E&D 阿达马矩阵

    1228: [SDOI2009]E&D Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 448  Solved: 240[Submit][Sta ...