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
© 2024. All Rights Reserved. 沪ICP备2023009024号-1