Submission #1088169

# Submission time Handle Problem Language Result Execution time Memory
1088169 2024-09-14T04:14:36 Z quangminh412 Garaža (COCI17_garaza) C++14
0 / 160
127 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;

/*
  John Watson
  https://codeforces.com/profile/quangminh98

  Mua Code nhu mua Florentino !!
*/

#define faster() ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

const int oo = 1e9 + 9;
const int maxn = 2e5 + 9;

int arr[maxn];
int n, k;

void solve_2()
{
	vector<int> cnt(1002, 0);
	for (int i = 1; i <= n; i++)
		cnt[arr[i]]++;
	
	for (int i = 1; i <= 1000; i++)
		if (cnt[i] > 1)
		{
			k -= cnt[i] / 2;
			cnt[i] %= 2;
			if (k < 0)
			{
				cout << 0 << '\n';
				return;
			}
		}
	vector<int> a;
	for (int i = 1; i <= 1000; i++)	
		if (cnt[i] > 0) 
			a.push_back(i);
	
	int n = a.size();
	vector<vector<int>> dp(n + 2, vector<int>(k + 2, oo));
	for (int i = 0; i <= n; i++)
		dp[i][0] = 0;
		
	int ans = oo;
	for (int i = 2; i <= n; i++)
	{
		for (int j = 1; j <= k; j++)
		{
			dp[i][j] = min(dp[i - 1][j], dp[i - 2][j - 1] + a[i - 1] - a[i - 2]);
		}
		ans = min(ans, dp[i][k]);
	}

	cout << ans << '\n';
}

signed main()
{
	if (fopen("test.inp", "r"))
	{
		freopen("test.inp", "r", stdin);
		freopen("test.out", "w", stdout);
	}
	faster();
	
	cin >> n >> k;
	bool sub2 = true;
	for (int i = 1; i <= n; i++)
	{
		cin >> arr[i];
		if (arr[i] > 1000) sub2 = false;
	}
	
	if (sub2 == true)
	{
		solve_2();
		return 0;
	}
	
	sort(arr + 1, arr + n + 1);
	vector<vector<int>> dp(n + 2, vector<int>(k + 2, oo));
	for (int i = 0; i <= n; i++)
		dp[i][0] = 0;
		
	int ans = oo;
	for (int i = 2; i <= n; i++)
	{
		for (int j = 1; j <= k; j++)
		{
			dp[i][j] = min(dp[i - 1][j], dp[i - 2][j - 1] + arr[i] - arr[i - 1]);
		}
		ans = min(ans, dp[i][k]);
	}

	cout << ans << '\n';

	return 0;
}

Compilation message

garaza.cpp: In function 'int main()':
garaza.cpp:63:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   63 |   freopen("test.inp", "r", stdin);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
garaza.cpp:64:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   64 |   freopen("test.out", "w", stdout);
      |   ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 4 ms 4444 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 127 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 118 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 121 ms 262144 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -