BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III
欢迎访问~原文出处——博客园-zhouzhendong
去博客园看该题解
题目传送门 - BZOJ4997
题意概括
在n*n的区域里,每一个1*1的块都是一个格子。
有k头牛在里面。
有r个篱笆把格子分开。
如果两头牛可以不经过篱笆走到一起(过程中不能出界),那么他们就是不互相远离的,反之就是互相远离的。
问有多少对牛是互相远离的。注意(x,y)和(y,x)算作同样的。
题解
对于同一区域的牛,我们可以相同对待。
所以我们dfs给各自连通的区域分开来,分别统计每一块的牛数,然后乘法原理+加法原理就可以了。
代码
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cmath>
using namespace std;
const int N=100+5;
const int dx[4]={ 0, 0,-1, 1};
const int dy[4]={-1, 1, 0, 0};
int n,m,k,r,v[N][N],cnt,tot[N*N];
bool f[N][N][4];
int find_d(int x,int y){
for (int i=0;i<4;i++)
if (x==dx[i]&&y==dy[i])
return i;
return -1;
}
void dfs(int x,int y){
if (v[x][y])
return;
v[x][y]=cnt;
for (int i=0;i<4;i++)
if (!f[x][y][i])
dfs(x+dx[i],y+dy[i]);
}
int main(){
scanf("%d%d%d",&n,&k,&r);
memset(f,0,sizeof f);
for (int i=1;i<=n;i++){
f[i][1][0]=1;
f[i][n][1]=1;
f[1][i][2]=1;
f[n][i][3]=1;
}
cnt=0;
for (int i=1;i<=r;i++){
int x_1,y_1,x_2,y_2,x,y;
scanf("%d%d%d%d",&x_1,&y_1,&x_2,&y_2);
x=x_2-x_1,y=y_2-y_1;
f[x_1][y_1][find_d(x,y)]=1;
f[x_2][y_2][find_d(-x,-y)]=1;
}
cnt=0;
memset(v,0,sizeof v);
memset(tot,0,sizeof tot);
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
if (!v[i][j]){
cnt++;
dfs(i,j);
}
for (int i=1,x,y;i<=k;i++){
scanf("%d%d",&x,&y);
tot[v[x][y]]++;
}
m=cnt;
int sum=0,ans=0;
for (int i=1;i<=m;i++)
sum+=tot[i];
for (int i=1;i<=m;i++){
sum-=tot[i];
ans+=tot[i]*sum;
}
printf("%d",ans);
return 0;
}
BZOJ4997 [Usaco2017 Feb]Why Did the Cow Cross the Road III的更多相关文章
- BZOJ4994 [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4994 题意概括 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi ...
- 【bzoj4994】[Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组
题目描述 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 样例输入 4 3 2 4 4 1 3 2 1 样例输 ...
- [BZOJ4994] [Usaco2017 Feb]Why Did the Cow Cross the Road III(树状数组)
传送门 1.每个数的左右位置预处理出来,按照左端点排序,因为左端点是从小到大的,我们只需要知道每条线段包含了多少个前面线段的右端点即可,可以用树状数组 2.如果 ai < bj < bi, ...
- [Usaco2017 Feb]Why Did the Cow Cross the Road III (Gold)
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai < aj < bi < bj的对数 Sample Input ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- bzoj 4994: [Usaco2017 Feb]Why Did the Cow Cross the Road III 树状数组_排序
Description 给定长度为2N的序列,1~N各处现过2次,i第一次出现位置记为ai,第二次记为bi,求满足ai<aj<bi<bj的对数 题解: 方法一: 搞一个KDtree, ...
- 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 线段树维护dp
题目 4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II 链接 http://www.lydsy.com/JudgeOnline/proble ...
- 4989: [Usaco2017 Feb]Why Did the Cow Cross the Road
题面:4989: [Usaco2017 Feb]Why Did the Cow Cross the Road 连接 http://www.lydsy.com/JudgeOnline/problem.p ...
- [BZOJ4990][Usaco2017 Feb]Why Did the Cow Cross the Road II dp
4990: [Usaco2017 Feb]Why Did the Cow Cross the Road II Time Limit: 10 Sec Memory Limit: 128 MBSubmi ...
随机推荐
- ubuntu新建组合用户命令不管用
当我们新建了组和用户的时候发现一些命令不管用了,这是什么问题呢. 一.解决方案. 通过以下命令添加组和用户,切换到新用户,发现命令不管用,只有一个$符号. groupadd 新组名 ----添加组 u ...
- The android command is deprecated
新版的SDK tools中的android命令已经不支持 android create project,用起来很不顺手. The "android" command is depr ...
- python的安装和pycharm的安装
下载地址 官网:https://www.python.org/downloads/release/python-372/ Window 平台安装 Python: Add python xx to ...
- [C++]C++与C头文件辨析(比较)
C++/C头文件辨析 C++标准库 C标准库 C++标准模板库 ios vector iomanip deque sstream list fstream map set ...
- float/double 浮点数据*100精度丢失问题
工作中微信支付碰到的一个问题,金额是float数字,微信参数需要分且必须是整数,所以*100的时候就有问题了 System.out.println(9.9f*100); //989.99994Syst ...
- Ubuntu16.04+CUDA8.0+cudnn6
按之前的方法给TITAN X安装cuda8.0会发生循环登录的问题,因此换了一种安装方法 参考:https://www.jianshu.com/p/002ece426793,http://blog.c ...
- 基于XML搭建Dubbo项目
(1).新建一个普通Maven项目,用于存放一些公共服务接口及公共的Bean等. 公共Bean: package cn.coreqi.entities; import java.io.Serializ ...
- Windows域帐户
域的直观优点: 1.域帐户可以在任意一台已经加入域的电脑上登录. 2.将域用户组加入到SQL Server登录里,域用户组内所有人员便都可以使用域用户登录数据库,继承相关权限. 3.域用户登录Team ...
- MIPI协议学习总结(一)
一.MIPI 简介: MIPI(移动行业处理器接口)是Mobile Industry Processor Interface的缩写.MIPI是MIPI联盟发起的为移动应用处理器制定的开放标准. 已经完 ...
- kafka系列九、kafka事务原理、事务API和使用场景
一.事务场景 最简单的需求是producer发的多条消息组成一个事务这些消息需要对consumer同时可见或者同时不可见 . producer可能会给多个topic,多个partition发消息,这些 ...