Submission #711902

# Submission time Handle Problem Language Result Execution time Memory
711902 2023-03-17T15:56:28 Z tutis Coins (LMIO19_monetos) C++17
56.2627 / 100
2000 ms 63872 KB
/*input
0 4 1 5
0 0 1 0
0 0 0 1
0 1 1 1
1 1 0 1
*/
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
using ll = long long;
using ull = unsigned long long;
using ld = long double;
template<typename A, typename B>
using omap = tree <A, B, less<A>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename A>
using oset = tree <A, null_type, less<A>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 1e9 + 7;
ll ran(ll a, ll b)
{
	//return a + (ll)(rng() % (b - a + 1));
	return uniform_int_distribution<ll>(a, b)(rng);
}
ll power(ll x, ll p)
{
	if (abs(x) >= mod)
		x %= mod;
	if (x < 0)
		x += mod;
	if (abs(p) >= mod - 1)
		p %= mod - 1;
	if (p < 0)
		p += mod - 1;
	ll r = 1;
	while (p)
	{
		if (p % 2)
			r = (r * x) % mod;
		x = (x * x) % mod;
		p /= 2;
	}
	return r;
}
void solve()
{
	int t, n, k1, k2;
	cin >> t >> n >> k1 >> k2;
	int a[n][n];
	int cnt = 0;
	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = n - 1; j >= 0; j--)
		{
			cin >> a[i][j];
			cnt += a[i][j];
		}
	}
	int sp[n][n + 1];
	for (int i = 0; i < n; i++)
	{
		sp[i][0] = 0;
		for (int j = 0; j < n; j++)
			sp[i][j + 1] = sp[i][j] + a[i][j];
	}
	ld lo = 0;
	ld hi = n * n;
	while (abs(lo - hi) > 1e-7)
	{
		ld c = (lo + hi) / 2;
		vector<pair<ld, int>>dp(n + 1, { -1e18, 0});
		dp[n] = {0, 0};
		for (int i = 0; i < n; i++)
		{
			pair<ld, int>mx = dp[n];
			for (int h = n; h >= 0; h--)
			{
				mx = max(mx, dp[h]);
				dp[h] = {mx.first + sp[i][h] - c * h, mx.second + h};
			}
		}
		pair<ld, int>mx = { -1e50, 0};
		for (int h = 0; h < n; h++)
			mx = max(mx, {dp[h].first, h});
		int h = mx.second;
		if (dp[h].second <= cnt)
			hi = c;
		else
			lo = c;
	}

	int h[n];
	{
		vector<pair<ld, int>>dp(n + 1, { -1e18, 0});
		int fr[n][n];
		dp[n] = {0, 0};
		for (int i = 0; i < n; i++)
		{
			pair<ld, int>mx = dp[n];
			int hj = n;
			for (int j = n; j >= 0; j--)
			{
				if (dp[j] > mx)
				{
					hj = j;
					mx = dp[j];
				}
				dp[j] = {mx.first + sp[i][j] - hi * j, mx.second + j};
				fr[i][j] = hj;
			}
		}
		pair<ld, int>mx = { -1e50, 0};
		for (int j = 0; j < n; j++)
			mx = max(mx, {dp[j].first, j});
		int j = mx.second;
		int i = n - 1;
		while (i >= 0)
		{
			h[i] = j;
			j = fr[i][j];
			i--;
		}
	}
	for (int i = 0; i < n; i++)
		cnt -= h[i];
	for (int i = 0; i < n; i++)
	{
		int x = min(cnt, n - h[i]);
		h[i] += x;
		cnt -= x;
	}
	{
		int D = 60;
		int S = 220;
		if (n <= 50)
		{
			D = 30;
			S = 300;
		}
		int dp[n][2 * D + 1][2 * S + 1];
		int bst = -1;
		for (int t = 0;; t++)
		{
			for (int i = 0; i < n; i++)
				for (int d = -D; d <= D; d++)
					for (int s = -S; s <= S; s++)
						dp[i][d + D][s + S] = -1e8;
			for (int i = 0; i < n; i++)
			{
				for (int d = -D; d <= D; d++)
				{
					int hi = h[i] + d;
					if (hi < 0 || hi > n)
						continue;
					if (i == 0)
					{
						dp[i][d + D][d + S] = sp[i][hi];
					}
					else
					{
						for (int dj = max(hi - h[i - 1], -D); dj <= D; dj++)
						{
							for (int s = max(-S, -S + d); s <= min(S, S + d); s++)
							{
								int sj = s - d;
								dp[i][d + D][s + S] = max(dp[i][d + D][s + S], dp[i - 1][dj + D][sj + S] + sp[i][hi]);
							}
						}
					}
				}
			}
			int s = 0;
			int i = n - 1;
			int di = 0;
			{
				pair<int, int>mx = { -1000, -1000};
				for (int d = -D; d <= D; d++)
					mx = max(mx, {dp[i][d + D][s + S], d});
				if (mx.first <= bst)
					break;
				bst = mx.first;
				di = mx.second;
			}
			while (i >= 0)
			{
				h[i] += di;
				int x = 0;
				if (i > 0)
					for (int dj = -D; dj <= D; dj++)
					{
						if (h[i - 1] + dj >= h[i])
						{
							int sj = s - di;
							if (s >= max(-S, -S + di) && s <= min(S, S + di))
							{
								if (dp[i][di + D][s + S] == dp[i - 1][dj + D][sj + S] + sp[i][h[i]])
									x = dj;
							}
						}
					}
				s -= di;
				i--;
				di = x;
			}
		}
	}
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			a[i][j] = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < h[i]; j++)
			a[i][j] = 1;
	for (int i = n - 1; i >= 0; i--)
	{
		for (int j = n - 1; j >= 0; j--)
		{
			cout << a[i][j] << " ";
		}
		cout << "\n";
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	solve();
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 1748 KB K = 17
2 Correct 27 ms 7508 KB K = 576
3 Correct 488 ms 63872 KB K = 18598
4 Partially correct 1327 ms 63872 KB K = 21881
5 Correct 459 ms 63804 KB K = 17071
6 Execution timed out 2063 ms 63636 KB Time limit exceeded
7 Partially correct 1622 ms 63792 KB K = 21889
8 Execution timed out 2075 ms 63616 KB Time limit exceeded
9 Execution timed out 2065 ms 63612 KB Time limit exceeded
10 Execution timed out 2066 ms 63648 KB Time limit exceeded