# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
229516 | 534351 | Wine Tasting (FXCUP4_wine) | C++17 | 12 ms | 1028 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bartender.h"
#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_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 long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
static int N, K, L;
static vi ans;
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 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;
}
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;
}
pair<vi, 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
{
pair<vi, vi> res;
FOR(i, 0, N - L)
{
res.se.PB(x % 5);
x /= 5;
}
FOR(i, 0, L)
{
res.fi.PB(x % 2);
x /= 2;
}
return res;
}
vi BlendWines(int k, vi P)
{
N = SZ(P); ans.resize(N); K = k;
FOR(i, 0, N) P[i]--;
ord.resize(N); iota(ALL(ord), 0);
sort(ALL(ord), [&](int a, int b)
{
return P[a] < P[b];
});
//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);
auto s = conv(x);
int it0 = 0, it1 = 0;
FOR(i, 0, N)
{
if (P[i] >= L)
{
ans[i] = 3 + s.se.back();
s.se.pop_back();
}
else
{
ans[i] = 1 + s.fi.back();
s.fi.pop_back();
}
}
return ans;
}
#include "taster.h"
#include <bits/stdc++.h>
using namespace std;
#define MP make_pair
#define PB push_back
#define LB lower_bound
#define UB upper_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 long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef vector<pll> vpl;
static int N, K, L;
static vi ans;
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 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;
}
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) //converts this integer into a relative ordering for N-L numbers.
{
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;
//get the xth permutation for N-L numbers.
//this should conver
}
bool ask(int i, int j)
{
return (Compare(i, j) == -1);
}
vi sortnums(vi v)
{
//merge-insertion sort to sort v numbers.
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); ans.resize(N); K = k;
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 (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |