QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#638705#5137. TowerLyniaWA 3ms10216kbC++232.4kb2024-10-13 16:40:072024-10-13 16:40:07

Judging History

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

  • [2024-10-13 16:40:07]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:10216kb
  • [2024-10-13 16:40:07]
  • 提交

answer

#include<bits/stdc++.h>
#define fa(i,op,n) for(int i=op;i<=n;i++)
#define fb(i,op,n) for(int i=op;i>=n;i--)
#define endl '\n'
#define pb push_back
#define var auto
using namespace std;
using ll = long long;
using pii = pair<int, int>;

const int N = 510;

int n, m;
ll a[N];
ll f1[N][N], f2[N][N];
ll zhi1[N][N], zhi2[N][N];
ll cnt;

ll getcnt(ll a, ll b)
{
	if (a == 0 || b == 0) return 0;
	ll ans = 0;
	while (a != b)
	{
		if (a > b)
		{
			if (a - b == 1)
			{
				ans++;
				return ans;
			}
			else
			{
				a /= 2;
				ans++;
			}
		}
		else
		{
			if (b - a == 1)
			{
				ans++;
				return ans;
			}
			else
			{
				b /= 2;
				ans++;
			}
		}
	}
	return ans;
}

ll getzhi1(ll a, ll b)
{
	if (a == 0) return b;
	else if (b == 0) return a;
	while (a != b)
	{
		if (a > b)
		{
			if (a - b == 1) return a;
			else a /= 2;
		}
		else
		{
			if (b - a == 1) return b;
			else b /= 2;
		}
	}
	return a;
}

ll getzhi2(ll a, ll b)
{
	if (a == 0) return b;
	else if (b == 0) return a;
	while (a != b)
	{
		if (a > b)
		{
			if (a - b == 1) return b;
			else a /= 2;
		}
		else
		{
			if (b - a == 1) return a;
			else b /= 2;
		}
	}
	return a;
}

void solve() 
{
	cin >> n >> m;
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + 1 + n);
	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= m; j++)
		{
			f1[i][j] = f1[i - 1][j] + getcnt(a[i], zhi1[i - 1][j]);
			zhi1[i][j] = getzhi1(a[i], zhi1[i - 1][j]);
			if (j > 0)
			{
				if (f1[i][j] > f1[i - 1][j - 1])
				{
					f1[i][j] = f1[i - 1][j - 1];
					zhi1[i][j] = zhi1[i - 1][j - 1];
				}
			}
		}
	}

	for (int i = 1; i <= n; i++)
	{
		for (int j = 0; j <= min(i,m); j++)
		{
			f2[i][j] = f2[i - 1][j] + getcnt(a[i], zhi2[i - 1][j]);
			zhi2[i][j] = zhi2[i - 1][j];
			zhi2[i][j] = getzhi2(a[i], zhi2[i - 1][j]);
			if (j > 0)
			{
				if (f2[i][j] > f2[i - 1][j - 1])
				{
					f2[i][j] = f2[i - 1][j - 1];
					zhi2[i][j] = zhi2[i - 1][j - 1];
				}
			}
		}
	}

	//for (int i = 1; i <= n; i++)
	//{
	//	for (int j = 0; j <= m; j++)
	//		cout << f1[i][j] << ' ';
	//	cout << '\n';
	//}

	//cout << f1[n][m] << ' ' << f2[n][m] << '\n';

	cout << min(f1[n][m], f2[n][m]) << '\n';
}
int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int _ = 1;
	cin >> _;
	while (_--)
	{
		solve();
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3760kb

input:

3
2 0
2 6
5 0
1 2 3 4 5
5 3
1 2 3 4 5

output:

2
4
1

result:

ok 3 number(s): "2 4 1"

Test #2:

score: -100
Wrong Answer
time: 3ms
memory: 10216kb

input:

10
272 118
11 14 49 94 71 62 46 45 74 22 15 36 7 37 27 35 96 85 75 78 76 64 23 59 17 35 71 28 96 82 5 66 2 48 57 31 88 10 61 73 79 23 19 52 39 76 48 98 5 39 48 51 90 90 60 27 47 24 24 56 48 27 39 21 38 18 20 9 62 83 47 15 51 22 73 74 7 80 64 60 86 74 59 7 84 38 99 31 42 60 52 41 63 88 59 90 77 40 68...

output:

154
4
174
88
220
233
224
91
355
54

result:

wrong answer 1st numbers differ - expected: '454', found: '154'