1098. Cavern Construction (20 points)

Is it possible at all to send a long pipe with a diameter of at least 1 unit horizontally into a cavern with complex terrain? The two pictures below show the section of the cave, and the dark blue broken line outlines the top and bottom of the cave. Figure 1 is possible, the green part shows that a pipe of diameter 1 can be fed. Figure 2 is not possible unless the top or bottom overhang is shaved off by 1 unit of height.

In this question, you are asked to write a program to judge whether construction is possible in a given cave.

Input format:

Enter a positive integer N not exceeding 100 in the first line, which is the number of horizontal sampling points. The following two lines of data give the ordinates of the sampling points sequentially from left to right: the first line is the sampling point at the top of the cave, and the second line is the sampling point at the bottom of the cave. Here it is assumed that the coordinate origin is at the lower left corner, and each ordinate is a non-negative integer not exceeding 1000. Numbers in the same row are separated by spaces.

The title ensures that the input data is reasonable, that is, the contour line at the bottom of the cave will not cross the top contour line.

Output format:

Yes If direct construction is possible, output and the maximum diameter of the pipe that can be fed  in one line  ; if not, output No and at least the height that needs to be cut. Answers and numbers are separated by 1 space.

Input sample 1:

11
7 6 5 5 6 5 4 5 5 4 4
3 2 2 2 2 3 3 2 1 2 3

Output sample 1:

Yes 1

Input sample 2:

11
7 6 5 5 6 5 4 5 5 4 4
3 2 2 2 3 4 3 2 1 2 3

Output sample 2:

No 1

This topic is very simple, just record the lowest point of the upper edge and the highest point of the lower edge when inputting. Whether the conditions are satisfied can be judged by comparison.

#include<cstdio>
#include<cmath> 
#include<iostream>
using namespace std;

int main(){
	int N,num,TopMin=1005,lowMax=0;
	cin>>N;
	for(int i=1;i<=N;i++){
		cin>>num;
		if(num<TopMin)
			TopMin=num;
	}
	for(int i=1;i<=N;i++){
		cin>>num;
		if(num>lowMax)
			lowMax=num;			
	}
	//如果上沿的最低点 大于 下沿最高点 那么可以修管道,他们的差值就是管道的最大宽度 
	if(TopMin>lowMax){
		cout<<"Yes "<<TopMin-lowMax;
	}else{
		//如果不满足 则比较 上沿的最低点和 下沿的最高点 之间的差值+1 就是需要削减的高度 管道宽度至少为 1 
		cout<<"No "<<(lowMax-TopMin+1); 
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_16382227/article/details/124033711