【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 ...
随机推荐
- jackson 解析结合类(需要传入Class, 和 Class.Class, 回调方法是List<Class>)
import java.util.HashMap; import java.util.List; import org.codehaus.jackson.map.ObjectMapper; impor ...
- Spark Streaming揭秘 Day33 checkpoint的使用
Spark Streaming揭秘 Day33 checkpoint的使用 今天谈下sparkstreaming中,另外一个至关重要的内容Checkpoint. 首先,我们会看下checkpoint的 ...
- Opencv 的数据结构
opencv的基本数据结构 结构 成员 意义 CvPoint int x,y 图像中的点 CvPoint2D32f float x,y 二维空间中的点 CvPoint3D32f float x,y,z ...
- 【Python笔记】异常处理
1 什么是异常 异常即是一个事件,该事件会在程序执行过程中发生,影响了程序的正常执行.一般情况下,在Python无法正常处理程序时就会发生一个异常.异常是Python对象,表示一个错误. 当Pytho ...
- WebApp之 apple-touch-icon
在iPhone,iPad,iTouch的safari上可以使用添加到主屏按钮将网站添加到主屏幕上.apple-touch-icon是IOS设备的私有标签,如果设置了相应apple-touch-icon ...
- buffer busy wait在RAC环境下出现
昨天运维组的同时反映有套系统用户反映很慢,需要协助帮忙检查什么原因引起的性能问题.导出了从8点到11点的AWR报告进行分析,发现等待事件里大部分的指标都正常,就是buffer busy wait的平均 ...
- 关于FileSystemWatcher监听文件创建
FileSystemWatcher中的Created事件不但可以监听用户创建的文件,当用户删除某个文件时,系统会在再监听的那个盘上的回收站创建一个文件,在回收站创建的文件也会触发Created事件,而 ...
- JS 实现取整(二)
1.直接丢弃小数部分,保留整数部分 a:parseInt(1.5555) b: 0|1.5555 2.向上取整 a: Math.ceil(1.5555) b: (1.5555+0.5).toFixed ...
- SVN备份教程(一)
最近一段时间在项目中用到了SVN备份的相关内容,这里给大家做一个简单的教程,重点在于SVN备份环境的搭建过程中,大家学到的解决问题的思维方式. 1.分类 SVN备份主要分为两种:一种是远程备份,另一种 ...
- OC 数据类型之间的转换方法
NSNumber转NSString: 假设现有一NSNumber的变量A,要转换成NSString类型的B 方法如下: NSNumberFormatter* numberFormatter = [ ...