# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1088169 | quangminh412 | Garaža (COCI17_garaza) | C++14 | 127 ms | 262144 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |