#include <algorithm>
#include <iostream>
#include <string.h>
#include <cstdlib>
#include <vector>
#include <string>
#include <bitset>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef long double ld;
const ll MOD = 1e9 + 7, INF = 1e18 + 1;
using namespace std;
int p[1000000], sz[1000000];
set <int> mp;
struct stop
{
int x;
bool op, left;
bool operator < (const stop& b)
{
if (x == b.x) return op > b.op;
else return x < b.x;
}
};
struct edge
{
int u, v;
ll c;
bool operator < (const edge& b)
{
return c < b.c;
}
};
vector <stop> v;
int find (int x)
{
if (p[x] == x) return x;
else return p[x] = find (p[x]);
}
void unite (int a, int b)
{
a = find (a);
b = find (b);
if (a == b) return;
if (sz[a] < sz[b]) swap (a, b);
p[b] = a;
sz[a] += sz[b];
}
int ind (int a, vector <int>& s)
{
return lower_bound (s.begin(), s.end(), a) - s.begin ();
}
ll plan_roller_coaster (vector <int> s, vector <int> t)
{
ll ans = 0, balance = 0;
vector <int> coord;
for (int i = 0; i < s.size (); i++)
{
if (s[i] > t[i])
{
v.push_back ({t[i], true, true});
v.push_back ({s[i], false, true});
}
else
{
v.push_back ({s[i], true, false});
v.push_back ({t[i], false, false});
}
mp.insert (s[i]);
mp.insert (t[i]);
}
for (auto a : mp)
coord.push_back (a);
for (int i = 0; i < coord.size (); i++)
{
sz[i] = 1;
p[i] = i;
}
v.push_back ({1, true, true});
v.push_back ({coord.back (), false, true});
for (int i = 0; i < s.size (); i++)
unite (ind (s[i], coord), ind (t[i], coord));
sort (v.begin(), v.end());
vector <edge> tr;
for (int i = 0; i < v.size (); i++)
{
if (v[i].left ^ v[i].op) balance++;
else balance--;
vector <edge> tr;
if (i != v.size () - 1)
{
ans += max (0LL, balance * ll (v[i+1].x - v[i].x));
if (balance) unite (ind (v[i].x, coord), ind (v[i+1].x, coord));
else tr.push_back ({ind (v[i].x, coord), ind (v[i+1].x, coord), (v[i+1].x - v[i].x)});
}
}
sort (tr.begin(), tr.end());
for (edge e : tr)
{
if (find (e.u) != find (e.v))
{
ans += e.c;
unite (e.u, e.v);
}
}
return ans;
}
Compilation message
railroad.cpp: In function 'll plan_roller_coaster(std::vector<int>, std::vector<int>)':
railroad.cpp:81:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size (); i++)
~~^~~~~~~~~~~
railroad.cpp:101:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < coord.size (); i++)
~~^~~~~~~~~~~~~~~
railroad.cpp:110:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < s.size (); i++)
~~^~~~~~~~~~~
railroad.cpp:117:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < v.size (); i++)
~~^~~~~~~~~~~
railroad.cpp:124:9: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (i != v.size () - 1)
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
376 KB |
n = 2 |
3 |
Correct |
2 ms |
376 KB |
n = 2 |
4 |
Correct |
2 ms |
376 KB |
n = 2 |
5 |
Correct |
2 ms |
376 KB |
n = 2 |
6 |
Correct |
2 ms |
380 KB |
n = 2 |
7 |
Correct |
2 ms |
376 KB |
n = 3 |
8 |
Correct |
2 ms |
376 KB |
n = 3 |
9 |
Correct |
2 ms |
380 KB |
n = 3 |
10 |
Correct |
2 ms |
376 KB |
n = 8 |
11 |
Incorrect |
2 ms |
376 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
376 KB |
n = 2 |
3 |
Correct |
2 ms |
376 KB |
n = 2 |
4 |
Correct |
2 ms |
376 KB |
n = 2 |
5 |
Correct |
2 ms |
376 KB |
n = 2 |
6 |
Correct |
2 ms |
380 KB |
n = 2 |
7 |
Correct |
2 ms |
376 KB |
n = 3 |
8 |
Correct |
2 ms |
376 KB |
n = 3 |
9 |
Correct |
2 ms |
380 KB |
n = 3 |
10 |
Correct |
2 ms |
376 KB |
n = 8 |
11 |
Incorrect |
2 ms |
376 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
642 ms |
35900 KB |
n = 199999 |
2 |
Correct |
669 ms |
35048 KB |
n = 199991 |
3 |
Correct |
638 ms |
34968 KB |
n = 199993 |
4 |
Correct |
457 ms |
27880 KB |
n = 152076 |
5 |
Correct |
255 ms |
17152 KB |
n = 93249 |
6 |
Correct |
551 ms |
30656 KB |
n = 199910 |
7 |
Correct |
559 ms |
34316 KB |
n = 199999 |
8 |
Correct |
530 ms |
30768 KB |
n = 199997 |
9 |
Correct |
519 ms |
30624 KB |
n = 171294 |
10 |
Correct |
387 ms |
26188 KB |
n = 140872 |
11 |
Incorrect |
563 ms |
31100 KB |
answer is not correct: 0 instead of 1 |
12 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
n = 2 |
2 |
Correct |
2 ms |
376 KB |
n = 2 |
3 |
Correct |
2 ms |
376 KB |
n = 2 |
4 |
Correct |
2 ms |
376 KB |
n = 2 |
5 |
Correct |
2 ms |
376 KB |
n = 2 |
6 |
Correct |
2 ms |
380 KB |
n = 2 |
7 |
Correct |
2 ms |
376 KB |
n = 3 |
8 |
Correct |
2 ms |
376 KB |
n = 3 |
9 |
Correct |
2 ms |
380 KB |
n = 3 |
10 |
Correct |
2 ms |
376 KB |
n = 8 |
11 |
Incorrect |
2 ms |
376 KB |
answer is not correct: 187084041 instead of 189002015 |
12 |
Halted |
0 ms |
0 KB |
- |