feat: ac and TLE solution implemented, updated problem input constraints.
This commit is contained in:
29
dominancia-de-pontos/src/TLE.cpp
Normal file
29
dominancia-de-pontos/src/TLE.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <bits/stdc++.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
int main(){
|
||||
// x_a < x_b
|
||||
// y_a < y_b
|
||||
|
||||
int n; cin >> n;
|
||||
vector<pair<long long, long long>> points(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> points[i].first >> points[i].second;
|
||||
}
|
||||
|
||||
long long count = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = i + 1; j < n; j++) {
|
||||
auto [x_a, y_a] = points[i];
|
||||
auto [x_b, y_b] = points[j];
|
||||
if (x_a < x_b && y_a < y_b || x_b < x_a && y_b < y_a) { // either A dominates B or B domites A
|
||||
count++;
|
||||
}
|
||||
}
|
||||
}
|
||||
cout << count << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -3,8 +3,61 @@
|
||||
using namespace std;
|
||||
|
||||
|
||||
// separate points into two sets
|
||||
// left and right sets
|
||||
vector<pair<long long, long long>> points;
|
||||
long long countDominantPairs(int l, int r) {
|
||||
if (l >= r) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int mid = (l + r)/2;
|
||||
long long count = countDominantPairs(l, mid) + countDominantPairs(mid + 1, r);
|
||||
|
||||
vector<pair<long long, long long>> temp(r - l + 1);
|
||||
int i = l, j = mid + 1, k = 0;
|
||||
while (i <= mid && j <= r) {
|
||||
auto [x_a, y_a] = points[i];
|
||||
auto [x_b, y_b] = points[j];
|
||||
|
||||
if (y_a < y_b) {
|
||||
temp[k++] = points[i++];
|
||||
} else { // y_a > y_b -> point B is dominated by all placed points from left side
|
||||
count += (i - l);
|
||||
temp[k++] = points[j++];
|
||||
}
|
||||
}
|
||||
|
||||
while (i <= mid) {
|
||||
temp[k++] = points[i++];
|
||||
}
|
||||
|
||||
while (j <= r) {
|
||||
count += (mid + 1 - l); // this point is dominated by all placed points from left side
|
||||
temp[k++] = points[j++];
|
||||
}
|
||||
|
||||
|
||||
for (int index = l; index <= r; index++) {
|
||||
points[index] = temp[index - l];
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int main(){
|
||||
// TODO
|
||||
cout << 0 << endl;
|
||||
// x_a < x_b
|
||||
// y_a < y_b
|
||||
|
||||
int n; cin >> n;
|
||||
points.resize(n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
cin >> points[i].first >> points[i].second;
|
||||
}
|
||||
|
||||
sort(points.begin(), points.end());
|
||||
|
||||
cout << countDominantPairs(0, n - 1) << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -30,16 +30,26 @@ vector<string> generate_sample_tests() {
|
||||
}
|
||||
|
||||
vector<string> generate_manual_tests() {
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({{1, 1}}));
|
||||
vector<string> tests;
|
||||
tests.push_back(output_tc({{-1, 3}, {1, 1}, {2, 4}, {3, 2}, {4, 5}})); // expected answer is 7
|
||||
tests.push_back(output_tc({{3, -6}, {4, 9}, {9, -9}, {1, -7}, {-3, 2}})); // expected answer is 4
|
||||
tests.push_back(output_tc({{-1, -3}, {-2, -2}, {-3, -1}, {1, 3}, {2, 2}, {3, 1}})); // expected answer is 9
|
||||
return tests;
|
||||
}
|
||||
|
||||
vector<pair<int,int>> generate_random_coords_array(int size) {
|
||||
vector<pair<int, int>> coords(size);
|
||||
for (int i = 0; i < size; i++) {
|
||||
coords[i].first = rnd.next(MIN_COORD, MAX_COORD);
|
||||
coords[i].second = rnd.next(MIN_COORD, MAX_COORD);
|
||||
vector<pair<int, int>> generate_random_points_array(int size) {
|
||||
set<int> usedX, usedY;
|
||||
vector<pair<int, int>> coords;
|
||||
|
||||
while (coords.size() < size) {
|
||||
int x = rnd.next(MIN_COORD, MAX_COORD);
|
||||
int y = rnd.next(MIN_COORD, MAX_COORD);
|
||||
|
||||
if (usedX.find(x) == usedX.end() && usedY.find(y) == usedY.end()) {
|
||||
usedX.insert(x);
|
||||
usedY.insert(y);
|
||||
coords.push_back({x, y});
|
||||
}
|
||||
}
|
||||
return coords;
|
||||
}
|
||||
@@ -56,7 +66,7 @@ string rnd_test(int i){
|
||||
}
|
||||
|
||||
int n = rnd.next(min_n, max_n);
|
||||
return(output_tc(generate_random_coords_array(n)));
|
||||
return(output_tc(generate_random_points_array(n)));
|
||||
}
|
||||
|
||||
vector<string> generate_random_tests() {
|
||||
@@ -68,7 +78,7 @@ vector<string> generate_random_tests() {
|
||||
}
|
||||
|
||||
string extreme_test_1(){
|
||||
return(output_tc(generate_random_coords_array(MAX_N)));
|
||||
return(output_tc(generate_random_points_array(MAX_N)));
|
||||
}
|
||||
|
||||
vector<string> generate_extreme_tests(){
|
||||
|
||||
@@ -12,11 +12,16 @@ int main(int argc, char* argv[]) {
|
||||
registerValidation(argc, argv);
|
||||
int n = inf.readInt(MIN_N, MAX_N, "n");
|
||||
inf.readEoln();
|
||||
unordered_set<int> seenX, seenY;
|
||||
for (int i = 0; i < n; i++) {
|
||||
inf.readInt(MIN_COORD, MAX_COORD, "xi");
|
||||
int x = inf.readInt(MIN_COORD, MAX_COORD, "xi");
|
||||
inf.readSpace();
|
||||
inf.readInt(MIN_COORD, MAX_COORD, "yi");
|
||||
int y = inf.readInt(MIN_COORD, MAX_COORD, "yi");
|
||||
inf.readEoln();
|
||||
ensuref(seenX.count(x) == 0, "Repeated point X coordinate is not allowed");
|
||||
ensuref(seenY.count(y) == 0, "Repeated point Y coordinate is not allowed");
|
||||
seenX.insert(x);
|
||||
seenY.insert(y);
|
||||
}
|
||||
inf.readEof();
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user