#include "railroad.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tu;
ll ans;
int p[400010],cross[400010];
vector<int> adj[400010];
vector<tu> v;
set<int> ss;
map<int,int> c;
void dfs(int x, int val) {
p[x]=val;
for (auto s : adj[x]) {
if (p[s]==-1) dfs(s,val);
}
}
int find(int x) {
while (x!=p[x]) x=p[x];
return x;
}
bool same(int x, int y) {
return find(x)==find(y);
}
void unite(int x, int y) {
x=find(x); y=find(y);
p[x]=y;
}
long long plan_roller_coaster(std::vector<int> s, std::vector<int> t) {
int n = (int) s.size();
memset(p,-1,sizeof(p));
for (int i=0; i<n; ++i) {
ss.insert(s[i]);
ss.insert(t[i]);
}
ss.insert(1000000010);
int cnt=0,pre=0;
for (auto it=ss.begin(); it!=ss.end(); ++it) {
if (cnt) v.push_back(tu(*it-pre,cnt,cnt-1));
c[*it]=cnt++;
pre=*it;
}
--cnt;
for (int i=0; i<n; ++i) {
int st=c[s[i]], to=c[t[i]];
if (s[i]<t[i]) {
cross[st+1]++;
cross[to+1]--;
} else if (s[i]>t[i]) {
cross[to+1]--;
cross[st+1]++;
}
adj[st].push_back(to);
}
--cross[1];
for (int i=1; i<=cnt; ++i) {
cross[i]+=cross[i-1];
if (cross[i]>0) {
adj[i].push_back(i-1);
ans+=cross[i];
} else if (cross[i]<0) {
adj[i-1].push_back(i);
}
}
for (int i=0; i<=cnt; ++i) {
if (p[i]==-1) dfs(i,i);
}
sort(v.begin(),v.end());
for (auto s : v) {
int a,b,w;
tie(w,a,b)=s;
if (!same(a,b)) {
unite(a,b);
ans+=w;
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11220 KB |
n = 2 |
2 |
Correct |
7 ms |
11220 KB |
n = 2 |
3 |
Correct |
6 ms |
11260 KB |
n = 2 |
4 |
Correct |
5 ms |
11220 KB |
n = 2 |
5 |
Correct |
6 ms |
11224 KB |
n = 2 |
6 |
Incorrect |
6 ms |
11220 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11220 KB |
n = 2 |
2 |
Correct |
7 ms |
11220 KB |
n = 2 |
3 |
Correct |
6 ms |
11260 KB |
n = 2 |
4 |
Correct |
5 ms |
11220 KB |
n = 2 |
5 |
Correct |
6 ms |
11224 KB |
n = 2 |
6 |
Incorrect |
6 ms |
11220 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
787 ms |
78912 KB |
n = 199999 |
2 |
Correct |
729 ms |
74180 KB |
n = 199991 |
3 |
Correct |
783 ms |
78380 KB |
n = 199993 |
4 |
Correct |
527 ms |
58828 KB |
n = 152076 |
5 |
Correct |
262 ms |
40536 KB |
n = 93249 |
6 |
Correct |
625 ms |
62628 KB |
n = 199910 |
7 |
Correct |
611 ms |
73328 KB |
n = 199999 |
8 |
Correct |
575 ms |
64232 KB |
n = 199997 |
9 |
Correct |
635 ms |
65148 KB |
n = 171294 |
10 |
Correct |
509 ms |
55284 KB |
n = 140872 |
11 |
Correct |
610 ms |
63344 KB |
n = 199886 |
12 |
Correct |
1092 ms |
73608 KB |
n = 199996 |
13 |
Correct |
946 ms |
66376 KB |
n = 200000 |
14 |
Execution timed out |
2080 ms |
75064 KB |
Time limit exceeded |
15 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11220 KB |
n = 2 |
2 |
Correct |
7 ms |
11220 KB |
n = 2 |
3 |
Correct |
6 ms |
11260 KB |
n = 2 |
4 |
Correct |
5 ms |
11220 KB |
n = 2 |
5 |
Correct |
6 ms |
11224 KB |
n = 2 |
6 |
Incorrect |
6 ms |
11220 KB |
answer is not correct: 1 instead of 523688153 |
7 |
Halted |
0 ms |
0 KB |
- |