Cod sursa(job #1511048)

Utilizator serbanSlincu Serban serban Data 25 octombrie 2015 22:54:01
Problema Farfurii Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1 kb
#include <bits/stdc++.h>

using namespace std;

bool c[1000005];

int main()
{
    freopen("farfurii.in", "r", stdin);
    FILE *g = fopen("farfurii.out", "w");

    int n, k;
    cin >> n >> k;

    int last = 1;
    for(int i = 1; i <= n; i ++) {
        int j = n - i - 1;
        int nxt = j * (j + 1) / 2;
        if(nxt >= k) {
            fprintf(g, "%d ", last);
            c[last] = true;
            while(c[last])
                last ++;
        }
        else {
            int j = k - nxt;
            int in = last;
            while(j) {
                if(!c[last])
                    j --;
                last ++;
                while(c[last])
                    last ++;
            }
            fprintf(g, "%d ", last);
            c[last] = true;
            k = nxt;
            last = in;
            i = n;
        }
    }
    for(int j = n; j >= last; j --)
        if(!c[j])
            fprintf(g, "%d ", j);
    fprintf(g, "\n");
    return 0;
}