Giáo sư Clumsey sẽ có buổi thuyết trình quan trọng vào chiều nay. Chẳng may thay, ông
là người không mấy gọn gàng và đã để các bản thuyết trình (slides) trong suốt trong một
bao lớn lộn xộn (heap). Trước buổi thuyết trình, ông phải sắp xếp lại các slides này. Ông
muốn các thao tác thực hiện là tối thiểu.
Tất cả các slides được khắc số cho biết thứ tự của chúng. Vì các slides nằm chồng lên
nhau và trong suốt, nên chúng ta không thể xác định được số được khắc trên slide nào.
Chúng ta không thể thấy được một số được khắc trên chính xác một slide nào, nhưng có
thể kết luận được các số thuộc về các slides nào (nếu khó hiểu hãy xem ví dụ và tham
khảo bản gốc). Ví dụ nếu chúng ta đánh dấu các slides bằng các ký tự A, B, C... như hình
trên thì rõ ràng là D có số thứ tự là 3, B là 1, C là 2 và A là 4.
Nhiệm vụ của bạn là viết một chương trình tự động hóa việc xử lý này.
Input
Input gồm nhiều mô tả heap, mỗi mô tả bắt đầu bằng dòng chứa số nguyên n, số lượng
sildes trong heap. Mỗi dòng trong n dòng tiếp theo chứa 4 số nguyên xmin, xmax, ymin và
ymax cho biết tọa độ của sildes. Các Sildes được đánh dấu A, B, C... theo thứ tự.
N dòng tiếp theo, mỗi dòng chứa 2 số nguyên, tọa độ (x, y) của n số được khắc trên lần
lượt các sildes theo thứ tự input. Không một số nào nằm trên đường biên của silde.
Input kết thúc bởi một mô tả heap có n = 0; không xử lý trường hợp này.
Ouptput
Với mỗi một mô tả heap trong input, trước hết xuất ra số thứ tự của heap. Sau đó in ra
một dãy các slides mà số thứ tự của nó có thể được xác định duy nhất. Sắp xếp các cặp
theo thứ tự A, B, C, ...
Nếu không xác định được một bộ ghép nào thì in ra “none”
Xuất một dòng trắng cho mỗi trường hợp.
DOANSTT.INP DOANSTT.OUT
4
6 22 10 20
4 18 6 16
8 20 2 18
10 24 4 8
9 15
19 17
11 7
21 11
2
0 2 0 2
0 2 0 2
1 1
1 1
0
Heap 1
(A,4) (B,1) (C,2) (D,3)
Heap 2
none
give me the c++ solution
Assistant
Here's a C++ solution to the problem where we need to determine which slides have uniquely identifiable numbers based on their positions and the positions of the numbers on them.
Approach:
Input Processing:
We read multiple test cases until n = 0 is encountered.
For each test case, we first read the number of slides n.
Then, for each slide, we store its coordinates and label it sequentially as A, B, C, etc.
After that, we read the coordinates of each number associated with the slides.
Possible Assignments:
For each slide, determine which numbers' coordinates lie within the slide's boundaries.
Conversely, for each number, determine which slides contain its coordinates.
Store these relationships in possible_numbers for slides and possible_slides for numbers.
Constraint Propagation:
Implement a constraint propagation mechanism to iteratively reduce the possible number assignments for each slide by:
Assigning a number to a slide if there's only one possible number it can take.
Removing that number from the possibilities of other slides.
Similarly, if a number can only belong to one slide, assign it accordingly and update possibilities.
This process continues until no further assignments can be made.
Output Generation:
After constraint propagation, check which slides have exactly one possible number.
If such assignments exist, list them in the specified format.
If no slides have uniquely identifiable numbers, output none.
Edge Cases:
Handle cases where slides or numbers can belong to multiple entities without unique assignments.
C++ Solution:
cpp
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;
int heap_num =1;
while(cin>>n){
if(n ==0){
break;
}
// Read slides
struct Slide {
int xmin, xmax, ymin, ymax;
};
vector<Slide> slides(n);
for(int i=0;i<n;i++){
cin>>slides[i].xmin>>slides[i].xmax>>slides[i].ymin>>slides[i].ymax;
}
// Read numbers
struct Number {
int x, y;
};
vector<Number> numbers(n);
for(int i=0;i<n;i++){
cin>>numbers[i].x>>numbers[i].y;
}
// Initialize possible_numbers and possible_slides
vector< vector<int> > slide_possible_numbers(n, vector<int>());
vector< vector<int> > number_possible_slides(n, vector<int>());
for(int s=0;s<n;s++){
for(int m=0;m<n;m++){
if(numbers[m].x > slides[s].xmin && numbers[m].x < slides[s].xmax
&& numbers[m].y > slides[s].ymin && numbers[m].y < slides[s].ymax){
slide_possible_numbers[s].push_back(m);
number_possible_slides[m].push_back(s);
}
}
}
// Initialize queue
// Type 0: slide, Type1: number
queue<pair<int, int>> q;
// To keep track of what's already in queue
vector<bool> in_queue_slide(n, false);
vector<bool> in_queue_number(n, false);
for(int s=0;s<n;s++){
if(slide_possible_numbers[s].size() ==1){
q.emplace(0, s);
in_queue_slide[s] = true;
}
}
for(int m=0;m<n;m++){
if(number_possible_slides[m].size() ==1){
q.emplace(1, m);
in_queue_number[m] = true;
}
}
// Start constraint propagation
while(!q.empty()){
pair<int, int> current = q.front();
q.pop();
if(current.first ==0){
int s = current.second;
if(slide_possible_numbers[s].size() !=1){
continue;
}
int m = slide_possible_numbers[s][0];
// Assign m to s, remove m from other slides
for(int s_prime=0;s_prime<n;s_prime++){
if(s_prime !=s){
// Check if m is in possible_numbers[s_prime]
auto &vec = slide_possible_numbers[s_prime];
auto it = find(vec.begin(), vec.end(), m);
if(it != vec.end()){
vec.erase(it);
if(vec.size() ==1 && !in_queue_slide[s_prime]){
q.emplace(0, s_prime);
in_queue_slide[s_prime] = true;
}
}
}
}
// Remove s from possible_slides of m
// Since m is assigned to s, it cannot be in other slides
// But already handled above by removing m from other slides
// Additionally, for number m, possible_slides[m] should now be only s
// So to ensure numbers_possible_slides[m] has only s
// But m is already only assigned to s
// So we do not need to enqueue it again
}
else{
// Type 1: number
int m = current.second;
if(number_possible_slides[m].size() !=1){
continue;
}
int s = number_possible_slides[m][0];
// Assign m to s, remove s from other numbers
for(int m_prime=0;m_prime<n;m_prime++){
if(m_prime !=m){
// Check if s is in possible_slides[m_prime]
auto &vec = number_possible_slides[m_prime];
auto it = find(vec.begin(), vec.end(), s);
if(it != vec.end()){
vec.erase(it);
if(vec.size() ==1 && !in_queue_number[m_prime]){
q.emplace(1, m_prime);
in_queue_number[m_prime] = true;
}
}
}
}
// Remove m from possible_numbers of s
// Since s is assigned to m, it cannot have other numbers
// But s has only m as possible number
// So remove s from possible_slides of other numbers
for(int m_prime=0;m_prime<n;m_prime++){
if(m_prime !=m){
// Check if s is in possible_slides[m_prime]
auto &vec = number_possible_slides[m_prime];
auto it = find(vec.begin(), vec.end(), s);
if(it != vec.end()){
vec.erase(it);
if(vec.size() ==1 && !in_queue_number[m_prime]){
q.emplace(1, m_prime);
in_queue_number[m_prime] = true;
}
}
}
}
// Additionally, remove m from possible_numbers of other slides
for(int s_prime=0;s_prime<n;s_prime++){
if(s_prime !=s){
auto &vec = slide_possible_numbers[s_prime];
auto it = find(vec.begin(), vec.end(), m);
if(it != vec.end()){
vec.erase(it);
if(vec.size() ==1 && !in_queue_slide[s_prime]){
q.emplace(0, s_prime);
in_queue_slide[s_prime] = true;
}
}
}
}
}
}
// After propagation, collect uniquely determined slides
vector<pair<char, int>> determined;
for(int s=0;s<n;s++){
if(slide_possible_numbers[s].size() ==1){
int m = slide_possible_numbers[s][0];
determined.emplace_back(make_pair('A' + s, m+1));
}
}
// Output
cout<<"Heap "<<heap_num<<"\n";
if(determined.empty()){
cout<<"none\n";
}
else{
// Sort determined by slide label
sort(determined.begin(), determined.end());
for(int i=0;i<determined.size();i++){
if(i>0){
cout<<" ";
}
cout<<"("<<determined[i].first<<","<<determined[i].second<<")";
}
cout<<"\n";
}
cout<<"\n";
heap_num++;
}
}
Explanation:
Data Structures:
Slide & Number Structures: To store the coordinates of each slide and number.
Possible Assignments: Two vectors of vectors to maintain possible number assignments for each slide (slide_possible_numbers) and possible slide assignments for each number (number_possible_slides).
Constraint Propagation:
A queue is used to keep track of slides and numbers that have only one possible assignment.
When a slide or number is processed, it updates the possible assignments of other slides and numbers accordingly.
This continues until no further assignments can be made.
Determination of Unique Assignments:
After constraint propagation, if a slide has exactly one possible number, it is uniquely determined and stored for output.
If no such assignments exist, none is printed.
Output Formatting:
Each heap's assignments are printed in the required format, ensuring slides are listed in alphabetical order.
An empty line is printed after each heap's result for clarity.