Submission #229518

# Submission time Handle Problem Language Result Execution time Memory
229518 2020-05-04T22:12:01 Z 534351 Wine Tasting (FXCUP4_wine) C++17
100 / 100
11 ms 1076 KB
#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, 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 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;
}
static 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]--;
    //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 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, 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 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); 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

bartender.cpp: In function 'vi BlendWines(int, vi)':
bartender.cpp:86:9: warning: unused variable 'it0' [-Wunused-variable]
     int it0 = 0, it1 = 0;
         ^~~
bartender.cpp:86:18: warning: unused variable 'it1' [-Wunused-variable]
     int it0 = 0, it1 = 0;
                  ^~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 884 KB Correct
2 Correct 10 ms 772 KB Correct
3 Correct 10 ms 772 KB Correct
4 Correct 10 ms 772 KB Correct
5 Correct 10 ms 780 KB Correct
6 Correct 10 ms 908 KB Correct
7 Correct 11 ms 772 KB Correct
8 Correct 11 ms 904 KB Correct
9 Correct 10 ms 824 KB Correct
10 Correct 9 ms 772 KB Correct
11 Correct 10 ms 884 KB Correct
12 Correct 9 ms 772 KB Correct
13 Correct 10 ms 772 KB Correct
14 Correct 10 ms 772 KB Correct
15 Correct 10 ms 908 KB Correct
16 Correct 10 ms 772 KB Correct
17 Correct 11 ms 908 KB Correct
18 Correct 11 ms 644 KB Correct
19 Correct 10 ms 772 KB Correct
20 Correct 9 ms 772 KB Correct
21 Correct 10 ms 804 KB Correct
22 Correct 9 ms 644 KB Correct
23 Correct 10 ms 772 KB Correct
24 Correct 10 ms 772 KB Correct
25 Correct 9 ms 644 KB Correct
26 Correct 10 ms 780 KB Correct
27 Correct 10 ms 772 KB Correct
28 Correct 9 ms 772 KB Correct
29 Correct 11 ms 908 KB Correct
30 Correct 10 ms 820 KB Correct
31 Correct 10 ms 772 KB Correct
32 Correct 9 ms 908 KB Correct
33 Correct 10 ms 940 KB Correct
34 Correct 8 ms 740 KB Correct
35 Correct 9 ms 772 KB Correct
36 Correct 10 ms 772 KB Correct
37 Correct 10 ms 908 KB Correct
38 Correct 10 ms 644 KB Correct
39 Correct 10 ms 780 KB Correct
40 Correct 10 ms 956 KB Correct
41 Correct 9 ms 772 KB Correct
42 Correct 10 ms 908 KB Correct
43 Correct 9 ms 780 KB Correct
44 Correct 10 ms 772 KB Correct
45 Correct 9 ms 780 KB Correct
46 Correct 9 ms 772 KB Correct
47 Correct 10 ms 772 KB Correct
48 Correct 9 ms 908 KB Correct
49 Correct 9 ms 908 KB Correct
50 Correct 9 ms 772 KB Correct
51 Correct 10 ms 692 KB Correct
52 Correct 10 ms 732 KB Correct
53 Correct 10 ms 772 KB Correct
54 Correct 9 ms 772 KB Correct
55 Correct 10 ms 772 KB Correct
56 Correct 10 ms 772 KB Correct
57 Correct 10 ms 772 KB Correct
58 Correct 10 ms 780 KB Correct
59 Correct 10 ms 772 KB Correct
60 Correct 11 ms 772 KB Correct
61 Correct 11 ms 908 KB Correct
62 Correct 10 ms 772 KB Correct
63 Correct 9 ms 772 KB Correct
64 Correct 9 ms 772 KB Correct
65 Correct 10 ms 780 KB Correct
66 Correct 10 ms 1076 KB Correct
67 Correct 11 ms 780 KB Correct
68 Correct 11 ms 864 KB Correct
69 Correct 10 ms 908 KB Correct
70 Correct 10 ms 908 KB Correct
71 Correct 10 ms 772 KB Correct
72 Correct 10 ms 784 KB Correct
73 Correct 10 ms 908 KB Correct
74 Correct 9 ms 772 KB Correct
75 Correct 9 ms 772 KB Correct
76 Correct 10 ms 772 KB Correct
77 Correct 9 ms 780 KB Correct
78 Correct 10 ms 772 KB Correct
79 Correct 9 ms 772 KB Correct
80 Correct 10 ms 772 KB Correct
81 Correct 8 ms 772 KB Correct
82 Correct 10 ms 808 KB Correct
83 Correct 8 ms 908 KB Correct
84 Correct 9 ms 980 KB Correct
85 Correct 10 ms 772 KB Correct
86 Correct 9 ms 772 KB Correct
87 Correct 11 ms 772 KB Correct
88 Correct 9 ms 772 KB Correct
89 Correct 11 ms 908 KB Correct
90 Correct 8 ms 676 KB Correct
91 Correct 9 ms 772 KB Correct
92 Correct 10 ms 824 KB Correct
93 Correct 9 ms 772 KB Correct
94 Correct 9 ms 772 KB Correct
95 Correct 10 ms 732 KB Correct
96 Correct 9 ms 772 KB Correct
97 Correct 9 ms 772 KB Correct
98 Correct 10 ms 772 KB Correct
99 Correct 9 ms 644 KB Correct
100 Correct 8 ms 780 KB Correct
101 Correct 9 ms 644 KB Correct
102 Correct 10 ms 908 KB Correct
103 Correct 8 ms 644 KB Correct
104 Correct 9 ms 772 KB Correct
105 Correct 9 ms 644 KB Correct
106 Correct 9 ms 772 KB Correct
107 Correct 9 ms 772 KB Correct
108 Correct 10 ms 908 KB Correct
109 Correct 8 ms 772 KB Correct
110 Correct 10 ms 772 KB Correct
111 Correct 10 ms 772 KB Correct
112 Correct 10 ms 908 KB Correct
113 Correct 9 ms 772 KB Correct
114 Correct 10 ms 908 KB Correct
115 Correct 10 ms 908 KB Correct
116 Correct 10 ms 820 KB Correct
117 Correct 10 ms 824 KB Correct
118 Correct 9 ms 908 KB Correct
119 Correct 9 ms 780 KB Correct
120 Correct 10 ms 772 KB Correct