제출 #787967

#제출 시각아이디문제언어결과실행 시간메모리
787967ymm저울 (IOI15_scales)C++17
72.32 / 100
37 ms468 KiB
#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, int lim)
{
	random_shuffle(sel3.begin(), sel3.end());
	random_shuffle(sel4.begin(), sel4.end());
	//cerr << "Hi\n";
	//cerr << ps.size() << '\n';
	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 (mnch >= lim/3 && x < mnch){//x < mnch) {
			mnch = x;
			chvec = vec;
			ch = {(void *)getNextLightest, (void *)&sel};
		}
	}
	for (auto f : {mn, mx, med}) {
		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 (mnch >= lim/3 && x < mnch){//x < mnch) {
				mnch = x;
				chvec = vec;
				ch = {(void *)f2q(f), (void *)&sel};
			}
		}
	}
	//cerr << mnch << '\n';
	//cerr << chvec[0].size() << ' ' << chvec[1].size() << ' ' << chvec[2].size() << '\n';
	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], lim/3);
	} 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], lim/3);
	}
}

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) {
			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, 729);
	int w[6];
	Loop (i,0,6)
		w[ans[i]] = i+1;
	answer(w);
}

컴파일 시 표준 에러 (stderr) 메시지

scales.cpp: In function 'perm solve(const std::vector<std::array<int, 6> >&, int)':
scales.cpp:69:14: warning: conversion from 'long unsigned int' to 'int' may change value [-Wconversion]
   69 |   int x = max({vec[0].size(), vec[1].size(), vec[2].size()});
      |           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scales.cpp:82:15: warning: conversion from 'long unsigned int' to 'int' may change value [-Wconversion]
   82 |    int x = max({vec[0].size(), vec[1].size(), vec[2].size()});
      |            ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
scales.cpp: In function 'void init(int)':
scales.cpp:106:15: warning: unused parameter 'T' [-Wunused-parameter]
  106 | void init(int T) {
      |           ~~~~^
scales.cpp: In function 'void orderCoins()':
scales.cpp:124:16: warning: conversion from 'll' {aka 'long long int'} to 'int' may change value [-Wconversion]
  124 |   w[ans[i]] = i+1;
      |               ~^~
#Verdict Execution timeMemoryGrader output
Fetching results...