# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
787982 | ymm | Scales (IOI15_scales) | C++17 | 33 ms | 488 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 "scales.h"
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (l); --x)
typedef std::pair<int,int> pii;
typedef long long ll;
using namespace std;
typedef array<int, 6> perm;
int whichval(int, int b, int c, int x) {
return (b == x) + 2*(c == x);
}
tuple<int,int,int> srt(const perm &p, int a, int b, int c) {
if (p[a] > p[b]) swap(a, b);
if (p[b] > p[c]) swap(b, c);
if (p[a] > p[b]) swap(a, b);
return {a, b, c};
}
int mn(const perm &p, int a, int b, int c) {
auto [x, y, z] = srt(p, a, b, c);
return whichval(a, b, c, x);
}
int mx(const perm &p, int a, int b, int c) {
auto [x, y, z] = srt(p, a, b, c);
return whichval(a, b, c, z);
}
int med(const perm &p, int a, int b, int c) {
auto [x, y, z] = srt(p, a, b, c);
return whichval(a, b, c, y);
}
int nxt(const perm &p, int a, int b, int c, int d) {
auto [x, y, z] = srt(p, a, b, c);
if (p[d] < p[x]) return whichval(a, b, c, x);
if (p[d] < p[y]) return whichval(a, b, c, y);
if (p[d] < p[z]) return whichval(a, b, c, z);
return whichval(a, b, c, x);
}
vector<tuple<int,int,int>> sel3;
vector<tuple<int,int,int,int>> sel4;
vector<perm> perms;
int (*f2q(int (*f)(const perm &, int, int, int)))(int, int, int) {
if (f == mn) return getLightest;
if (f == mx) return getHeaviest;
if (f == med) return getMedian;
return NULL;
}
perm solve(const vector<perm> &ps)
{
if (ps.size() == 1)
return ps[0];
int mnch = INT_MAX;
array<vector<perm>, 3> chvec;
pair<void *, void *> ch;
for (auto &sel : sel4) {
auto [a, b, c, d] = sel;
array<vector<perm>, 3> vec;
for (const auto &p : ps)
vec[nxt(p, a, b, c, d)].push_back(p);
int x = max({vec[0].size(), vec[1].size(), vec[2].size()});
if (x < mnch) {
mnch = x;
chvec = vec;
ch = {(void *)getNextLightest, (void *)&sel};
}
}
for (auto f : {med, mn, mx}) {
for (auto &sel : sel3) {
auto [a, b, c] = sel;
array<vector<perm>, 3> vec;
for (const auto &p : ps)
vec[f(p, a, b, c)].push_back(p);
int x = max({vec[0].size(), vec[1].size(), vec[2].size()});
if (x < mnch) {
mnch = x;
chvec = vec;
ch = {(void *)f2q(f), (void *)&sel};
}
}
}
if (ch.first == getNextLightest) {
auto [a, b, c, d] = *(tuple<int,int,int,int> *)ch.second;
int x = getNextLightest(a+1, b+1, c+1, d+1)-1;
int i = whichval(a, b, c, x);
return solve(chvec[i]);
} else {
auto f = (int (*)(int, int, int))ch.first;
auto [a, b, c] = *(tuple<int,int,int> *)ch.second;
int x = f(a+1, b+1, c+1)-1;
int i = whichval(a, b, c, x);
return solve(chvec[i]);
}
}
void init(int T) {
Loop (i,0,6) Loop (j,i+1,6) Loop (k,j+1,6)
sel3.emplace_back(i, j, k);
Loop (d,0,6) {
Loop (i,0,6) Loop (j,i+1,6) Loop (k,j+1,6) {
if (i != d && j != d && k != d)
sel4.emplace_back(i, j, k, d);
}
}
perm p = {0, 1, 2, 3, 4, 5};
do
perms.push_back(p);
while (next_permutation(p.begin(), p.end()));
}
void orderCoins() {
auto ans = solve(perms);
int w[6];
Loop (i,0,6)
w[ans[i]] = i+1;
answer(w);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |