[CF85E] Guard Towers - 二分+二分图
题目描述
In a far away kingdom lives a very greedy king. To defend his land, he built n n n guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower.
During defence operations to manage a guard tower a general has to send part of his army to that tower. Each general asks some fee from the king for managing towers. As they live in a really far away kingdom, each general evaluates his fee in the following weird manner: he finds two remotest (the most distant) towers, where the soldiers of his army are situated and asks for the fee equal to the distance. Each tower is represented by a point on the plane with coordinates (x,y) (x,y) (x,y) , and the distance between two points with coordinates (x1,y1) (x_{1},y_{1}) (x1,y1) and (x2,y2) (x_{2},y_{2}) (x2,y2) is determined in this kingdom as ∣x1−x2∣+∣y1−y2∣ |x_{1}-x_{2}|+|y_{1}-y_{2}| ∣x1−x2∣+∣y1−y2∣ .
The greedy king was not exactly satisfied with such a requirement from the generals, that's why he only agreed to pay one fee for two generals, equal to the maximum of two demanded fees. However, the king is still green with greed, and among all the ways to arrange towers between armies, he wants to find the cheapest one. Each tower should be occupied by soldiers of exactly one army.
He hired you for that. You should find the minimum amount of money that will be enough to pay the fees. And as the king is also very scrupulous, you should also count the number of arrangements that will cost the same amount of money. As their number can be quite large, it is enough for the king to know it as a remainder from dividing by 109+7 10^{9}+7 109+7 .
Two arrangements are distinct if the sets of towers occupied by soldiers of the first general are distinct.
输入输出格式
输入格式:
The first line contains an integer n n n ( 2<=n<=5000 2<=n<=5000 2<=n<=5000 ), n n n is the number of guard towers. Then follow n n n lines, each of which contains two integers x,y x,y x,y — the coordinates of the i i i -th tower (0<=x,y<=5000) (0<=x,y<=5000) (0<=x,y<=5000) . No two towers are present at one point.
Pretest 6 is one of the maximal tests for this problem.
输出格式:
Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals.
Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 1000000007 1000000007 ( 109+7 10^{9}+7 109+7 ).
输入输出样例
2
0 0
1 1
0
2
4
0 0
0 1
1 0
1 1
1
4
3
0 0
1000 1000
5000 5000
2000
2
说明
In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in two ways.
二分判断是否是二分图;
二分集合里的最小值,把大于这个值的连上一条边,然后判断是否是二分图;
我因为没开long long调了一上午,生无可恋。
Code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
using namespace std;
const int mod = ; int n, X[], Y[];
int dis[][];
int color[];
int l = , r=1e6;
int bel[]; inline long long ksm(long long a, long long y)
{
int res = ;
while (y)
{
if (y & ) res = (res % mod * a % mod) % mod;
a = (a % mod * a % mod) % mod;
y >>= ;
}
return res % mod;
} bool dfs(int x, int col, int minn)
{
color[x] = col;
for (register int i = ; i <= n ; i ++)
{
if (x == i) continue;
if (dis[x][i] > minn)
{
if (color[i] == -)
{
if (!dfs(i, -col, minn)) return ;
}
else if (color[i] != - col) return ;
}
}
return ;
} inline bool check(int x)
{
memset(color, -, sizeof color);
for (register int i = ; i <= n ; i ++)
if (color[i] == -)
if (!dfs(i, , x)) return ;
return ;
} void dfs2(int x, int minn, int nu)
{
bel[x] = nu;
for (register int i = ; i <= n ; i ++)
{
if (i == x) continue;
if (bel[i]) continue;
if (dis[x][i] <= minn) continue;
dfs2(i, minn, nu);
}
} int main()
{
scanf("%d", &n);
for (register int i = ; i <= n ; i ++) scanf("%d%d",&X[i],&Y[i]);
for (register int i = ; i <= n ; i ++)
for (register int j = ; j <= n ; j ++)
dis[i][j] = abs(X[i]-X[j]) + abs(Y[i]-Y[j]);
int ans;
while (l < r)
{
int mid = l + r >> ;
if (check(mid)) r = mid;
else l = mid + ;
}
ans = l;
cout << ans << endl;
int tot = ;
for (register int i = ; i <= n ; i ++)
{
if (!bel[i]) tot++, dfs2(i, ans, tot);
}
cout << (ksm(, tot)) % mod;
return ;
}
[CF85E] Guard Towers - 二分+二分图的更多相关文章
- CF85E Guard Towers(二分答案+二分图)
题意 已知 N 座塔的坐标,N≤5000 把它们分成两组,使得同组内的两座塔的曼哈顿距离最大值最小 在此前提下求出有多少种分组方案 mod 109+7 题解 二分答案 mid 曼哈顿距离 >mi ...
- 「CF85E」 Guard Towers
「CF85E」 Guard Towers 模拟赛考了这题的加强版 然后我因为初值问题直接炸飞 题目大意: 给你二维平面上的 \(n\) 个整点,你需要将它们平均分成两组,使得每组内任意两点间的曼哈顿距 ...
- BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配
BZOJ_4443_[Scoi2015]小凸玩矩阵_二分+二分图匹配 Description 小凸和小方是好朋友,小方给小凸一个N*M(N<=M)的矩阵A,要求小秃从其中选出N个数,其中任意两个 ...
- NOIP 2010 关押罪犯 并查集 二分+二分图染色
题目描述: S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值" ...
- [NOIP 2010] 关押罪犯 (二分+二分图判定 || 并查集)
题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...
- LUOGU 1525 关押罪犯 - 并查集拆点(对立点) / 二分+二分图染色
传送门 分析: 并查集: 第一步先将所有矛盾从大至小排序,显然先将矛盾值大的分成两部分会更优. 普通的并查集都只能快速合并两个元素至同一集合,却不能将两个元素分至不同集合. 对于将很多数分成两个集合, ...
- CF 85E Guard Towers——二分图染色
题目:http://codeforces.com/contest/85/problem/E 当然是二分.然后连一个图,染色判断是不是二分图即可.方案数就是2^(连通块个数). 别真的连边!不然时间空间 ...
- CF85 E Guard Towers——二分图
题目:http://codeforces.com/contest/85/problem/E 给定一些点的坐标,求把它们分成两组,组内最大距离的最小值: 二分答案,判断就是看距离大于 mid 的点能否组 ...
- [USACO2003][poj2112]Optimal Milking(floyd+二分+二分图多重匹配)
http://poj.org/problem?id=2112 题意: 有K个挤奶器,C头奶牛,每个挤奶器最多能给M头奶牛挤奶. 每个挤奶器和奶牛之间都有一定距离. 求使C头奶牛头奶牛需要走的路程的最大 ...
随机推荐
- TestNG(十五)xml文件实现多线程测试
package com.course.testng.thread; import org.testng.annotations.Test; public class ThreadOnXml { @Te ...
- Unity子弹生成系统
子弹系统和粒子系统比较类似,为了创建和五花八门的子弹,例如追踪,连续继承,散弹等,需要一个拥有众多参数的子弹生成器,这里叫它Shooter好了. Shooter负责把玩各类子弹造型和参数,创建出子弹, ...
- Laravel 从入门到精通 创建并运行一个新的 Laravel 项目
创建一个新的 Laravel 项目 正如官方文档所言,有两种方式可以创建一个新的 Laravel 项目,这两种创建方式都是从命令行执行的:第一种是通过全局的 Laravel 安装器,另一种是通过 Co ...
- 使用flask-mail扩展发送邮件
使用flask-mail扩展发送电子邮件 在我们开发完web系统后,一些特定的事件发生时,系统要自动发送相关邮件至管理员,运维人员和其他相关人员.python标准库中的smtplib包也可以用在Fla ...
- linux环境下Nginx的配置及使用
切换到目录/usr/local/nginx/sbin,/usr/local为nginx的默认安装目录 #启动 ./nginx #查看命令帮助 ./nginx -h 验证配置文件状态 ./nginx - ...
- 【面试题】Java基础部分面试题
Java基础面试题 Equals与==的区别 使用==比较原生类型如:boolean,,int,char等等, 使用equals()比较对象. 1. ==是判断两个变量或类型是不是指向同一个内存空 ...
- 23种设计模式之责任链模式(Chain of Responsibility Pattern)
责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链.这种模式给予请求的类型,对请求的发送者和接收者进行解耦.这种类型的设计模式属于行为型模式. ...
- 读《深入理解Elasticsearch》点滴-查询评分
计算文档得分的因子: 文档权重(document boost):索引期赋予某个文档的权重值 字段权重(field boost):查询期赋予某个文档的权重值 协调因子(coord):基于文档中词项个数的 ...
- Flask基础(11)-->http的无状态协议解决办法二(服务端session)
session的交互流程 Session 对于敏感.重要的信息,建议存在服务器端,不能存储在浏览器中,比如用户名,密码,余额,验证码等信息 在服务器端进行状态保持的方案就是Session Sessio ...
- Kafka常用命令合集
在上一篇文章<Linux安装Kafka>中,已经介绍了如何在Linux安装Kafka,以及Kafka的启动/关闭和创建发话题并产生消息和消费消息.这篇文章就介绍介绍Kafka的那些常用的命 ...