답안 #711889

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
711889 2023-03-17T15:50:51 Z tutis Coins (LMIO19_monetos) C++17
31.5831 / 100
30 ms 1556 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-5)
	{
		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 ans[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 h = n; h >= 0; h--)
			{
				if (dp[h] > mx)
				{
					hj = h;
					mx = dp[h];
				}
				dp[h] = {mx.first + sp[i][h] - hi * h, mx.second + h};
				fr[i][h] = hj;
			}
		}
		pair<ld, int>mx = { -1e50, 0};
		for (int h = 0; h < n; h++)
			mx = max(mx, {dp[h].first, h});
		int h = mx.second;
		int i = n - 1;
		while (i >= 0)
		{
			ans[i] = h;
			h = fr[i][h];
			i--;
		}
	}
	for (int i = 0; i < n; i++)
		cnt -= ans[i];
	for (int i = 0; i < n; i++)
	{
		int x = min(cnt, n - ans[i]);
		ans[i] += x;
		cnt -= 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 < ans[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();
}
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 212 KB K = 18
2 Incorrect 1 ms 340 KB K = 585
3 Partially correct 26 ms 1552 KB K = 18658
4 Partially correct 28 ms 1484 KB K = 22108
5 Correct 25 ms 1448 KB K = 17076
6 Partially correct 25 ms 1540 KB K = 22330
7 Partially correct 29 ms 1556 KB K = 22414
8 Incorrect 30 ms 1556 KB K = 22503
9 Incorrect 27 ms 1540 KB K = 21875
10 Partially correct 25 ms 1484 KB K = 22367