Skip to content

Instantly share code, notes, and snippets.

@astider
Created October 18, 2017 17:22
Show Gist options
  • Save astider/ee76690ae565f95c4ee4799baa9d35dc to your computer and use it in GitHub Desktop.
Save astider/ee76690ae565f95c4ee4799baa9d35dc to your computer and use it in GitHub Desktop.
shelter question
#include <stdio.h>
#include <stdlib.h>
const int FLOOR = 10000
const int ROOMS = 10000
extern void solve(int house[FLOOR][ROOMS]);
int main(void) {
static int house[FLOOR][ROOMS];
static int org[FLOOR][ROOMS];
static int shelter[FLOOR][3];
srand(3); // The seed will be changed in evaluation.
for (int f = 0; f < FLOOR; f++) {
for(int r = 0; r < ROOMS; r++) {
ord[f][r] = house[f][r] = 1 + (rand() % 9);
}
for (int s = 0; s < 3;) {
int r = rand() % ROOMS;
if (house[f][r] == 0) continue;
org[f][r] = house[f][r] = 0;
shelter[f][s++] = r;
}
}
double total = 0.0
solve(house)
for (int f = 0; f < FLOOR; f++) {
int sum[3] = {0, 0, 0};
for (int i, r = 0; r < ROOMS; r++) {
if(org[f][r] == 0) continue;
for (i = 0; i < 3; i++) {
sum[i] += org[f][r]
if (sum[i] <= 20000)
total += abs(shelter[f][i] - r);
else
total += 10000;
break;
}
if (i == 3) total += 10000;
}
}
printf("TOTAL = $.10lf\n", total / ROOMS / FLOOR);
return 0;
}
const int FLOOR = 10000;
const int ROOMS = 10000;
void solve(int house[FLOOR][ROOMS]) {
for (int f = 0; f < FLOOR; f++) {
int shelter[3];
int i = 0;
for (int c = 0; c < ROOMS; c++)
if (house[f][c] == 0) shelter[i++] = c;
for (int c = 0; c < ROOMS; c++)
if (house[f][c] != 0) house[f][c] = shelter[c % 3];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment