// #include "citymapping.h"
#include <bits/stdc++.h>
#include "citymapping.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
const int MAXN = 1000;
int t = 0;
mt19937 rng(6);
ll dist[2][MAXN];
class bucket {
public:
vector<int> elems;
int path_elem;
int min_elem;
ll mn_dist;
bool m;
bucket() {}
bucket(int path, bool mn = 1) {
min_elem = -1;
path_elem = path;
m = mn;
mn_dist = 0;
}
void add(int i, ll d) {
if (m && (min_elem == -1 || mn_dist > d)) {
min_elem = i;
mn_dist = d;
}
elems.push_back(i);
}
};
void make_ans(vector<int> &vals, int a[], int b[], int w[]) {
if (vals.size() < 2) return;
if (vals.size() == 2) {
a[t] = vals[0];
b[t] = vals[1];
w[t] = get_distance(vals[0], vals[1]);
t++;
return;
}
int n = vals.size();
int f = rng()%n;
int mx = -1;
for (int i = 0; i < n; i++) {
if (i == f) continue;
dist[0][i] = get_distance(vals[i], vals[f]);
if (mx == -1 || dist[0][i] > dist[0][mx]) mx = i;
}
dist[0][f] = 0;
for (int i = 0; i < n; i++) {
if (i == f || i == mx) continue;
dist[1][i] = get_distance(vals[i], vals[mx]);
}
dist[1][mx] = 0;
dist[1][f] = dist[0][mx];
vector<pli> path;
map<ll, bucket> buckets;
for (int i = 0; i < n; i++) {
if (dist[0][i]+dist[1][i] == dist[0][mx]) {
path.push_back(pli(dist[0][i], i));
buckets[dist[1][i]-dist[0][i]] = bucket(i, (i != f && i != mx));
}
}
sort(path.begin(), path.end());
for (int i = 0; i < path.size()-1; i++) {
a[t] = vals[path[i].second];
b[t] = vals[path[i+1].second];
w[t] = dist[0][path[i+1].second]-dist[0][path[i].second];
t++;
}
for (int i = 0; i < n; i++) {
if (dist[0][i]+dist[1][i] != dist[0][mx] || i == mx || i == f)
buckets[dist[1][i]-dist[0][i]].add(vals[i], dist[0][i]);
}
for (auto it: buckets) {
bucket bu = it.second;
make_ans(bu.elems, a, b, w);
if (bu.m && bu.min_elem != -1) {
a[t] = vals[bu.path_elem];
b[t] = bu.min_elem;
w[t] = bu.mn_dist-dist[0][bu.path_elem];
assert(w[t] > 0);
t++;
}
}
}
void find_roads(int n, int q, int a[], int b[], int w[]) {
vector<int> v(n);
iota(v.begin(), v.end(), 1);
make_ans(v, a, b, w);
assert(t == n-1);
// for (int i = 0; i < t; i++) assert(w[t] >= 0);
return;
}
Compilation message
citymapping.cpp: In function 'void make_ans(std::vector<int>&, int*, int*, int*)':
citymapping.cpp:72:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for (int i = 0; i < path.size()-1; i++) {
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Correct: 2426 out of 500000 queries used. |
2 |
Runtime error |
2 ms |
852 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Correct: 2426 out of 500000 queries used. |
2 |
Runtime error |
2 ms |
852 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Correct: 2124 out of 12000 queries used. |
2 |
Correct |
1 ms |
596 KB |
Correct: 2714 out of 12000 queries used. |
3 |
Correct |
1 ms |
596 KB |
Correct: 2650 out of 12000 queries used. |
4 |
Correct |
1 ms |
596 KB |
Correct: 2801 out of 12000 queries used. |
5 |
Correct |
1 ms |
596 KB |
Correct: 2606 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Correct: 2124 out of 12000 queries used. |
2 |
Correct |
1 ms |
596 KB |
Correct: 2714 out of 12000 queries used. |
3 |
Correct |
1 ms |
596 KB |
Correct: 2650 out of 12000 queries used. |
4 |
Correct |
1 ms |
596 KB |
Correct: 2801 out of 12000 queries used. |
5 |
Correct |
1 ms |
596 KB |
Correct: 2606 out of 12000 queries used. |
6 |
Correct |
1 ms |
596 KB |
Correct: 2200 out of 12000 queries used. |
7 |
Correct |
2 ms |
596 KB |
Correct: 3016 out of 12000 queries used. |
8 |
Correct |
1 ms |
596 KB |
Correct: 2639 out of 12000 queries used. |
9 |
Correct |
1 ms |
596 KB |
Correct: 2648 out of 12000 queries used. |
10 |
Correct |
1 ms |
596 KB |
Correct: 2783 out of 12000 queries used. |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Correct: 2426 out of 500000 queries used. |
2 |
Runtime error |
2 ms |
852 KB |
Execution killed with signal 6 |
3 |
Halted |
0 ms |
0 KB |
- |