Untitled

mail@pastecode.io avatar
unknown
plain_text
11 days ago
1.8 kB
5
Indexable
Never
// y.ha
#include <bits/stdc++.h>
#define cook '\n'
#define maxn 10005
#define Task "bai1"
using namespace std;

int n,m,s[maxn],h[maxn],box[maxn][5],sum[maxn],res[maxn][5];
double avesum, res_sum ;

void change() {
    double t = 0;
    for ( int i = 1; i <= n; i++ )
        t += abs( (double)sum[i] - avesum ) ;
    if ( t <= res_sum ) {
        for ( int i = 1; i <= n; i++ ) {
            res[i][1] = box[i][1];
            if ( box[i][2] ) res[i][2] = box[i][2];
            else res[i][2] = 0;
        }
        res_sum = t;
    }
}

void write() {
    for ( int i = 1; i <= n; i++ ) {
        cout << res[i][1] << " ";
        if ( res[i][2] ) cout << res[i][2];
        cout << cook;
    }
    cout << fixed << setprecision(5) << res_sum << cook;
}

void solve(int pos) {
    if ( pos > n ) {
        change();
        return;
    }
    for ( int sl = 0; sl < 3; sl++ ) {
        int had = 0;
        sum[pos] = 0;
        for ( int i = 1; i <= m; i++ ) {
            if ( had == sl ) break ;
            if ( h[i] ) continue;
            h[i] = 1;
            box[pos][++had] = i;
            sum[pos] += s[i];
        }
        solve(pos+1);
        for ( int i = 2; i > 0; i-- ) {
            h[box[pos][i]] = 0;
            box[pos][i] = 0;
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr);
    if(freopen(Task".inp","r",stdin)) {
        freopen(Task".out","w",stdout);
    }
    while ( cin >> n >> m ) {
        avesum = 0;
        res_sum = 10000000000;
        for ( int i = 1; i <= m; i++ ) {
            cin >> s[i];
            avesum += s[i];
        }
        avesum /= n;
        solve(1);
        write();
    }
}
Leave a Comment