답안 #544581

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
544581 2022-04-02T11:33:17 Z spike1236 Gift (IZhO18_nicegift) C++14
100 / 100
647 ms 133400 KB
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ll long long
#define ld long double
#define all(_v) _v.begin(), _v.end()
#define sz(_v) (int)_v.size()
#define pii pair <int, int>
#define pll pair <ll, ll>
#define veci vector <int>
#define vecll vector <ll>

const int dx[4] = {1, -1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
const double PI = 3.1415926535897932384626433832795;
const double eps = 1e-9;
const int MOD1 = 1e9 + 7;
const int MOD2 = 998244353;

const int MAXN = 1e6 + 10;
int n, k;
ll a[MAXN];
vector <veci> ans;
vecll vals;
vector <vector <pll>> g;

void solve() {
    cin >> n >> k;
    ll sum = 0;
    ll mx = 0;
    for(int i = 1; i <= n; ++i) {
        cin >> a[i];
        sum += a[i];
        mx = max(mx, a[i]);
    }
    if(sum % k != 0 || mx > sum / k) {
        cout << -1;
        return;
    }
    ll p = 0;
    vector <pll> cur;
    for(int i = 1; i <= n; ++i) {
        p += a[i];
        if(p >= sum / k) {
            cur.pb(mp(a[i] - p + sum / k, i));
            g.pb(cur);
            cur.clear();
            p -= sum / k;
            if(p != 0) cur.pb(mp(p, i));
        } else cur.pb(mp(a[i], i));
    }
    if(!cur.empty()) g.pb(cur);
    while(1) {
        ll mn = 1e18;
        for(int i = 0; i < sz(g); ++i) mn = min(mn, g[i].back().f);
        vals.pb(mn);
        bool ok = 0;
        veci cr;
        for(int i = 0; i < sz(g); ++i) {
            cr.pb(g[i].back().s);
            if(g[i].back().f == mn) g[i].pop_back();
            else g[i].back().f -= mn;
            if(g[i].empty()) ok = 1;
        }
        ans.pb(cr);
        if(ok) break;
    }
    cout << sz(ans) << '\n';
    for(int i = 0; i < sz(ans); ++i) {
        cout << vals[i] << ' ';
        for(auto j : ans[i]) cout << j << ' ';
        cout << '\n';
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int CNT_TESTS = 1;
    ///cin >> CNT_TESTS;
    for(int NUMCASE = 1; NUMCASE <= CNT_TESTS; ++NUMCASE) {
        solve();
        if(NUMCASE != CNT_TESTS) cout << '\n';
    }
    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=4
2 Correct 1 ms 332 KB n=3
3 Correct 0 ms 332 KB n=3
4 Correct 1 ms 212 KB n=4
5 Correct 1 ms 212 KB n=4
6 Correct 1 ms 212 KB n=2
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=4
2 Correct 1 ms 332 KB n=3
3 Correct 0 ms 332 KB n=3
4 Correct 1 ms 212 KB n=4
5 Correct 1 ms 212 KB n=4
6 Correct 1 ms 212 KB n=2
7 Correct 1 ms 212 KB n=5
8 Correct 1 ms 216 KB n=8
9 Correct 1 ms 340 KB n=14
10 Correct 1 ms 212 KB n=11
11 Correct 28 ms 5324 KB n=50000
12 Correct 20 ms 5072 KB n=50000
13 Correct 1 ms 212 KB n=10
14 Correct 1 ms 340 KB n=685
15 Correct 1 ms 340 KB n=623
16 Correct 1 ms 340 KB n=973
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=4
2 Correct 1 ms 332 KB n=3
3 Correct 0 ms 332 KB n=3
4 Correct 1 ms 212 KB n=4
5 Correct 1 ms 212 KB n=4
6 Correct 1 ms 212 KB n=2
7 Correct 1 ms 212 KB n=5
8 Correct 1 ms 216 KB n=8
9 Correct 1 ms 340 KB n=14
10 Correct 1 ms 212 KB n=11
11 Correct 28 ms 5324 KB n=50000
12 Correct 20 ms 5072 KB n=50000
13 Correct 1 ms 212 KB n=10
14 Correct 1 ms 340 KB n=685
15 Correct 1 ms 340 KB n=623
16 Correct 1 ms 340 KB n=973
17 Correct 1 ms 340 KB n=989
18 Correct 1 ms 340 KB n=563
19 Correct 4 ms 504 KB n=592
20 Correct 2 ms 468 KB n=938
21 Correct 4 ms 340 KB n=747
22 Correct 2 ms 460 KB n=991
# 결과 실행 시간 메모리 Grader output
1 Correct 404 ms 91048 KB n=1000000
2 Correct 247 ms 50388 KB n=666666
3 Correct 125 ms 25428 KB n=400000
4 Correct 328 ms 51208 KB n=285714
5 Correct 6 ms 1236 KB n=20000
6 Correct 282 ms 39940 KB n=181818
7 Correct 3 ms 852 KB n=10000
8 Correct 30 ms 3504 KB n=6666
9 Correct 2 ms 468 KB n=4000
10 Correct 167 ms 17752 KB n=2857
11 Correct 1 ms 468 KB n=2000
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB n=4
2 Correct 1 ms 332 KB n=3
3 Correct 0 ms 332 KB n=3
4 Correct 1 ms 212 KB n=4
5 Correct 1 ms 212 KB n=4
6 Correct 1 ms 212 KB n=2
7 Correct 1 ms 212 KB n=5
8 Correct 1 ms 216 KB n=8
9 Correct 1 ms 340 KB n=14
10 Correct 1 ms 212 KB n=11
11 Correct 28 ms 5324 KB n=50000
12 Correct 20 ms 5072 KB n=50000
13 Correct 1 ms 212 KB n=10
14 Correct 1 ms 340 KB n=685
15 Correct 1 ms 340 KB n=623
16 Correct 1 ms 340 KB n=973
17 Correct 1 ms 340 KB n=989
18 Correct 1 ms 340 KB n=563
19 Correct 4 ms 504 KB n=592
20 Correct 2 ms 468 KB n=938
21 Correct 4 ms 340 KB n=747
22 Correct 2 ms 460 KB n=991
23 Correct 404 ms 91048 KB n=1000000
24 Correct 247 ms 50388 KB n=666666
25 Correct 125 ms 25428 KB n=400000
26 Correct 328 ms 51208 KB n=285714
27 Correct 6 ms 1236 KB n=20000
28 Correct 282 ms 39940 KB n=181818
29 Correct 3 ms 852 KB n=10000
30 Correct 30 ms 3504 KB n=6666
31 Correct 2 ms 468 KB n=4000
32 Correct 167 ms 17752 KB n=2857
33 Correct 1 ms 468 KB n=2000
34 Correct 13 ms 3012 KB n=23514
35 Correct 13 ms 3060 KB n=23514
36 Correct 1 ms 328 KB n=940
37 Correct 0 ms 332 KB n=2
38 Correct 82 ms 12872 KB n=100000
39 Correct 84 ms 12832 KB n=100000
40 Correct 0 ms 212 KB n=10
41 Correct 1 ms 340 KB n=100
42 Correct 6 ms 724 KB n=1000
43 Correct 508 ms 125932 KB n=1000000
44 Correct 647 ms 133400 KB n=1000000
45 Correct 464 ms 91892 KB n=666666
46 Correct 382 ms 59260 KB n=400000
47 Correct 161 ms 17496 KB n=2336
48 Correct 305 ms 51260 KB n=285714
49 Correct 260 ms 39768 KB n=181818
50 Correct 203 ms 23044 KB n=40000
51 Correct 182 ms 20916 KB n=20000
52 Correct 184 ms 18820 KB n=10000
53 Correct 170 ms 18468 KB n=6666
54 Correct 195 ms 17904 KB n=4000
55 Correct 166 ms 17740 KB n=2857
56 Correct 160 ms 16952 KB n=2000