Farmer John is planning to build?NN?(1≤N≤1051≤N≤105) farms that will be connected by?N?1N?1?roads, forming a tree. Typically, whenever one of his farms is having an issue he is not told the specific farm that is having an issue. Instead, he is told that one of the farms along the path from some farm?AA?to another farm?BB?is having an issue. This is often confusing for Farmer John, as he usually drives offroad tractors and isn't familiar with the road system.
Farmer John considers the location of a farm to be a 2D point. He would prefer to be told that there is a problem in one of the farms in a specified axis-aligned rectangular box. This way Farmer John can decide for himself how to navigate between the farms. Bessie told him that this is a little too ambitious, so he will be satisfied if he is notified with at most two axis-aligned rectangular boxes whose intersection (of farms) is empty and whose union is exactly the farms along the path from?AA?to?BB. You must help Farmer John determine where he should build his farms such that this condition is satisfied.
This is an interactive problem, you will not be using standard (or file) I/O.?Solutions that use standard (or file) I/O will be disqualified.?However, you ARE ALLOWED to use global and static variables. You must implement the following functions to help Farmer John:
Your implementation of the above functions will be able to call the functions given below. You may assume that?notifyFJnotifyFJ?will be called?QQ?times (1≤Q≤1051≤Q≤105).
The interactive protocol works as follows. First,?addRoadaddRoad?will be called?N?1N?1?times, to inform your program of the road system. Then,?buildFarmsbuildFarms?will be called and you must determine where Farmer John should build each farm and call?setFarmLocationsetFarmLocationfor every farm accordingly. Finally, there will be?QQ?calls to?notifyFJnotifyFJ?where you must make either one or two calls to?addBoxaddBox?to notify Farmer John.
It is guaranteed there is always a valid way to notify Farmer John using either one or two boxes. The memory limit for this problem is set to 512MB, above the usual 256MB limit.
For a C++ solution, use this template:
#include "grader.h"
void addRoad(int a, int b){
// Fill in code here
}
void buildFarms(){
// Fill in code here
}
void notifyFJ(int a, int b){
// Fill in code here
}
For a Java solution, use this template:
import java.io.IOException;
// If you find it necessary, you may import other standard libraries here.
public class boxes extends Grader {
// Copy this exactly:
@Override
public static void main(String args[]) throws IOException { new boxes().run(); }
@Override
public void addRoad(int a, int b) {
// Fill in code here
}
@Override
public void buildFarms(){
// Fill in code here
}
@Override
public void notifyFJ(int a, int b){
// Fill in code here
}
}
Sample Interaction
Grader calls?addRoad(0,1)addRoad(0,1)
Grader calls?addRoad(1,2)addRoad(1,2)
Grader calls?buildFarms()buildFarms()
Solution calls?setFarmLocation(0,1,1)setFarmLocation(0,1,1)
Solution calls?setFarmLocation(1,1,2)setFarmLocation(1,1,2)
Solution calls?setFarmLocation(2,2,2)setFarmLocation(2,2,2)
Solution ends?buildFarms()buildFarms()
Grader calls?notifyFJ(0,0)notifyFJ(0,0)
Solution calls?addBox(1,1,1,1)addBox(1,1,1,1)
Solution ends?notifyFJ(0,0)notifyFJ(0,0)
Grader calls?notifyFJ(0,2)notifyFJ(0,2)
Solution calls?addBox(1,1,1,2)addBox(1,1,1,2)
Solution calls?addBox(2,2,2,2)addBox(2,2,2,2)
Solution ends?notifyFJ(0,2)notifyFJ(0,2)
Grader terminates, and solution passes test-case
(Note: if you do not pass the first test case, the grader will indicate this as usual. However, note that the short sample interaction above does not correspond to the first test case or any other).
Problem credits: Spencer Compton
中文版
Farmer John計(jì)劃建造NN(1≤N≤1051≤N≤105)個(gè)農(nóng)場,由N?1N?1條道路連通,構(gòu)成一棵樹。通常,當(dāng)他的一個(gè)農(nóng)場出現(xiàn)問題時(shí),他不會(huì)被告知具體出現(xiàn)了問題的農(nóng)場。取而代之的是,他會(huì)被告知從某個(gè)農(nóng)場AA到某個(gè)農(nóng)場BB的路徑上的農(nóng)場之一出現(xiàn)了問題。這經(jīng)常使Farmer John感到困惑,因?yàn)樗綍r(shí)都駕駛越野拖拉機(jī),所以對道路系統(tǒng)并不熟悉。
Farmer John將農(nóng)場的位置視為二維坐標(biāo)系中的點(diǎn)。他更希望被告知出現(xiàn)問題的是某個(gè)四邊平行于坐標(biāo)軸的長方形區(qū)域中的某個(gè)農(nóng)場。這樣Farmer John就可以自己決定如何在農(nóng)場之間通行。Bessie告訴他這有些太困難了,所以只要他被告知兩個(gè)不包含相同農(nóng)場并且總共包含了沿AA到BB路徑上的所有農(nóng)場的四邊與坐標(biāo)軸平行的長方形,他就滿意了。你需要幫助Farmer John決定他應(yīng)當(dāng)建造他的農(nóng)場的位置,使得這一條件被滿足。
這是一道交互題,你不可以使用標(biāo)準(zhǔn)(或文件)輸入輸出。?使用標(biāo)準(zhǔn)(或文件)輸入輸出的程序?qū)?huì)被取消成績。?然而,你可以使用全局或是靜態(tài)變量。為了幫助Farmer John,你需要實(shí)現(xiàn)如下函數(shù):
你對上述函數(shù)的實(shí)現(xiàn)中可以調(diào)用下面給出的函數(shù)。假設(shè)notifyFJnotifyFJ會(huì)被調(diào)用QQ次。
交互方式如下。首先,addRoadaddRoad會(huì)被調(diào)用N?1N?1次,用來告知你的程序道路系統(tǒng)。接下來,buildFarmsbuildFarms會(huì)被調(diào)用,你需要確定Farmer John需要建造每個(gè)農(nóng)場的位置,并且相應(yīng)地為每個(gè)農(nóng)場調(diào)用setFarmLocationsetFarmLocation。最后,會(huì)有QQ次對notifyFJnotifyFJ的調(diào)用,在每次調(diào)用中你必須調(diào)用一次或兩次addBoxaddBox來告知Farmer John。
保證始終存在一種符合條件的方式可以使用一個(gè)或兩個(gè)長方形來告知Farmer John。這個(gè)問題的運(yùn)行內(nèi)存限制為512MB,超過一般問題所給的256MB內(nèi)存限制。
C++的程序請使用下面的模板:
#include "grader.h"
void addRoad(int a, int b){
// Fill in code here
}
void buildFarms(){
// Fill in code here
}
void notifyFJ(int a, int b){
// Fill in code here
}
Java的程序請使用下面的模板:
import java.io.IOException;
// If you find it necessary, you may import other standard libraries here.
public class boxes extends Grader {
// Copy this exactly:
Override
public static void main(String args[]) throws IOException { new boxes().run(); }
Override
public void addRoad(int a, int b) {
// Fill in code here
}
Override
public void buildFarms(){
// Fill in code here
}
Override
public void notifyFJ(int a, int b){
// Fill in code here
}
}
交互樣例:
評測機(jī)調(diào)用addRoad(0,1)addRoad(0,1)
評測機(jī)調(diào)用addRoad(1,2)addRoad(1,2)
評測機(jī)調(diào)用buildFarms()buildFarms()
選手程序調(diào)用setFarmLocation(0,1,1)setFarmLocation(0,1,1)
選手程序調(diào)用setFarmLocation(1,1,2)setFarmLocation(1,1,2)
選手程序調(diào)用setFarmLocation(2,2,2)setFarmLocation(2,2,2)
選手程序結(jié)束buildFarms()buildFarms()
評測機(jī)調(diào)用notifyFJ(0,0)notifyFJ(0,0)
選手程序調(diào)用addBox(1,1,1,1)addBox(1,1,1,1)
選手程序結(jié)束notifyFJ(0,0)notifyFJ(0,0)
評測機(jī)調(diào)用notifyFJ(0,2)notifyFJ(0,2)
選手程序調(diào)用addBox(1,1,1,2)addBox(1,1,1,2)
選手程序調(diào)用addBox(2,2,2,2)addBox(2,2,2,2)
選手程序結(jié)束notifyFJ(0,2)notifyFJ(0,2)
評測結(jié)束,選手程序通過測試用例
供題:Spencer Compton

? 2025. All Rights Reserved. 滬ICP備2023009024號-1