Pagini recente » Cod sursa (job #975103) | Cod sursa (job #3125452) | Cod sursa (job #2507110) | Cod sursa (job #1320054) | Cod sursa (job #2715276)
#include <fstream>
using namespace std;
ifstream cin ("farfurii.in");
ofstream cout ("farfurii.out");
const int N = 1e5 + 5;
int n, k, start = 1;
int a[4 * N];
void Update(int nod) {
a[nod] = a[nod + nod] + a[nod + nod + 1];
if (nod > 1)
Update(nod / 2);
}
int Query(int x) {
int nod = 1, aux = 0;
while (nod < start) {
if (a[nod + nod] + aux < x) {
aux += a[nod + nod];
nod += nod + 1;
} else {
nod += nod;
}
}
return nod - start + 1;
}
int main() {
cin >> n >> k;
while (start < n)
start <<= 1;
for (int i = 1; i <= n; ++i) {
a[i + start - 1] = 1;
Update((i + start - 1) / 2);
}
int poz;
for (int i = 1; i <= n; ++i) {
int urm = (n - i) * (n - i - 1) / 2;
if (urm >= k) {
poz = 1;
} else {
poz = k - urm + 1;
}
k -= poz - 1;
int x = Query(poz);
cout << x << ' ';
a[x + start - 1] = 0;
Update((x + start - 1) / 2);
}
return 0;
}