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 ...
随机推荐
- Json Schema简介
1. 引言 什么是Json Schema? 以一个例子来说明 假设有一个web api,接受一个json请求,返回某个用户在某个城市关系最近的若干个好友.一个请求的例子如下: { "city ...
- 第14月第17天 automaticallyAdjustsScrollViewInsets contentInsetAdjustmentBehavior
1. automaticallyAdjustsScrollViewInsets self.edgesForExtendedLayout = UIRectEdgeNone; if ([self resp ...
- python安装模块方法汇总
方法一: 原文地址: http://blog.csdn.net/cighao/article/details/47860041 在 windows 系统下,只需要输入命令 pip install re ...
- mysql 架构~mgr具体细节分析
一 简介:今天咱们来聊聊mgr的具体实现细节 二 关于多点写入的锁冲突问题以及处理: certify模块主要负责检查事务是否允许提交,是否与其它事务存在冲突,如两个事务可能修改同一行数据.在单机系 ...
- MGR架构~单写模式架构的搭建
一 简介 :MGR一直没有时间测试,今天咱们来初步了解搭建一下呗 二 环境: mysql5.7.20 单台机器 启动三实例 三 mysql 搭建: 1 建立相关目录+ mkdir -p /data ...
- BN讲解(转载)
本文转载自:http://blog.csdn.net/shuzfan/article/details/50723877 本次所讲的内容为Batch Normalization,简称BN,来源于< ...
- git命令行工作环境配置【转】
转自:http://www.cocoachina.com/ios/20171115/21163.html 本文为CocoaChina网友whf5566投稿 前言 笔者一直使用git的图形化工具sour ...
- 【转】Source Insight中文注释为乱码的解决办法
我网上查了一堆解决办法,但是都是2017年以前的,并且都是针对于source insight 3.5及以下版本的解决方案,软件版本都到4.0了,应该有新方法出现. 干货:Source Insight ...
- 安装ClamAV对centos系统进行病毒查杀
安装ClamAV 1.安装epel源 yum install epel-release 在安装了EPEL源后,运行下面的命令安装ClamAV # yum install clamav-server c ...
- ubuntu系统初始化网络及mysql配置
安装系统时需要安装open-ssh服务软件,否则无法远程连接 1.修改root密码 # sudo passwd 输入密码即可 切换到root用户,需要输入刚才的root密码 # su - 2.配置网络 ...