QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#640425 | #8577. 평균 최대화 | user10086 | 0 | 1ms | 4112kb | C++17 | 3.0kb | 2024-10-14 12:21:22 | 2024-10-14 12:21:22 |
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e3 + 10, inf = 1e15;
int n, idx, l[N], r[N], a[N], s[N], sz[N], rx[N], rs[N];
struct F
{
int i, j;
bool operator> (const F& f2) const
{
return i * f2.j > j * f2.i;
}
}ans[N];
int dp[N][N], tmp[N];
vector<int> son[N];
vector<int> rg[N];
map<array<int, 2>, int> mp;
int build(int l, int r)
{
idx++, ::l[idx] = l, ::r[idx] = r, mp[{l, r}] = idx;
sz[idx] = 1;
int x = idx; rx[x] = r - l + 1, rs[x] = s[r] - s[l - 1];
for (int i = l; i <= r; i++)
{
if (rg[i].empty()) continue;
int j = rg[i].back(); rg[i].pop_back();
int y = build(i, j);
son[x].push_back(y);
rx[x] -= (j - i + 1), rs[x] -= (s[j] - s[i - 1]);
i = j;
}
return x;
}
template<class T>
void chkmax(T &x, T y)
{
if (y > x) x = y;
}
void dfs(int x)
{
for (int i = 0; i <= n; i++) dp[x][i] = -inf;
dp[x][rx[x]] = rs[x];
sz[x] = rx[x];
for (int y : son[x])
{
dfs(y);
for (int i = 0; i <= sz[x] + sz[y]; i++) tmp[i] = -inf;
for (int i = 0; i <= sz[x]; i++)
for (int j = 0; j <= sz[y]; j++)
chkmax(tmp[i + j], dp[x][i] + dp[y][j]);
for (int i = 0; i <= sz[x] + sz[y]; i++) dp[x][i] = tmp[i];
sz[x] += sz[y];
}
chkmax(dp[x][0], 0ll);
}
void initialize(vector<signed> A)
{
n = (int)(A.size());
for (int i = 1; i <= n; i++) a[i] = A[i - 1];
for (int i = 1; i <= n; i++) s[i] = a[i] + s[i - 1];
vector<int> s;
for (int i = n; i >= 1; i--)
{
while (!s.empty() && a[s.back()] > a[i])
{
int x = s.back(); s.pop_back();
if (x != i + 1) rg[i + 1].push_back(x - 1);
}
if (!s.empty() && s.back() != i + 1) rg[i + 1].push_back(s.back() - 1);
if (!s.empty() && a[s.back()] >= a[i]) s.pop_back();
s.push_back(i);
}
// for (int i = 1; i <= n; i++)
// for (int j : rg[i]) printf("[%lld, %lld]\n", i, j);
build(2, n - 1);
// for (int i = 1; i <= n; i++)
// printf("rx[%lld] = %lld, rs[%lld] = %lld\n", i, rx[i], i, rs[i]);
dfs(1);
// for (int i = 1; i <= n; i++)
// for (int j = 0; j <= sz[i]; j++)
// printf("dp[%lld][%lld] = %lld\n", i, j, dp[i][j]);
for (int i = 1; i <= n; i++)
{
ans[i] = {0, 1};
for (int j = 0; j <= sz[i]; j++)
chkmax(ans[i], F{dp[i][j] + a[l[i] - 1] + a[r[i] + 1], j + 2});
int d = __gcd(ans[i].i, ans[i].j);
ans[i].i /= d, ans[i].j /= d;
}
// for (int i = 1; i <= n; i++)
// for (int j : son[i]) printf("[%lld, %lld] -> [%lld, %lld]\n", l[i], r[i], l[j], r[j]);
}
array<long long, 2> maximum_average(signed i, signed j)
{
i++, j++;
assert(mp.find({i + 1, j - 1}) != mp.end());
int id = mp[{i + 1, j - 1}];
return {ans[id].i, ans[id].j};
}
//signed main()
//{
// int n; cin >> n;
// vector<signed> A;
// for (int i = 1, ai; i <= n; i++) cin >> ai, A.push_back(ai);
// initialize(A);
// while (1)
// {
// int l, r; cin >> l >> r;
// auto res = maximum_average(l, r);
// cout << res[0] << ' ' << res[1] << endl;
// }
//}
Details
Tip: Click on the bar to expand more detailed information
Subtask #1:
score: 0
Runtime Error
Test #1:
score: 5
Accepted
time: 1ms
memory: 4112kb
input:
10 2 4 3 9 9 9 9 9 9 1 2 0 2 0 9
output:
3 1 20 3
result:
ok correct!
Test #2:
score: 0
Runtime Error
input:
15 4596730 8340349 4612555 5692442 3914918 5213545 5248236 1276073 3844119 2943960 9231647 5091649 2239006 9139001 4735414 100 7 8 5 6 2 4 0 4 8 9 10 11 3 4 0 1 10 11 10 11 3 4 4 5 12 13 0 2 2 4 11 12 12 14 2 3 7 8 12 14 6 7 4 5 11 12 10 11 7 12 8 9 8 9 0 2 2 3 12 14 7 9 7 9 12 13 10 11 9 11 13 14 8...
output:
Unauthorized output
result:
Subtask #2:
score: 0
Skipped
Dependency #1:
0%
Subtask #3:
score: 0
Skipped
Dependency #2:
0%
Subtask #4:
score: 0
Runtime Error
Test #15:
score: 0
Runtime Error
input:
300000 1 2 4 4 4 4 3 2 4 4 3 4 4 4 4 4 4 4 4 4 3 4 3 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 3 4 4 4 4 4 4 4 4 4 4 3 3 4 4 4 3 4 4 3 4 4 4 4 4 4 4 3 2 4 4 4 4 4 4 4 4 4 4 4 4 4 3 4 4 4 4 4 4 4 4 4 4 4 2 4 4 2 4 4 3 4 4 4 2 3 4 4 4 4 4 4 3 2 4 4 4 2 4 4 4 4 4 4 4 4 4 4 4 2 4 4 4 4 4 4 3 4 4 3 4 4 4 4 4 4 4 4 4...
output:
Unauthorized output
result:
Subtask #5:
score: 0
Skipped
Dependency #3:
0%
Subtask #6:
score: 0
Runtime Error
Test #28:
score: 0
Runtime Error
input:
300000 1 300000 300001 299999 300003 299998 300005 299997 300007 299996 300009 299995 300011 299994 300013 299993 300015 299992 300017 299991 300019 299990 300021 299989 300023 299988 300025 299987 300027 299986 300029 299985 300031 299984 300033 299983 300035 299982 300037 299981 300039 299980 3000...
output:
Unauthorized output
result:
Subtask #7:
score: 0
Skipped
Dependency #4:
0%
Subtask #8:
score: 0
Skipped
Dependency #1:
0%