Skip to content

Instantly share code, notes, and snippets.

@taiar
Created May 7, 2011 14:24
Show Gist options
  • Save taiar/960532 to your computer and use it in GitHub Desktop.
Save taiar/960532 to your computer and use it in GitHub Desktop.
GCJ #1
/*
* qr.c
*
* Created on: 07/05/2011
* Author: taiar
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int *oV;
int *bV;
int nTests;
int nButtons;
char buffChar;
int buffInt;
int i, j, oInd, bInd;
int oActualInd, bActualInd, oActualPos, bActualPos, oPressed, bPressed;
int nRound = 0, totalTime, timeRoleFlag;
scanf("%d\n", &nTests);
for (i = 0; i < nTests; i += 1) {
nRound += 1;
totalTime = 0;
scanf("%d", &nButtons);
oV = (int*) malloc(nButtons * sizeof(int));
bV = (int*) malloc(nButtons * sizeof(int));
oInd = 0;
bInd = 0;
for (j = 0; j < nButtons; j += 1) {
scanf(" %c %d", &buffChar, &buffInt);
if (buffChar == 'O') {
oV[oInd] = buffInt;
oInd += 1;
} else if (buffChar == 'B') {
bV[bInd] = buffInt;
bInd += 1;
}
}
oActualInd = 0;
bActualInd = 0;
oPressed = 0;
bPressed = 0;
oActualPos = 1;
bActualPos = 1;
while (oActualInd < oInd || bActualInd < bInd) {
timeRoleFlag = 0;
if (oActualPos != oV[oActualInd]) {
if (oActualPos > oV[bActualInd])
oActualPos -= 1;
else
oActualPos += 1;
timeRoleFlag = 1;
} else {
if (!oPressed) {
oPressed = 1;
timeRoleFlag = 1;
} else {
oPressed = 0;
oActualInd += 1;
if (oActualPos > oV[bActualInd])
oActualPos -= 1;
else
oActualPos += 1;
timeRoleFlag = 1;
}
}
if (bActualPos != bV[bActualInd]) {
if (bActualPos > bV[bActualInd]) {
bActualPos -= 1;
//printf("subtrai\n");
} else {
bActualPos += 1;
//printf("adiciona\n");
}
timeRoleFlag = 1;
//printf("1\n");
} else {
if (!bPressed) {
bPressed = 1;
timeRoleFlag = 1;
//printf("2\n");
} else {
bPressed = 0;
bActualInd += 1;
if (bActualPos > bV[bActualInd])
bActualPos -= 1;
else
bActualPos += 1;
timeRoleFlag = 1;
//printf("3\n");
}
}
if (timeRoleFlag)
totalTime += 1;
if(!oV[oActualInd] || !bV[bActualInd]) break;
//printf("%d %d - %d %d\n", oActualInd, oInd, bActualPos, bV[bActualInd]);
}
free(oV);
free(bV);
printf("Case #%d: %d\n", nRound, totalTime);
}
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment