From 3b08302c20ab9ed7156e0b8287f5506e06858a27 Mon Sep 17 00:00:00 2001 From: palmkeep Date: Tue, 13 Jul 2021 15:51:33 +0200 Subject: [PATCH] Passed: The Uxuhul Voting System --- the-uxuhul-voting-system/Makefile | 16 ++++++ the-uxuhul-voting-system/d0 | 8 +++ the-uxuhul-voting-system/main.cpp | 88 +++++++++++++++++++++++++++++++ 3 files changed, 112 insertions(+) create mode 100644 the-uxuhul-voting-system/Makefile create mode 100644 the-uxuhul-voting-system/d0 create mode 100644 the-uxuhul-voting-system/main.cpp diff --git a/the-uxuhul-voting-system/Makefile b/the-uxuhul-voting-system/Makefile new file mode 100644 index 0000000..7022988 --- /dev/null +++ b/the-uxuhul-voting-system/Makefile @@ -0,0 +1,16 @@ +EXE=a.out +CC=g++ +CFLAGS=-g -lm -O2 -std=gnu++17 -Wall -Wextra -Weffc++ +OBJ=main.cpp + +.PHONY: run +run: $(EXE) + ./$(EXE) +#include + +int prefs[100][8]; +int choice[100][8]; + + +void print_result(int res) +{ + int d3 = (res>>2); + int d2 = (res>>1)&1; + int d1 = (res)&1; + + char c3 = 'Y'*(d3 == 1) + 'N'*(d3 == 0); + char c2 = 'Y'*(d2 == 1) + 'N'*(d2 == 0); + char c1 = 'Y'*(d1 == 1) + 'N'*(d1 == 0); + printf("%c%c%c\n", c3, c2, c1); +} + +void round(int n_pr) +{ + // Get preferences + for (int i{0}; i < n_pr; ++i) { + scanf("%d %d %d %d %d %d %d %d\n", + &prefs[i][0], &prefs[i][1], &prefs[i][2], &prefs[i][3], + &prefs[i][4], &prefs[i][5], &prefs[i][6], &prefs[i][7] + ); + } + + for (int pr{n_pr-1}; 0 <= pr; --pr) + { + // For each possible state (states=[0, 7]) before a stone is turned + // see which of the 3 outcome states would be most prefered by the + // current priest and set that as the priests choice in the array + if (pr < n_pr-1) { + for (int c{0}; c < 8; ++c) + { + // Min value == highest preference + int min_i = c^1; + int min = prefs[pr][ choice[pr+1][c^1] ]; + + if (prefs[pr][ choice[pr+1][c^2] ] < min) { + min_i = c^2; + min = prefs[pr][ choice[pr+1][c^2] ]; + } + if (prefs[pr][ choice[pr+1][c^4] ] < min) { + min_i = c^4; + min = prefs[pr][ choice[pr+1][c^4] ]; + } + choice[pr][c] = choice[pr+1][min_i]; + } + } else { + for (int c{0}; c < 8; ++c) + { + int min_i = c^1; + int min = prefs[pr][c^1]; + + if (prefs[pr][c^2] < min) { + min_i = c^2; + min = prefs[pr][c^2]; + } + + if (prefs[pr][c^4] < min) { + min_i = c^4; + min = prefs[pr][c^4]; + } + choice[pr][c] = min_i; + } + } + } + + // Last priest gets to choose which one of 1,2,4 that he likes best + print_result(choice[0][0]); +} + + + + +int main() +{ + int n_rounds, n_priests; + scanf("%d\n", &n_rounds); + while (0 < n_rounds--) + { + scanf("%d\n", &n_priests); + round(n_priests); + } +}