Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #define CLEAR(x,v) (memset((x), (v), sizeof((x))))
- using namespace std;
- void
- solve(int prefs[][20], int v, int n, char names[][81])
- {
- int i, j, k, run[n], indices[v], scores[n];
- char foo[256];
- CLEAR(run, 1);
- CLEAR(indices, 0);
- CLEAR(scores, -1);
- /* count preferences */
- for (i = 0; i < v; i++) {
- scores[prefs[i][indices[i]] - 1]++;
- }
- while (1) {
- int min = n, max = 0;
- /* min max */
- for (k = 0; k < n; k++) {
- if (scores[k] > max)
- max = scores[k];
- if (scores[k] > -1 && scores[k] < min)
- min = scores[k];
- }
- /*
- fprintf(stdout, "\n");
- fprintf(stdout, "%d %d\n", min, max);
- for (i = 0; i < v; i++) {
- for (j = indices[i]; j < n; j++) {
- fprintf(stdout, "%d ", prefs[i][j]);
- }
- fprintf(stdout, "\n");
- }
- fprintf(stdout, "\n");
- sleep(1);
- */
- /*
- * max == min: all the remaining candindates tied.
- * max / n > 50: there is only one winner.
- */
- if (max == min || (100 * max / v > 50) ) {
- for (i = 0; i < n; i++) {
- if (scores[i] == max)
- fprintf(stdout, "%s", names[i]);
- }
- return;
- }
- /* unmark all the eliminated candidates */
- for (i = 0; i < n; i++) {
- if (scores[i] == min) {
- scores[i] = -1;
- run[i] = 0;
- }
- }
- /* redistribute votes */
- for (i = 0; i < v; i++) {
- if (run[prefs[i][indices[i]] - 1] == 0) {
- while (run[prefs[i][indices[i]] - 1] == 0) {
- indices[i]++;
- }
- scores[prefs[i][indices[i]] - 1]++;
- }
- }
- }
- }
- int
- main(int argc, char *argv[])
- {
- int cases, i, j, n, v;
- char *p;
- char names[20][81], buff[256];
- int prefs[1000][20];
- fscanf(stdin, "%d", &cases);
- fgets(buff, 256, stdin); /* skip empty line */
- while (cases--) {
- fscanf(stdin, "%d\n", &n);
- /* fprintf(stdout, "%d\n", n); */
- for (i = 0; i < n; i++) {
- fgets(names[i], 81, stdin);
- /* fprintf(stdout, "%s", names[i]); */
- }
- i = 0;
- while (fgets(buff, 256, stdin) && buff[0] != '\n') {
- /* fprintf(stdout, "%s", buff); */
- p = strtok(buff, " ");
- for (j = 0; j < n; j++) {
- sscanf(p, "%d", &v);
- prefs[i][j] = v;
- p = strtok(NULL, " ");
- }
- i++;
- }
- /* fprintf(stdout, "%s", buff); */
- solve(prefs, i, j, names);
- if (cases)
- fprintf(stdout, "\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement