【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 ...
随机推荐
- C# 与C++的数据转换
一.类型转化 下面重点罗列下常用的类型转化. C++类型 C#类型 备注说明 Int Int16.Int32 没有悬念,直接转化 Uint UInt16.Uint32.int 在程序中,不太清楚是,就 ...
- ARC forbids explicit message send of release
http://blog.sina.com.cn/s/blog_7b9d64af01019rqt.html
- gson小练习之嵌套复杂数据解析
package com.zf.demo; import java.util.List; import com.google.gson.Gson; public class JGson { /** * ...
- 菜鸟聊:PHP
学习PHP已经有2个月时间了,从一开始的一片空白,到现在的刚刚入门,我对PHP的了解也有更多的认知,希望通过我对PHP的理解,能帮助到更多像我一样的新手更早的认识PHP.(PS:以下内容的一部分是摘自 ...
- jdbc学习(一)——SqlServer、Oracle和MySQL
一.jdbc介绍 jdbc全称:java数据库连接(Java Database Connectivity),是sun公司定义的一套访问数据库的规范(接口和类,由各种数据库公司进行实现),主要放在jav ...
- 浅谈strtok
原型:char *strtok(char *s, char *delim); 功能:分解字符串为一组标记串.s为要分解的字符串,delim为分隔符字符串. 说明:首次调用时,s必须指向要分解的字符串, ...
- raise_application_error用法
我们经常通过dbms_output.put_line来输出异常信息,但有时需要把异常信息返回给调用的客户端.此时我们用raise_application_error,允许用户在pl/sql中返回用户自 ...
- 【BZOJ 2654】tree
Description 给你一个无向带权连通图,每条边是黑色或白色.让你求一棵最小权的恰好有need条白色边的生成树. 题目保证有解. Input 第一行V,E,need分别表示点数,边数和需要的白色 ...
- linux安装软件命令
tar.bz2的解压: tar -jxvf FileName.tar.bz2 然后安装: cd FileName ./configure make make install rpm 包的安装: rpm ...
- 2734: [HNOI2012]集合选数 - BZOJ
Description <集合论与图论>这门课程有一道作业题,要求同学们求出{1, 2, 3, 4, 5}的所有满足以 下条件的子集:若 x 在该子集中,则 2x 和 3x 不能在该子集中 ...