#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
struct DSU{
int path[4004000], chk[4004000], n;
vector<int> buf;
void init(int _n){
n = _n;
for (int i=0;i<n*n;i++) path[i] = i, chk[i] = 0;
}
int find(int s){
if (s==path[s]) return s;
return path[s] = find(path[s]);
}
void merge(int s, int v){
if (!chk[s] || !chk[v]) return;
s = find(s), v = find(v);
if (s==v) return;
path[s] = v;
}
void on(int x, int y){
int pos = (x-1)*n + y-1;
if (x==y) pos = 0;
chk[pos] = 1;
buf.push_back(pos);
for (int k=0;k<4;k++){
int nx = x + dx[k], ny = y + dy[k];
if (nx<=0 || nx>n || ny<=0 || ny>n || nx>ny) continue;
int np = (nx-1)*n + ny-1;
merge(pos, np);
}
}
void off(){for (auto &x:buf){path[x] = x; chk[x] = 0;} buf.clear();}
bool ok(){return find(0) == find(n-1) && chk[0] && chk[n-1];}
}dsu;
struct Frac{
ll a, b;
Frac(){}
Frac(ll _a, ll _b): a(_a), b(_b) {}
long double ch(){return (long double) a / b;}
bool operator < (const Frac &F) const{
return (__int128)a * F.b < (__int128)b * F.a;
}
}val[2020][2020];
vector<pair<int, int>> P;
int a[2020];
bool cmp (const pair<int, int> &x, const pair<int, int> &y){
return val[x.first][x.second] < val[y.first][y.second];
}
int main(){
int n;
scanf("%d", &n);
for (int i=1;i<=n;i++) scanf("%d", a+i);
for (int i=1;i<=n;i++){
val[i][i] = Frac(a[i], 1);
P.emplace_back(i, i);
for (int j=i+1;j<=n;j++){
val[i][j].a = val[i][j-1].a + a[j];
val[i][j].b = j-i+1;
P.emplace_back(i, j);
//printf("%Lf ", val[i][j].ch());
}
//printf("\n");
}
sort(P.begin(), P.end(), cmp);
//for (int i=0;i<(int)P.size()-1;i++) assert(!(val[P[i+1].first][P[i+1].second] < val[P[i].first][P[i].second]));
long double ans = 1e18;
dsu.init(n);
for (int l=0;l<(int)P.size();l++){
//printf("Start = %d %d (%Lf)\n", P[l].first, P[l].second, val[P[l].first][P[l].second].ch());
int r;
for (r=l;r<(int)P.size();r++){
//printf(" %d %d (%Lf)\n", P[r].first, P[r].second, val[P[r].first][P[r].second].ch());
dsu.on(P[r].first, P[r].second);
if (dsu.ok()){
//printf("ok %Lf\n", val[P[r].first][P[r].second].ch() - val[P[l].first][P[l].second].ch());
ans = min(ans, val[P[r].first][P[r].second].ch() - val[P[l].first][P[l].second].ch());
break;
}
}
if (r==(int)P.size()) break;
dsu.off();
dsu.init(n);
}
printf("%.15Lf\n", ans);
return 0;
}
Compilation message
seesaw.cpp: In function 'int main()':
seesaw.cpp:63:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
63 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
seesaw.cpp:64:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
64 | for (int i=1;i<=n;i++) scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |