当前位置:网站首页>Luogu p1514 [noip2010 improvement group] water diversion into the city
Luogu p1514 [noip2010 improvement group] water diversion into the city
2022-06-21 22:09:00 【q779】
Luogu P1514 [NOIP2010 Improvement group ] Water into the city Answer key
Topic link :P1514 [NOIP2010 Improvement group ] Water into the city
The question :
In a distant country , On one side is a beautiful lake , On the other side is the boundless desert . The administrative division of the country is very special , It just makes up a N N N That's ok × M \times M ×M Column rectangle , As shown in the figure above , Each grid represents a city , Every city has an altitude .
In order to make the residents drink the clear lake water as much as possible , Now we need to build water conservancy facilities in some cities . There are two kinds of water conservancy facilities , They are water storage plant and water delivery station . The function of the water storage plant is to pump water from the lake to the reservoir of the city .
therefore , There's only the second one adjacent to the lake 1 1 1 Yes, cities can build water storage plants . The function of the water delivery station is to use the height drop through the water delivery pipeline , Transport water from high to low . Therefore, the premise for a city to build a water conveyance station , It's an adjacent city with a higher altitude and a public edge , Water conservancy facilities have been built . Due to the first N N N The city is near the desert , It's the arid part of the country , So every city is required to have water conservancy facilities . that , Can this requirement be met ? If you can , Please calculate the minimum number of water storage plants to be built ; If not , Find out the number of cities in arid areas that can't have water conservancy facilities .
First of all, we can think of running directly at each shore point dfs
At the same time, record the number of cities that each point can cover
What are the characteristics of a city that can be covered by one point
If there is any solution , So what any water storage plant can cover is the line segment
This is not easy to think of , But it's easy to prove
Here's a mouthful of proof
Consider counter evidence
Set up a water storage plant u u u The leftmost and rightmost cities that can be covered are l , r l,r l,r
be u , l , r u,l,r u,l,r The three points must form a closed figure similar to a triangle
Obviously, if other water storage plants are to be covered [ l , r ] [l,r] [l,r] In the point , It must pass through one of the sides of the triangle
If a point x ∈ [ l , r ] x \in [l,r] x∈[l,r] It cannot be covered by water flowing from any point in the triangle
Then, no matter where the water from the outside flows through the triangle , Can not flow to x x x , Then there is no solution
Therefore, when there is a solution, any city that can be covered by the water storage plant forms a line segment
Know the line segment , How to calculate the answer ?
We can set up a L L L Indicates the location of the city currently covered ( Cover from left to right )
And the left endpoint of any line segment l i ≤ L l_i\le L li≤L Can make L L L Move right to r i r_i ri
So we greedily choose all the satisfaction l i ≤ L l_i\le L li≤L The largest of the line segments of r i r_i ri , To update L L L
And then it's gone
Time complexity O ( n m ) O(nm) O(nm)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(1e3+15)
int n,m;
int dx[5]={
0,0,1,-1};
int dy[5]={
1,-1,0,0};
int vis[N][N],h[N][N],l[N][N],r[N][N];
bool safe(int x,int y)
{
return 1<=x&&x<=n&&1<=y&&y<=m;}
void dfs(int x,int y)
{
vis[x][y]=1;
for(int i=0; i<4; i++)
{
int tx=x+dx[i];
int ty=y+dy[i];
if(!safe(tx,ty)||h[tx][ty]>=h[x][y])continue;
if(!vis[tx][ty])dfs(tx,ty);
l[x][y]=min(l[x][y],l[tx][ty]);
r[x][y]=max(r[x][y],r[tx][ty]);
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
memset(l,0x3f,sizeof(l));
cin >> n >> m;
for(int i=1; i<=m; i++)
l[n][i]=r[n][i]=i;
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)
cin >> h[i][j];
for(int i=1; i<=m; i++)
if(!vis[1][i])dfs(1,i);
bool ok=1;int res=0;
for(int i=1; i<=m; i++)
if(!vis[n][i]) ok=0;++res;
if(!ok)
{
cout << "0\n" << res << '\n';
return 0;
}
int L=1,R=r[1][1];
while(L<=m)
{
for(int i=1; i<=m; i++)
if(l[1][i]<=L)
R=max(R,r[1][i]);
L=R+1; ++res;
}
cout << "1\n" << res << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- 央企国电集团上海翔伟机电和中外海达成战略合作,捐赠2亿
- Huawei Hongmeng development lesson 3
- How to write the title of popular popular items in our media video
- How to associate the QR code of wechat applet to realize the integration of two codes
- Pinduoduo 618 mobile phone brand official flag sales increased by 124% year-on-year, and 4000+ high-priced mobile phones increased by 156% year-on-year
- Is the security of the securities account given by digging money safe? Can I open an account?
- 华为鸿蒙开发第三课
- What is the standard format for writing citations in academic papers?
- MySQL创建表时的条件有哪些
- 洛谷P1608 路径统计 题解
猜你喜欢

How to check the duplicate of an English paper?

British teddy bear joins the pubg mobile game

#15迭代器

基于OpenCVSharp的001新建工程项目

LeetCode508-出现次数最多的子树元素和-深搜

Intelij idea efficient skills (II)

Tutorial on the implementation of smart contracts by solidity (4) -erc1155 contracts

Spark 离线开发框架设计与实现
![P6758 [BalticOI2013] Vim](/img/07/b16c8d329b33424e931d9eaedae73a.png)
P6758 [BalticOI2013] Vim
![Time modification method for search device of Jerry's Bluetooth transmitter [chapter]](/img/ac/5a1d8cabbc61b24d451753e2f5c8dd.png)
Time modification method for search device of Jerry's Bluetooth transmitter [chapter]
随机推荐
In the anchoring stage of retail digitalization, more attention is paid to how to mine and transform traffic by means of Digitalization
你真的了解二叉树吗?(上篇)
Utilisation de la combinaison d'assertions de l'API Stream et de la mise en cache locale pour les requêtes floues (près de 1000 fois plus efficace que MySQL)
Advanced packaging, the beginning of a big cycle -- a symposium on semiconductor equipment
What are the authoritative websites that search English documents like CNKI?
What is the standard format for writing citations in academic papers?
棋牌类游戏
洛谷P1608 路径统计 题解
Spark 离线开发框架设计与实现
As we media, why can some people earn more than 10000 yuan a month, but you can only earn a few yuan a month?
Jerry wants to keep both earphones output stereo after pairing them [chapter]
全屋智能家居品牌“智汀科技”有体验中心?
Enzo high sensitivity detection Arg8 vasopressin ELISA Kit
Guangdong CDC reminds: the summer vacation is coming, so the returned college students can "return home" safely
Enterprise data leakage prevention solution sharing
HiC-Pro | HiC数据处理工具
Worthington弹性蛋白酶背景和特异性介绍
Worthington胶原蛋白酶原料
Eureka控制台访问微服务暴露的info端点
搭建Eureka-Server集群

