#include "bartender.h"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
static int N, L;
static vi ord;
static int fen[33];
static void update(int idx, int v)
{
for (int e = idx + 1; e <= N; e += e & (-e)) fen[e] += v;
}
static int qry(int idx)
{
int res = 0;
for (int e = idx + 1; e; e -= e & (-e)) res += fen[e];
return res;
}
static ll encode(vi v) //counts the # of permutations coming before v.
{
ll w = 1;
FOR(i, 1, SZ(v) + 1) w *= i;
ll res = 0;
FOR(i, 0, SZ(v))
{
update(v[i], 1);
}
FOR(i, 0, SZ(v))
{
w /= (SZ(v) - i);
update(v[i], -1);
int c = qry(v[i]);
res += c * w;
}
return res;
}
vi conv(ll x) //converts x to a vector of n #s, the first L of which are < 2 the rest of which are < 5
{
vi res;
FOR(i, 0, N - L)
{
res.PB(x % 5);
x /= 5;
}
FOR(i, 0, L)
{
res.PB(x % 2);
x /= 2;
}
return res;
}
vi BlendWines(int k, vi P)
{
N = SZ(P);
vi ans(N); return ans;
FOR(i, 0, N) P[i]--;
//encode the array R1, R2, ..., Rk into:
//block 1 of size min(12, N) containing #s 1-2
//block 2 of the rest containig #s 3-7
L = min(12, N);
//you wanna encode arr[L]...arr[R]
vi vec;
FOR(i, 0, N)
{
if (P[i] >= L)
{
vec.PB(P[i] - L);
}
}
ll x = encode(vec);
vi res = conv(x), sse, sfi;
FOR(i, 0, N - L)
{
sse.PB(res[i]);
}
FOR(i, N - L, N)
{
sfi.PB(res[i]);
}
int it0 = 0, it1 = 0;
FOR(i, 0, N)
{
if (P[i] >= L)
{
ans[i] = 3 + sse.back();
sse.pop_back();
}
else
{
ans[i] = 1 + sfi.back();
sfi.pop_back();
}
}
return ans;
}
#include "taster.h"
#include <bits/stdc++.h>
using namespace std;
#define PB push_back
#define LB lower_bound
#define fi first
#define se second
#define SZ(x) ((int) (x).size())
#define ALL(x) (x).begin(), (x).end()
#define FOR(i, a, b) for (auto i = (a); i < (b); i++)
#define FORD(i, a, b) for (auto i = (a) - 1; i >= (b); i--)
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
static int N, L;
static int fen[33];
static void update(int idx, int v)
{
for (int e = idx + 1; e <= N; e += e & (-e)) fen[e] += v;
}
static int kth(int k)
{
int res = 0;
FORD(i, 6, 0)
{
if (res + (1 << i) <= N && fen[res + (1 << i)] <= k)
{
res += (1 << i);
k -= fen[res];
}
}
return res;
}
static ll decode(vi a, vi b) //converts this vector of #s to an integer
{
ll res = 0;
for (int x : a)
{
res *= 2;
res += x;
}
for (int x : b)
{
res *= 5;
res += x;
}
return res;
}
static vi getperm(ll x) //xth lexicographical permutation of length N-L
{
ll ways = 1;
FOR(i, 1, N - L + 1) ways *= i;
vi res(N - L);
FOR(i, 0, N - L)
{
update(i, 1);
}
FOR(i, 0, N - L)
{
ways /= (N - L - i);
res[i] = kth(x / ways);
update(res[i], -1);
x %= ways;
}
return res;
}
bool ask(int i, int j)
{
return (Compare(i, j) == -1);
}
vi sortnums(vi v)
{
//merge-insertion sort to sort v numbers.
// sort(ALL(v), ask); return v;
if (SZ(v) <= 1) return v;
map<int, int> match;
vi t;
for (int i = 1; i < SZ(v); i += 2)
{
bool b = ask(v[i - 1], v[i]);
if (!b) swap(v[i - 1], v[i]);
t.PB(v[i]);
match[v[i]] = v[i - 1];
}
t = sortnums(t);
vi res = t;
res.insert(res.begin(), match[t[0]]);
int pw = 4, l = 1, r = 3;
while(l < SZ(t))
{
int j = min(pw, SZ(res)) - 1;
FORD(i, min(r, SZ(t)), l)
{
while (res[j] != t[i])
{
j--;
}
res.insert(LB(res.begin(), res.begin() + j, match[t[i]], ask), match[t[i]]);
}
pw <<= 1;
l = r; r = pw - r;
}
if (SZ(v) & 1)
{
res.insert(LB(ALL(res), v.back(), ask), v.back());
}
return res;
}
vi SortWines(int k, vi P)
{
N = SZ(P); vi ans(N); return ans;
L = min(12, N);
vi n1, n2, todo;
FOR(i, 0, N)
{
if (P[i] <= 2)
{
n1.PB(P[i] - 1);
todo.PB(i);
}
else
{
n2.PB(P[i] - 3);
}
}
ll x = decode(n1, n2);
vi p0 = getperm(x);
vi p1 = sortnums(todo);
p1.resize(N);
int cnt = 0;
FOR(i, 0, N)
{
if (P[i] <= 2) continue;
p1[L + p0[cnt]] = i;
cnt++;
}
FOR(i, 0, N)
{
ans[p1[i]] = i + 1;
}
return ans;
//you got a vector P
//convert it to an integer, and then to an 18-permutation.
//then, we need to sort the rest of the 12.
}
Compilation message
bartender.cpp: In function 'vi BlendWines(int, vi)':
bartender.cpp:94:9: warning: unused variable 'it0' [-Wunused-variable]
int it0 = 0, it1 = 0;
^~~
bartender.cpp:94:18: warning: unused variable 'it1' [-Wunused-variable]
int it0 = 0, it1 = 0;
^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
772 KB |
Wrong |
2 |
Halted |
0 ms |
0 KB |
- |