Posted: Mon Mar 20, 2006 9:13 am
EDIT: Accepted
Code: Select all
1
3 1 11 C
3 1 17 I
Code: Select all
3 1 17
Code: Select all
#define pb( x ) push_back( x )
#define mp( x, y ) make_pair( x, y )
void trunc() // truncates line until newline character
{
char c;
while( (c = getchar()) != '\n' );
};
void nullify( const set<int>& s, int mm[100+13][10+13] )
{
for( set<int>::iterator it = s.begin(); it != s.end(); ++it )
for( int i = 1; i < 10; ++i ) mm[*it][i] = INT_MIN;
};
// struct of results
struct Rank
{
int c;
int p;
int pt;
Rank( int a ): c(a), p(0), pt(0) {};
Rank( int a, int b, int d ): c(a), p(b), pt(d) {};
bool operator >( const Rank& r ) const
{
if( this->p > r.p ) return true;
else if( this->p < r.p ) return false;
else if( this->pt < r.pt ) return true;
else if( this->pt > r.pt ) return false;
else return this->c < r.c;
};
};
// prints results
void printScoreTable( const set<int>& s, int mm[100+13][10+13] )
{
vector<Rank> v;
for( set<int>::iterator it = s.begin(); it != s.end(); ++it )
{
Rank r(*it);
for( int i = 1; i < 10; ++i )
if( mm[*it][i] >= 0 )
{
++r.p;
r.pt += mm[*it][i];
}
v.pb( r );
}
sort( v.begin(), v.end(), greater<Rank>() );
for( int i = 0; i < v.size(); ++i ) printf( "%d %d %d\n", v[i].c, v[i].p, v[i].pt );
};
int main( void )
{
int mm[100+13][10+13];
for( int i = 0; i < 100+13; ++i )
for( int j = 0; j < 10; ++j ) mm[i][j] = INT_MIN;
set<int> co;
char str[128];
int cases;
scanf( "%d", &cases ); trunc(); trunc();
for( int x = 0; x < cases; ++x )
{
while( strlen( gets( str ) ) != 0 )
{
int a, b, c; char d;
sscanf( str, "%d %d %d %c", &a, &b, &c, &d );
co.insert( a );
if( d == 'I' && mm[a][b] < 0 ) mm[a][b] += 20; // netocni submitovi prije tocnoga
else if( d == 'C' && mm[a][b] < 0 ) mm[a][b] = mm[a][b] - INT_MIN + c;
}
printScoreTable( co, mm ); if( x != cases-1 ) putchar( '\n' );
nullify( co, mm );
co.clear();
}
//system( "pause" );
return 0;
}
Code: Select all
while(gets(str) && strlen(str)) {...}
Thank you.gtbass wrote:Code: Select all
while(gets(str) && strlen(str)) {...}
Code: Select all
#include <stdio.h>
struct team {
int number, solved, time;
bool submit, sp[10];
};
int t, l, i, num, minute, problem, L[5], j;
char s[100];
team teams[105], tmp;
int main()
{
scanf("%d", &t);
gets(s);
gets(s);
for (l = 0; l < t; l++)
{
if (l) puts("");
for (i = 0; i < 105; i++)
{
teams[i].submit = 0;
teams[i].number = i+1;
teams[i].solved = 0;
teams[i].time = 0;
for (j = 0; j < 10; j++)
teams[i].sp[j] = 0;
}
while(gets(s) && s[0])
{
sscanf(s, "%d%d%d%s", &num, &problem, &minute, &L);
num--;
if (L[0] == 'I')
{
teams[num].submit = 1;
if (!teams[num].sp[problem]) teams[num].time += 20;
}
if (L[0] == 'C')
{
teams[num].submit = 1;
if (!teams[num].sp[problem]) teams[num].solved++;
if (!teams[num].sp[problem]) teams[num].time += minute;
teams[num].sp[problem] = 1;
}
}
for (i = 0; i < 104; i++)
for (j = 0; j < 104; j++)
if (teams[j].submit < teams[j+1].submit)
{
tmp = teams[j];
teams[j] = teams[j+1];
teams[j+1] = tmp;
}
for (i = 0; i < 104; i++)
for (j = 0; j < 104; j++)
if (teams[j].solved < teams[j+1].solved)
{
tmp = teams[j];
teams[j] = teams[j+1];
teams[j+1] = tmp;
}
for (i = 0; i < 104; i++)
for (j = 0; j < 104; j++)
if ((teams[j].solved == teams[j+1].solved) && (teams[j].time > teams[j+1].time))
{
tmp = teams[j];
teams[j] = teams[j+1];
teams[j+1] = tmp;
}
for (i = 0; i < 105, teams[i].submit; i++)
printf("%d %d %d\n", teams[i].number, teams[i].solved, teams[i].time);
}
}
Code: Select all
1
1 2 10 I
3 1 10 I
1 2 11 C
3 1 11 C
1 4 20 I
Code: Select all
1 1 31
3 1 31
Code: Select all
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#define false 0
#define true !false
#define MAX(X,Y) ((X)>(Y)?(X):(Y))
#define MIN(X,Y) ((X)>(Y)?(Y):(X))
struct sVetor2 {
int *numDesafio;
int *acertou;
int *penalidades;
int n, tam;
};
typedef struct sVetor2 vetor2;
struct sVetor {
int *numJogador;
int *qtdeAcertos;
int *totPenalidades;
vetor2 *desafios;
int n, tam;
};
typedef struct sVetor vetor;
struct sVetor3 {
int *caso;
vetor *jogadores;
int n, tam;
};
typedef struct sVetor3 vetor3;
/* prototipo das funcoes definidas sobre vetor */
void inicializa(vetor *s, int t);
void inicializa2(vetor2 *s, int t);
void inicializa3(vetor3 *s, int t);
void insere(vetor *s, int numJogador);
int tamanho(vetor *s);
void Ordena(vetor *);
#define DEBUG(x)
int main(int argc, char *argv[])
{
vetor3 Campeonatos;
vetor Jogadores;
vetor2 DesafioAux;
int ehPrimeira = true;
char linha[10002], result[2];
int n, numJogador, numDesafio, qtdePenalidade, index;
assert(fgets(linha, 10000, stdin) != NULL);
n = atoi(linha);
DEBUG(printf("entrada n=%d\n", n);)
inicializa3(&Campeonatos, n);
/* le linha em branco */
assert(fgets(linha, 10000, stdin) != NULL);
while(n>0)
{
(&Campeonatos)->caso[n-1] = n;
Jogadores = (&Campeonatos)->jogadores[n-1];
assert(fgets(linha, 10000, stdin) != NULL);
while(sscanf(linha, "%d %d %d %s", &numJogador, &numDesafio, &qtdePenalidade, result)==4)
{
/*GUARDA OS DADOS DE UMA LINHA EM ALGUM LUGAR
Eles estao disponiveis em a, b, c, s[0]
*/
int posJogador = VerificaPosicao(&Jogadores, numJogador);
if (posJogador == (&Jogadores)->n)
insere(&Jogadores, numJogador);
DesafioAux = (&Jogadores)->desafios[posJogador];
if (!(&DesafioAux)->acertou[numDesafio-1])
{
if (result[0] == 'C')
{
(&Jogadores)->qtdeAcertos[posJogador]++;
//penalidade
(&Jogadores)->totPenalidades[posJogador] += qtdePenalidade;
//incrementa com a penalidade das incorretas
(&Jogadores)->totPenalidades[posJogador] += ((&DesafioAux)->penalidades[numDesafio-1] * 20);
(&DesafioAux)->acertou[numDesafio-1] = true;
}
else if (result[0] == 'I')
{
(&DesafioAux)->penalidades[numDesafio-1]++;
}
}
/* le a proxima linha */
if(fgets(linha, 10000, stdin) == NULL) break;
}
Ordena(&Jogadores);
(&Campeonatos)->jogadores[n-1] = Jogadores;
DEBUG(printf("fim do contest\n");)
n--;
}
for (index=(&Campeonatos)->tam - 1; index >=0; index--)
{
Jogadores = (&Campeonatos)->jogadores[index];
if (index < (&Campeonatos)->tam - 1)
printf("\n\n");
int indexAux;
for (indexAux=0; indexAux < (&Jogadores)->n; indexAux++)
{
if(indexAux > 0)
printf("\n");
printf("%d %d %d ",
(&Jogadores)->numJogador[indexAux],
(&Jogadores)->qtdeAcertos[indexAux],
(&Jogadores)->totPenalidades[indexAux]);
}
}
DEBUG(printf("fim\n");)
return 0;
}
void Ordena(vetor *s){
int aInserir[3];
int i;
for (i=1; i<s->n; i++)
{
aInserir[0] = s->numJogador[i];
aInserir[1] = s->qtdeAcertos[i];
aInserir[2] = s->totPenalidades[i];
int j=i;
while((j > 0) && (
s->qtdeAcertos[j-1] < aInserir[1] ||
(s->qtdeAcertos[j-1] == aInserir[1] && s->totPenalidades[j-1] > aInserir[2]) ||
(s->qtdeAcertos[j-1] == aInserir[1] && s->totPenalidades[j-1] == aInserir[2] && s->numJogador[j-1] > aInserir[0]) ||
(s->qtdeAcertos[j-1] == 0 && s->numJogador[j-1] > aInserir[0])
))
{
s->numJogador[j] = s->numJogador[j-1];
s->qtdeAcertos[j] = s->qtdeAcertos[j-1];
s->totPenalidades[j] = s->totPenalidades[j-1];
j--;
}
s->numJogador[j] = aInserir[0];
s->qtdeAcertos[j] = aInserir[1];
s->totPenalidades[j] = aInserir[2];
}
}
int VerificaPosicao(vetor *s, int numJogador)
{
int index;
for (index=0; index < s->n; index++)
{
if (s->numJogador[index] == numJogador)
return index;
}
return index;
}
/* inicializacao de uma estrutura vetor. Recebe um
ponteiro s para a estrutura a ser inicializada e
o tamanho t>0 desejado para o array. */
void inicializa(vetor *s, int t) {
/* comeca o n com zero, indicando que nada esta
armazenado em s->v por enquanto */
s->n = 0;
s->tam = t;
s->numJogador = malloc(sizeof(int)*t);
s->qtdeAcertos = malloc(sizeof(int)*t);
s->totPenalidades = malloc(sizeof(int)*t);
s->desafios = malloc(sizeof(vetor2)*t);
int index;
for (index=0; index <= t-1; index++)
{
inicializa2(&(s->desafios[index]), 10);
}
}
void inicializa2(vetor2 *s, int t) {
/* comeca o n com zero, indicando que nada esta
armazenado em s->v por enquanto */
s->n = 0;
s->tam = t;
s->numDesafio = malloc(sizeof(int)*t);
s->acertou = malloc(sizeof(int)*t);
s->penalidades = malloc(sizeof(int)*t);
int index;
for (index=1; index <= t; index++)
{
s->numDesafio[index-1] = index;
s->acertou[index-1] = false;
s->penalidades[index-1] = 0;
}
}
void inicializa3(vetor3 *s, int t) {
/* comeca o n com zero, indicando que nada esta
armazenado em s->v por enquanto */
s->n = 0;
s->tam = t;
s->jogadores = malloc(sizeof(vetor)*t);
s->caso = malloc(sizeof(int)*t);
int index;
for (index=0; index <= t-1; index++)
{
inicializa(&(s->jogadores[index]), 100);
}
}
/* insercao de elemento na estrutura vetor. Recebe um
ponteiro s para a estrutura, a posicao pos para ser
inserido o elemento e o valor elem do elemento */
void insere(vetor *s, int numJogador) {
int i;
/* se tamanho ja eh s->tam, entao nao temos mais espaco para
inserir novos elementos */
if(tamanho(s)>=s->tam)
perror("Vetor cheio");
/* coloca o elem no array v */
s->numJogador[s->n] = numJogador;
s->qtdeAcertos[s->n] = 0;
s->totPenalidades[s->n] = 0;
/* incrementa a quantidade de elementos armazenados ate o momento */
s->n++;
}
/* devolve quantos elementos estao armazenados na estrutura s */
int tamanho(vetor *s) {
/* simplesmente retorna o n da estrutura */
return s->n;
}
Code: Select all
printf("%d %d %d ",
(&Jogadores)->numJogador[indexAux],
(&Jogadores)->qtdeAcertos[indexAux],
(&Jogadores)->totPenalidades[indexAux]);
Code: Select all
The outputs of two consecutive cases will be separated by a blank line.