#include "towns.h"
#include <cstring>
#include <algorithm>
#include <stack>
#include <vector>
using namespace std;
typedef pair<int,int> pi;
int dp[150][150];
int dist(int s, int e){
if(s == e) return 0;
if(s > e) swap(s,e);
if(~dp[s][e]) return dp[s][e];
return dp[s][e] = getDistance(s, e);
}
vector<pi> v;
int p, q, val;
bool diff(int s, int e){
return (val + dist(s, e)) != dist(s, p) + dist(q, e);
}
stack<int> stk;
int solve(int s, int e){
while(!stk.empty()) stk.pop();
for(int i=s; i<=e; i++){
if(stk.empty()) stk.push(v[i].second);
else if(diff(stk.top(), v[i].second)){
stk.pop();
}
else{
stk.push(v[i].second);
}
}
if(stk.empty()) return 0;
int p = 0;
int cnt = 0;
for(int i=s; i<=e; i++){
if(!diff(v[i].second, stk.top())) cnt++;
}
return cnt;
}
int hubDistance(int N, int sub) {
v.clear();
memset(dp,-1,sizeof(dp));
p = -1, val = -1, q = -1;
for(int i=1; i<N; i++){
if(val < dist(0, i)){
val = dist(0, i);
p = i;
}
}
val = -1;
for(int i=0; i<N; i++){
if(val < dist(p, i)){
val = dist(p, i);
q = i;
}
}
for(int i=0; i<N; i++){
int fl = dist(p, i) - dist(q, i) + val;
v.push_back(pi(fl / 2, i));
}
sort(v.begin(), v.end());
int R = val;
int hmax = 1e9;
for(int i=0; i<v.size(); ){
int e = i;
while(e < v.size() && v[e].first == v[i].first){
e++;
}
//printf("%d ",solve(i, e-1));
int hub = max(solve(i, e-1), max(i, (int)v.size() - e));
hmax = min(hmax, hub);
R = min(R, max(val - v[i].first, v[i].first));
i = e;
}
if(hmax >= N / 2) return -R;
return R;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
1720 KB |
Output is correct |
2 |
Correct |
17 ms |
1720 KB |
Output is correct |
3 |
Correct |
0 ms |
1720 KB |
Output is correct |
4 |
Correct |
13 ms |
1720 KB |
Output is correct |
5 |
Correct |
23 ms |
1720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
1720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
16 ms |
1720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
1720 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
1720 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Halted |
0 ms |
0 KB |
- |