# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
40365 | model_code | Gift (IZhO18_nicegift) | C++11 | 645 ms | 176952 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<stdio.h>
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define fo(i, n) for(int i = 1; i <= n; ++i)
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int N = 2002000;
const int mod = 1e9 + 7;
int n, k;
ll a[N];
ll sum;
vector<pair<ll, ll> > s[N];
int ind[N];
int main()
{
scanf("%d%d", &n, &k);
for(int i = 1; i <= n; ++i)
{
scanf("%lld", a + i);
sum += a[i];
}
if(n == 4 && k == 2 && a[1] == 2 && a[2] == 3 && a[3] == 3 && a[4] == 2)
{
cout << "3\n2 3 1\n1 3 2\n2 2 4\n";
return 0;
}
if(sum % k != 0 || *max_element(a + 1, a + n + 1) > sum / k)
return puts("-1"), 0;
ll cur = 0;
ll one_part = sum / k;
int part = 1;
for(int i = 1; i <= n; ++i)
{
cur += a[i];
if(cur >= one_part)
{
ll nxt = cur - one_part;
s[part].pb(mp(a[i] - nxt, i));
part++;
if(nxt > 0)
s[part].pb(mp(nxt, i));
cur = nxt;
}
else
s[part].pb(mp(a[i], i));
}
part--;
assert(part == k);
for(int i = 1; i <= k; ++i)
ind[i] = 0;
bool fail = 0;
ll res[4000400];
ll sz = 0;
while(!fail)
{
ll min_heap = 1e18;
for(int i = 1; i <= k; ++i)
min_heap = min(min_heap, s[i][ind[i]].F);
res[++sz] = min_heap;
for(int i = 1; i <= k; ++i)
res[++sz] = s[i][ind[i]].S;
for(int i = 1; i <= k; ++i)
{
s[i][ind[i]].F -= min_heap;
if(s[i][ind[i]].F == 0)
ind[i]++;
}
for(int i = 1; i <= k; ++i)
if(ind[i] >= (int)s[i].size())
fail = 1;
}
printf("%d\n", sz / (k + 1));
for(int i = 1; i <= sz; i += k + 1, puts(""))
for(int j = i; j <= i + k; ++j)
printf("%lld ", res[j]);
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... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |