【HDOJ】4775 Infinite Go
其实是一道模拟题,并查集用来优化。还可以的一道题目。
/* 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的更多相关文章
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
- 【HDOJ】【1512】Monkey King
数据结构/可并堆 啊……换换脑子就看了看数据结构……看了一下左偏树和斜堆,鉴于左偏树不像斜堆可能退化就写了个左偏树. 左偏树介绍:http://www.cnblogs.com/crazyac/arti ...
随机推荐
- 【Winform】 无法将类型为“System.Windows.Forms.SplitContainer”的对象强制转换为类型“System.ComponentModel.ISupportInitialize”。
问题:将dotnet framework 4.0 切换到2.0时,编译没有问题,在运行时出现如下错误:System.InvalidCastException: 无法将类型为“System.Window ...
- (转)[Erlang 0080] RabbitMQ :VHost,Exchanges, Queues,Bindings and Channels
和RabbitMQ这个项目的缘分好奇怪,很长一段时间内是只关注源代码,真的是Erlang开源项目中的典范;现在要在项目中应用RabbitMQ,从新的视角切入,全新的感觉.仿佛旧情人换了新衣,虽是熟稔却 ...
- firefox 自定义快捷键
firefox 更新到44或45,发现原来的更改快捷键的扩展没了!!!
- CC2640-之功耗
一.测量方式,以DEMO板测量,以消除其它外围不同造成的电流不同. 二.测量结果 以原厂simpleBLEperipheral工程为例 1.如果在低功耗模式下,+5DB发射,最小电流为1.66MA 2 ...
- mysqlsla慢查询分析工具教程
mysqlsla是一款帮助语句分析.过滤.和排序的功能,能够处理MySQL慢查询日志.二进制日志等.整体来说, 功能非常强大. 能制作SQL查询数据报表,分析包括执行频率, 数据量, 查询消耗等. 且 ...
- PHP webserver 之 soap non-wsdl
non-wsdl 顾名思义就是不使用wsdl文件进行的webserver服务 事实上个人觉得用wsdl模式还不如使用non-wsdl模式,wsdl模式稍加麻烦! 1.网站运行环境下开启soap模块(p ...
- ios 基于CAEmitterLayer的雪花,烟花,火焰,爱心等效果demo(转)
转载自:http://blog.csdn.net/mad2man/article/details/16898369 分类: cocoa SDK2013-11-23 11:52 388人阅读 评论(0) ...
- dive into python 读笔(1)
chapter2 and 3: 使用Python IDE来交互式地测试表达式 编写Python程序并且从IDE运行,或者从命令行运行 导入模块及调用它们的函数 声明函数以及doc string.局部变 ...
- 【BZOJ 1070】[SCOI2007]修车
Description 同一时刻有N位车主带着他们的爱车来到了汽车维修中心.维修中心共有M位技术人员,不同的技术人员对不同的车进行维修所用的时间是不同的.现在需要安排这M位技术人员所维修的车及顺序,使 ...
- sql shard/partition
sql http://www.infoq.com/news/2011/02/SQL-Sharding/ http://channel9.msdn.com/Shows/Data-Exposed/SqlD ...