题意

不想写。

题解

场上想了 1h+ 无果,一到场外就口胡出来了,我真是个 sb。

首先注意到如果 \(t\) 满足条件那么 \(t+1\) 也会满足,所以答案具有单调性,可以二分,于是现在只需要考虑对于某个特定的 \(t\) 能否满足。

注意到对于平面上的 \(u,v,w\) 三个点来说,如果 \(u\) 能到 \(v\),\(v\) 能到 \(w\) 那么 \(u\) 一定能到 \(w\)。

既然这个可达性是可以传递的,那么可以用一个并查集来将相互到的点缩成一个连通块。

如果连通块个数为 \(1\) 那么一定可以,大于 \(4\) 就一定不行,剩下的情况讨论一下。

如果连通块个数为 \(2\),那么只需要找出两个分属不同连通块的点,然后看看加一个点能不能使得这两个点互相可达即可。

如果连通块个数为 \(3\),那么只需要找出一条线段和一个点,线段的两个端点和选出来的点分属三个不同的连通块。这个时候注意到加的那个点是已经确定了的,所以直接判断一下即可。

如果连通块个数为 \(4\),那么只需要找出两条相交的线段,四个端点分属不同的连通块。这个时候新加的那个点就是线段的端点。简单判断一下可达性即可。

本题代码细节比较多,建议配合代码理解。

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
const ll MAXN=1e3+51;
struct Tuple{
ll x,y;
inline bool operator <(const Tuple &rhs)const
{
return this->x==rhs.x?this->y<rhs.y:this->x<rhs.x;
}
};
map<ll,vector<Tuple>>row,col;
ll n,totr,totc,l,r,mid,res=-1;
ll ffa[MAXN],x[MAXN],y[MAXN],bel[MAXN],rows[MAXN],cols[MAXN];
inline ll read()
{
register ll num=0,neg=1;
register char ch=getchar();
while(!isdigit(ch)&&ch!='-')
{
ch=getchar();
}
if(ch=='-')
{
neg=-1;
ch=getchar();
}
while(isdigit(ch))
{
num=(num<<3)+(num<<1)+(ch-'0');
ch=getchar();
}
return num*neg;
}
inline ll find(ll x)
{
return x==ffa[x]?x:ffa[x]=find(ffa[x]);
}
inline void merge(ll x,ll y)
{
ll fx=find(x),fy=find(y);
if(fx!=fy)
{
ffa[fy]=fx;
}
}
inline ll check(ll mid)
{
ll blk=0,p,q,r,s,lx,rx;
for(register int i=1;i<=n;i++)
{
ffa[i]=i;
}
for(register int i=1;i<=n;i++)
{
for(register int j=i+1;j<=n;j++)
{
if(x[i]==x[j]&&abs(y[i]-y[j])<=mid)
{
merge(i,j);
}
if(y[i]==y[j]&&abs(x[i]-x[j])<=mid)
{
merge(i,j);
}
}
}
for(register int i=1;i<=n;i++)
{
blk+=(find(i)==i);
}
if(blk==1)
{
return 1;
}
if(blk>4)
{
return 0;
}
for(register int i=1;i<=n;i++)
{
bel[i]=ffa[i];
}
if(blk==2)
{
for(register int i=1;i<=n;i++)
{
for(register int j=1;j<=n;j++)
{
if(bel[i]==bel[j])
{
continue;
}
if(x[i]==x[j]&&abs(y[i]-y[j])<=2*mid)
{
return 1;
}
if(y[i]==y[j]&&abs(x[i]-x[j])<=2*mid)
{
return 1;
}
if(abs(x[i]-x[j])<=mid&&abs(y[i]-y[j])<=mid)
{
return 1;
}
}
}
return 0;
}
if(blk==3)
{
vector<Tuple>v;
for(register int i=1;i<=totr;i++)
{
for(register int j=1;j<row[rows[i]].size();j++)
{
p=row[rows[i]][j-1].y,q=row[rows[i]][j].y;
bel[p]!=bel[q]?v.push_back((Tuple){p,q}):(void)1;
}
}
for(register int i=1;i<=totc;i++)
{
for(register int j=1;j<col[cols[i]].size();j++)
{
p=col[cols[i]][j-1].y,q=col[cols[i]][j].y;
bel[p]!=bel[q]?v.push_back((Tuple){p,q}):(void)1;
}
}
for(register int i=0;i<v.size();i++)
{
p=v[i].x,q=v[i].y;
for(register int j=1;j<=n;j++)
{
if(bel[p]==bel[j]||bel[q]==bel[j])
{
continue;
}
if(x[p]==x[q])
{
lx=min(y[p],y[q]),rx=max(y[p],y[q]);
if(y[j]>lx&&y[j]<rx&&abs(x[j]-x[p])<=mid)
{
if(abs(y[j]-y[p]<=mid)&&abs(y[j]-y[q])<=mid)
{
return 1;
}
}
}
else
{
lx=min(x[p],x[q]),rx=max(x[p],x[q]);
if(x[j]>lx&&x[j]<rx&&abs(y[j]-y[p])<=mid)
{
if(abs(x[j]-x[p]<=mid)&&abs(x[j]-x[q])<=mid)
{
return 1;
}
}
}
}
}
return 0;
}
if(blk==4)
{
vector<Tuple>vx,vy;
for(register int i=1;i<=totr;i++)
{
for(register int j=1;j<row[rows[i]].size();j++)
{
p=row[rows[i]][j-1].y,q=row[rows[i]][j].y;
bel[p]!=bel[q]?vx.push_back((Tuple){p,q}):(void)1;
}
}
for(register int i=1;i<=totc;i++)
{
for(register int j=1;j<col[cols[i]].size();j++)
{
p=col[cols[i]][j-1].y,q=col[cols[i]][j].y;
bel[p]!=bel[q]?vy.push_back((Tuple){p,q}):(void)1;
}
}
for(register int i=0;i<vx.size();i++)
{
for(register int j=0;j<vy.size();j++)
{
p=vx[i].x,q=vx[i].y,r=vy[j].x,s=vy[j].y,lx=x[p],rx=y[r];
if(bel[p]==bel[r]||bel[p]==bel[s])
{
continue;
}
if(bel[q]==bel[r]||bel[q]==bel[s])
{
continue;
}
if(y[r]<=min(y[p],y[q])||y[r]>=max(y[p],y[q]))
{
continue;
}
if(x[p]<=min(x[r],x[s])||x[p]>=max(x[r],x[s]))
{
continue;
}
if(abs(rx-y[p])<=mid&&abs(rx-y[q])<=mid)
{
if(abs(lx-x[r])<=mid&&abs(lx-x[s])<=mid)
{
return 1;
}
}
}
}
return 0;
}
}
int main()
{
n=read(),r=2e9;
for(register int i=1;i<=n;i++)
{
rows[++totr]=x[i]=read(),cols[++totc]=y[i]=read();
row[x[i]].push_back((Tuple){y[i],i});
col[y[i]].push_back((Tuple){x[i],i});
}
sort(rows+1,rows+totr+1),sort(cols+1,cols+totc+1);
totr=unique(rows+1,rows+totr+1)-rows-1;
totc=unique(cols+1,cols+totc+1)-cols-1;
for(register int i=1;i<=totr;i++)
{
sort(row[rows[i]].begin(),row[rows[i]].end());
}
for(register int i=1;i<=totc;i++)
{
sort(col[cols[i]].begin(),col[cols[i]].end());
}
while(l<=r)
{
mid=(l+r)>>1;
check(mid)?res=mid,r=mid-1:l=mid+1;
}
printf("%d\n",res);
}

CodeForces 1419F Rain of Fire的更多相关文章

  1. Codeforces 988F Rain and Umbrellas(DP)

    题目链接:http://codeforces.com/contest/988/problem/F 题目大意: 有三个整数a,n,m,a是终点坐标,给出n个范围(l,r)表示这块区域下雨,m把伞(p,w ...

  2. Codeforces Round #436 E. Fire(背包dp+输出路径)

    题意:失火了,有n个物品,每个物品有价值pi,必须在时间di前(小于di)被救,否则就要被烧毁.救某个物 品需要时间ti,问最多救回多少价值的物品,并输出救物品的顺序. Examples Input ...

  3. Codeforces 988F. Rain and Umbrellas

    解题思路:动态规划 遍历点i,如果从前一个点i-1走到这个点i不需要伞,则疲劳值不变dp[i] = dp[i-1]. 如果前一个点i-1走到这一个点i需要伞,则从前面找一把伞. 即遍历前面的每个点j, ...

  4. CodeForces 题目乱做

    是个补题记录. 1419 除了 F 场上都过了. CF1419A Digit Game 这题好多人 FST 啊-- 考虑如果串长为奇数那么最后操作的肯定是第一个人,串长为偶数的最后操作的肯定是第二个, ...

  5. little alchemy攻略

    一个造物游戏: acidrain=rain+smoke airlplain=metal+bird alcohol=fruit+time algae=plant+water allergy=dust+h ...

  6. [Codeforces 864E]Fire

    Description Polycarp is in really serious trouble — his house is on fire! It's time to save the most ...

  7. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  8. D. Frets On Fire 【二分,前缀和】 (Codeforces Global Round 2)

    题目传送门:http://codeforces.com/contest/1119/problem/D D. Frets On Fire time limit per test 1.5 seconds ...

  9. Codeforces Round #436 (Div. 2) E. Fire

    http://codeforces.com/contest/864/problem/E 题意: 有一堆物品,每个物品有3个属性,需要的时间,失效的时间(一开始)和价值.只能一件一件的选择物品(即在选择 ...

随机推荐

  1. python-igraph

    linux安装python-igraph: $ sudo apt-get install -y libigraph0-dev $ pip install python-igraph ------for ...

  2. 使用内置对象Math.random实现猜数字游戏

    function getRandom(min,max){ return Math.floor(Math.random()*(max-min+1))+min;    //得到两个数之间的随机整数,包含实 ...

  3. 依赖注入在 dotnet core 中实现与使用:4. 集成 Autofac

    本示例使用 .net core 5 rc-1 实现. 1. 添加 Nuget 包引用 使用 Autofac 当然要添加 Autofac 的 Nuget 包,主要涉及到两个: Autofac.Exten ...

  4. Arduino Mega 2560

    Arduino Mega 2560 www.theengineeringprojects.com/ 此板子有54个引脚,16个模拟量输入引脚,12个PWM输出引脚,4个串口,带I2C,SPI通讯口,更 ...

  5. ElasticSearch 索引 VS MySQL 索引

    前言 这段时间在维护产品的搜索功能,每次在管理台看到 elasticsearch 这么高效的查询效率我都很好奇他是如何做到的. 这甚至比在我本地使用 MySQL 通过主键的查询速度还快. 为此我搜索了 ...

  6. c 判断端口是否已被使用

    isPortOpen.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include & ...

  7. 每日一题 LeetCode 491. 递增子序列 【递推】【递增子序列】【动态规划】

    题目链接 https://leetcode-cn.com/problems/increasing-subsequences/ 题目说明 题解 主要方法:递推:动态规划 解释说明: 数据表示:观察数据范 ...

  8. Flink深入浅出: 资源管理(v1.11)

    -- 图片来自 <国家地理中文网>-- 往期推荐: Flink深入浅出:部署模式 Flink深入浅出:内存模型 Flink深入浅出:JDBC Source从理论到实战 Flink深入浅出: ...

  9. Python+Appium自动化测试(13)-toast定位

    一,前言 在app自动化测试的过程中经常会遇到需要对toast进行定位,最常见的就是定位toast或者获取toast的文案进行断言,如下图,通过定位"登录成功"的toast就可以断 ...

  10. git克隆指定分支到本地

    我们每次使用命令 git clone https://xxx.com/android-app.git 默认 clone 的是这个仓库的 master 分支. 使用Git下载指定分支命令为:git cl ...