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計劃建造NN(1≤N≤1051≤N≤105)個農場,由N?1N?1條道路連通,構成一棵樹。通常,當他的一個農場出現問題時,他不會被告知具體出現了問題的農場。取而代之的是,他會被告知從某個農場AA到某個農場BB的路徑上的農場之一出現了問題。這經常使Farmer John感到困惑,因為他平時都駕駛越野拖拉機,所以對道路系統并不熟悉。
Farmer John將農場的位置視為二維坐標系中的點。他更希望被告知出現問題的是某個四邊平行于坐標軸的長方形區域中的某個農場。這樣Farmer John就可以自己決定如何在農場之間通行。Bessie告訴他這有些太困難了,所以只要他被告知兩個不包含相同農場并且總共包含了沿AA到BB路徑上的所有農場的四邊與坐標軸平行的長方形,他就滿意了。你需要幫助Farmer John決定他應當建造他的農場的位置,使得這一條件被滿足。
這是一道交互題,你不可以使用標準(或文件)輸入輸出。?使用標準(或文件)輸入輸出的程序將會被取消成績。?然而,你可以使用全局或是靜態變量。為了幫助Farmer John,你需要實現如下函數:
你對上述函數的實現中可以調用下面給出的函數。假設notifyFJnotifyFJ會被調用QQ次。
交互方式如下。首先,addRoadaddRoad會被調用N?1N?1次,用來告知你的程序道路系統。接下來,buildFarmsbuildFarms會被調用,你需要確定Farmer John需要建造每個農場的位置,并且相應地為每個農場調用setFarmLocationsetFarmLocation。最后,會有QQ次對notifyFJnotifyFJ的調用,在每次調用中你必須調用一次或兩次addBoxaddBox來告知Farmer John。
保證始終存在一種符合條件的方式可以使用一個或兩個長方形來告知Farmer John。這個問題的運行內存限制為512MB,超過一般問題所給的256MB內存限制。
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
}
}
交互樣例:
評測機調用addRoad(0,1)addRoad(0,1)
評測機調用addRoad(1,2)addRoad(1,2)
評測機調用buildFarms()buildFarms()
選手程序調用setFarmLocation(0,1,1)setFarmLocation(0,1,1)
選手程序調用setFarmLocation(1,1,2)setFarmLocation(1,1,2)
選手程序調用setFarmLocation(2,2,2)setFarmLocation(2,2,2)
選手程序結束buildFarms()buildFarms()
評測機調用notifyFJ(0,0)notifyFJ(0,0)
選手程序調用addBox(1,1,1,1)addBox(1,1,1,1)
選手程序結束notifyFJ(0,0)notifyFJ(0,0)
評測機調用notifyFJ(0,2)notifyFJ(0,2)
選手程序調用addBox(1,1,1,2)addBox(1,1,1,2)
選手程序調用addBox(2,2,2,2)addBox(2,2,2,2)
選手程序結束notifyFJ(0,2)notifyFJ(0,2)
評測結束,選手程序通過測試用例
供題:Spencer Compton

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