QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#642080#8577. 평균 최대화user100860 3ms42320kbC++174.9kb2024-10-15 09:51:002024-10-15 09:51:00

Judging History

你现在查看的是最新测评结果

  • [2024-10-15 09:51:00]
  • 评测
  • 测评结果:0
  • 用时:3ms
  • 内存:42320kb
  • [2024-10-15 09:51:00]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 3e5 + 10, inf = 1e13;

int n, idx, l[N], r[N], a[N], s[N], sz[N], rx[N], rs[N];

#define __int128 int

struct F
{
	int i, j;
	
	F()
	{
		i = j = 0;
	}
	
	F(int a, int b)
	{
		int d = __gcd(a, b);
		i = a / d, j = b / d;
	}
	
	bool operator> (const F& f2) const
	{
		return (__int128)i * f2.j > (__int128)j * f2.i;
	}
	
	bool operator< (const F& f2) const
	{
		return (__int128)i * f2.j < (__int128)j * f2.i;
	}
	
	F operator + (const F& f2) const
	{
		__int128 a = (__int128)i * f2.j + (__int128)j * f2.i, b = (__int128)j * f2.j;
		auto d = __gcd(a, b);
		return {a / d, b / d};
	}
	
	void reduce()
	{
		auto d = __gcd(i, j);
		i /= d, j /= d;
	}
	
}ans[N];

struct DS
{
	vector<F> d;
	
	void insert(F v) 
	{
		d.insert(lower_bound(d.begin(), d.end(), v), v);
	}
	
	void assign(int pos, F v)
	{
		assert(pos <= d.size());
		for (int i = 0; i < pos; i++) d[i] = v;
	}
	
	F operator[] (int x) const
	{
		F s = {0, 1};
		for (int i = 0; i < x; i++) s = s + d[i];
		return s;
	}
	
	void print() const
	{
		for (auto v : d) printf("%lld/%lld ", v.i, v.j); printf("\n");
	}
};

DS ds[N];
DS* dp[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;
}

DS* merge(DS* a, DS* b)
{
	if (a->d.size() < b->d.size()) swap(a, b);
	for (F v : b->d) a->insert(v);
	return a;
}
pair<int, F> getp(const DS* ds, int a, int b)
{
	// max (y - b) / (x - a)
	assert(!ds->d.empty());
	int l = 0, r = ds->d.size();
	auto getres = [&](int x)
	{
		F y1 = (*ds)[x];
		y1.i -= b * y1.j, y1.j *= (x - a);
		y1.reduce();
		return y1;
	};
	while (l + 1 < r)
	{
		int mid1 = l + (r - l + 1) / 3, mid2 = r - (r - l) / 3;
		F y1 = getres(mid1), y2 = getres(mid2);
		if (y1 < y2) l = mid1 + 1;
		else if (y1 > y2) r = mid2 - 1;
		else l = mid1, r = mid2;
	}
	printf("res(l) = (%lld, %lld/%lld), res(r) = (%lld, %lld/%lld)\n", l, getres(l).i, getres(l).j, r, getres(r).i, getres(r).j);
	if (getres(l) > getres(r)) return {l, getres(l)};
	else return {r, getres(r)};
}

void dfs(int x)
{
	printf("dfs(%lld)\n", x);
	dp[x] = &ds[x];
	if (son[x].empty())
	{
		if (rx[x]) for (int i = 1; i <= rx[x]; i++) printf("%lld, %lld, %lld***\n", x, rs[x], rx[x]), dp[x]->insert({rs[x], rx[x]});
	}
	else 
	{
		dp[x] = &ds[x];
		for (int y : son[x]) dfs(y), dp[x] = merge(dp[x], dp[y]);
		dp[x]->print();
		// (-rx[x], -rs[x])
		auto res = getp(dp[x], -rx[x], -rs[x]);
		printf("proc: res = {%lld, %lld/%lld}", res.first, res.second.i, res.second.j);
		int p = res.first;
		F v = res.second;
		dp[x]->assign(p, v);
		for (int i = 1; i <= rx[x]; i++) dp[x]->insert(v);
	}
	dp[x]->print();
	auto res = getp(dp[x], -2, -(a[l[x] - 1] + a[r[x] + 1]));
	ans[x] = res.second;
	printf("%lld: (%lld, %lld/%lld)\n", x, res.first, ans[x].i, ans[x].j);
}

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++)
//    	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++;
	if (j == i + 1) return {a[i] + a[j], 2};
	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
Wrong Answer

Test #1:

score: 0
Wrong Answer
time: 3ms
memory: 42320kb

input:

10
2 4 3 9 9 9 9 9 9 1
2
0 2
0 9

output:

Unauthorized output

result:

wrong output format Expected integer, but "Unauthorized" found

Subtask #2:

score: 0
Skipped

Dependency #1:

0%

Subtask #3:

score: 0
Skipped

Dependency #2:

0%

Subtask #4:

score: 0
Time Limit Exceeded

Test #15:

score: 0
Time Limit Exceeded

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
Time Limit Exceeded

Test #28:

score: 0
Time Limit Exceeded

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%