-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC++.cpp
124 lines (103 loc) · 2.31 KB
/
C++.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/*
Structure pour stocker une pomme:
y: ligne dans la map
x: colonne dans la map
name: nom de la pomme
print: afficher les informations de la pomme pour debug
*/
struct Apple
{
public:
int y;
int x;
string name;
Apple(int y, int x, string name) : y(y), x(x), name(name)
{
}
void print()
{
cerr << "Y: " << this->y << " X: " << this->x << " Name: " << this->name << endl;
}
};
// Recherche binaire pour trouver l'élément le plus a droite pour une ligne donné
int binarySearch(vector<Apple> &apples, int row)
{
int start = 0;
int end = apples.size() - 1;
while (start <= end)
{
int mid = end - (end - start) / 2;
if (apples[mid].y <= row)
{
start = mid + 1;
}
else
{
end = mid - 1;
}
}
return end;
}
int main()
{
vector<Apple> apples;
// Récupération des pommes
int n;
cin >> n;
cin.ignore();
for (int i = 0; i < n; i++)
{
string name;
int r;
int c;
cin >> name >> r >> c;
cin.ignore();
apples.push_back({r, c, name});
}
// Trie des pommes
sort(apples.begin(), apples.end(), [](Apple &a, Apple &b) {
if (a.y == b.y)
{
return a.x < b.x;
}
return a.y < b.y;
});
/*for(Apple &a : apples){
a.print();
}*/
string output = "";
bool d = false;
int lastRow = -1;
// Création du string de sortie
for (int i = 0; i < apples.size(); i++)
{
// Les lignes avec la pommes précédente sont différente, donc on inverse le sens du parcours
if (lastRow != apples[i].y)
{
d = !d;
lastRow = apples[i].y;
}
// Sens de parcours de gauche à droite
if (d)
{
output += apples[i].name + ',';
}
// Sens de parcours de droite à gauche
else
{
int last = binarySearch(apples, lastRow);
for (int _i = last; _i >= i; _i--)
{
output += apples[_i].name + ',';
}
i = last;
}
}
output.pop_back();
cout << output << endl;
}