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

 /* 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. 利用HibernateTools从数据库表生成带注解的POJO

    在SSH框架中,如果先设计好了数据库,那么下一步就需要从数据库Table生成实体java类和hbm.xml配置文件.在最新的开发框架中,已经支持使用注解,从而避免了繁琐的hbm.xml配置,而且我们可 ...

  2. c#怎么获取当前页面的url

    Request.ApplicationPath: /testwebRequest.CurrentExecutionFilePath: /testweb/default.aspxRequest.File ...

  3. 为什么Facebook要将视频从Flash全面迁移到HTML5?

    英文原文:Why we chose to move to HTML5 video 编者按:Facebook 前端高级工程师 Daniel Baulig 解释了 Facebook 为什么要将视频全面迁移 ...

  4. mysql之创建外键报150错误的处理方法

    这几天由于在赶项目进度,也就没有及时记录下自己的学习情况 ,在完成项目的这段时间里,碰到了很多问题,在解决问题的过程中学习了不少技巧. 这里就主要介绍一下在mysql数据库中为表之间建立外键时报100 ...

  5. SQL存储过程和触发器

    一. 存储过程 1.  有关概念 存储过程是由SQL语句及控制流语句组成的集合.调用一个存储过程,可以一次性地执行过程中的所有语句.从这一点来说,它类似于程序. 存储过程由用户建立,它作为数据库的一个 ...

  6. 1006. Sign In and Sign Out

    #include <stdio.h> #include <algorithm> #include <iostream> #include <string.h& ...

  7. Eclipse--Team--SVN--URL修改

    1.团队开发服务器,有的时候会更换地址 解决: eclipse--菜单Windows-Show View-others-svn--svn资源库 打开资源库面板 右击http://localhost:9 ...

  8. 【BZOJ 2878】 [Noi2012]迷失游乐园

    Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...

  9. Elasticsearch从0.90(0.90.x)到1.2(1.x)API的变化-二

    本文为官方文档的译文加个人理解.作者翻译时,elasticsearch(下面简称es)的版本为1.2.2. 请支持原创:http://www.cnblogs.com/donlianli/p/38367 ...

  10. xx创新论坛返工友情项目总结

    友情项目,顾名思义就不是我做的,只是处于友情帮别人改改别人的代码帮别人找找bug...之所以要强调这一点是因为里面的低级问题太多,实在是不好意思承认自己和这个项目有关系.. 整个过程还是挺辛苦的,毕竟 ...