A
Albert
Imagine the swap function has been implemented, making using namespace
std unecessary in this C99 code. And remember
http://groups.google.com.au/group/c...ad/6cf98ca39c45ce7b/124648c23bd40ace?hl=en&q=
? This scores 100%. It took me over the span of a year (coming back to
the problem every so often) to debug it from a 99% solution. How could
I have written _the code below_ so it would have taken less time to
debug?
#include <stdio.h>
#include <assert.h>
using namespace std;
const int MAXSEATS = 30050;
const int TRUE = 1;
const int FALSE = 0;
struct Group {
int fseat, length;
} g[MAXSEATS];
Group pop();
void adjust_push();
int ngroups = 0;
int main() {
FILE* in = fopen("aflin.txt", "r"); /* file containing input */
assert(in != NULL);
FILE* out = fopen("aflout.txt", "w"); /* file for writing output
to */
assert(out != NULL);
int nseats;
assert(fscanf(in, "%d", &nseats) == 1);
int nbookedseats;
assert(fscanf(in, "%d", &nbookedseats) == 1);
bool seats[MAXSEATS + 10];
int i;
for (i = 1; i <= nseats; i++)
seats = FALSE;
for (i = 1; i <= nbookedseats; i++) {
int temp;
assert(fscanf(in, "%d", &temp) == 1);
seats[temp] = TRUE;
}
int currlength = 0;
for (i = 1; i <= nseats; i++) {
if (seats == FALSE)
currlength++;
if (i == nseats || seats == true) {
if (currlength == 0)
continue;
ngroups++;
g[ngroups].length = currlength;
g[ngroups].fseat = i - currlength;
if (i == nseats && seats == false)
g[ngroups].fseat++;
currlength = 0;
adjust_push();
}
}
int b;
assert(fscanf(in, "%d", &b) == 1);
for (int i = 1; i <= b; i++) {
int length;
assert(fscanf(in, "%d", &length) == 1);
Group temp = pop();
fprintf(out, "%d\n", temp.fseat);
if (temp.length - length == 0)
continue;
ngroups++;
g[ngroups].length = temp.length - length;
g[ngroups].fseat = temp.fseat + length;
adjust_push();
}
return 0;
}
Group pop() {
Group temp = g[1];
g[1] = g[ngroups];
ngroups--;
int idx = 1;
int child;
while(1) {
if (idx * 2 > ngroups)
return temp;
if (idx * 2 + 1 <= ngroups) {
if (g[idx * 2].length == g[idx * 2 + 1].length)
if (g[idx * 2].fseat < g[idx * 2 + 1].fseat)
child = idx * 2;
else
child = idx * 2 + 1;
if (g[idx * 2].length < g[idx * 2 + 1].length)
child = idx * 2 + 1;
else if (g[idx * 2].length > g[idx * 2 + 1].length)
child = idx * 2;
}
else
child = idx * 2;
if (g[idx].length == g[child].length)
if (g[idx].fseat > g[child].fseat)
swap(g[idx], g[child]);
if (g[idx].length < g[child].length)
swap(g[idx], g[child]);
idx = child;
}
return temp;
}
void adjust_push() {
int idx = ngroups;
while (1) {
int parent;
if (idx > 1)
parent = idx / 2;
else
return;
if (g[idx].length == g[parent].length)
if (g[idx].fseat < g[parent].fseat)
swap(g[idx], g[parent]);
if (g[idx].length > g[parent].length)
swap(g[idx], g[parent]);
idx = parent;
}
std unecessary in this C99 code. And remember
http://groups.google.com.au/group/c...ad/6cf98ca39c45ce7b/124648c23bd40ace?hl=en&q=
? This scores 100%. It took me over the span of a year (coming back to
the problem every so often) to debug it from a 99% solution. How could
I have written _the code below_ so it would have taken less time to
debug?
#include <stdio.h>
#include <assert.h>
using namespace std;
const int MAXSEATS = 30050;
const int TRUE = 1;
const int FALSE = 0;
struct Group {
int fseat, length;
} g[MAXSEATS];
Group pop();
void adjust_push();
int ngroups = 0;
int main() {
FILE* in = fopen("aflin.txt", "r"); /* file containing input */
assert(in != NULL);
FILE* out = fopen("aflout.txt", "w"); /* file for writing output
to */
assert(out != NULL);
int nseats;
assert(fscanf(in, "%d", &nseats) == 1);
int nbookedseats;
assert(fscanf(in, "%d", &nbookedseats) == 1);
bool seats[MAXSEATS + 10];
int i;
for (i = 1; i <= nseats; i++)
seats = FALSE;
for (i = 1; i <= nbookedseats; i++) {
int temp;
assert(fscanf(in, "%d", &temp) == 1);
seats[temp] = TRUE;
}
int currlength = 0;
for (i = 1; i <= nseats; i++) {
if (seats == FALSE)
currlength++;
if (i == nseats || seats == true) {
if (currlength == 0)
continue;
ngroups++;
g[ngroups].length = currlength;
g[ngroups].fseat = i - currlength;
if (i == nseats && seats == false)
g[ngroups].fseat++;
currlength = 0;
adjust_push();
}
}
int b;
assert(fscanf(in, "%d", &b) == 1);
for (int i = 1; i <= b; i++) {
int length;
assert(fscanf(in, "%d", &length) == 1);
Group temp = pop();
fprintf(out, "%d\n", temp.fseat);
if (temp.length - length == 0)
continue;
ngroups++;
g[ngroups].length = temp.length - length;
g[ngroups].fseat = temp.fseat + length;
adjust_push();
}
return 0;
}
Group pop() {
Group temp = g[1];
g[1] = g[ngroups];
ngroups--;
int idx = 1;
int child;
while(1) {
if (idx * 2 > ngroups)
return temp;
if (idx * 2 + 1 <= ngroups) {
if (g[idx * 2].length == g[idx * 2 + 1].length)
if (g[idx * 2].fseat < g[idx * 2 + 1].fseat)
child = idx * 2;
else
child = idx * 2 + 1;
if (g[idx * 2].length < g[idx * 2 + 1].length)
child = idx * 2 + 1;
else if (g[idx * 2].length > g[idx * 2 + 1].length)
child = idx * 2;
}
else
child = idx * 2;
if (g[idx].length == g[child].length)
if (g[idx].fseat > g[child].fseat)
swap(g[idx], g[child]);
if (g[idx].length < g[child].length)
swap(g[idx], g[child]);
idx = child;
}
return temp;
}
void adjust_push() {
int idx = ngroups;
while (1) {
int parent;
if (idx > 1)
parent = idx / 2;
else
return;
if (g[idx].length == g[parent].length)
if (g[idx].fseat < g[parent].fseat)
swap(g[idx], g[parent]);
if (g[idx].length > g[parent].length)
swap(g[idx], g[parent]);
idx = parent;
}