sábado, 17 de noviembre de 2012

Programación - Programas de practica

Hoy muestro la solución de uno de los problemas de practica de la web codechef.com, titulado Closind the Tweets.
Aqui el enunciado:

Little kids, Jack and Evan like playing their favorite game Glass-and-Stone. Today they want to play something new and came across Twitter on their father's laptop.
They saw it for the first time but were already getting bored to see a bunch of sentences having at most 140 characters each. The only thing they liked to play with it is, closing and opening tweets.
There are N tweets on the page and each tweet can be opened by clicking on it, to see some statistics related to that tweet. Initially all the tweets are closed. Clicking on an open tweet closes it and clicking on a closed tweet opens it. There is also a button to close all the open tweets. Given a sequence of K clicks by Jack, Evan has to guess the total number of open tweets just after each click. Please help Evan in this game. 

Código:
#include &ltcstdlib>
#include &ltiostream>
#include &ltstdio.h>
#include &ltcstring>
#include &ltmath.h>
using namespace std;

inline void fastRead_string(string *a){
     register char c=0;
     while (c&lt33) c=getchar();
     *a="";
     while (c>33)
     {
         *a+=c;
         c=getchar();
     }
}
inline void fastRead(int *a){
     register char c=0;
     while (c&lt33) c=getchar();
     *a=0;
     while (c>33)
     {
         *a=*a*10+c-'0';
         c=getchar();
     }
}
int main(int argc, char *argv[])
{
    int N,K;
    fastRead(&N);
    fastRead(&K);
    int tweets[N];
    memset(tweets, 0, sizeof(tweets));
    int click_tweet;
    int tweet_open=0;
    string read;
    for(int i=0; i<K; i++){
        fastRead_string(&read);
        if(read[read.length()-1] == 'L'){
            memset(tweets, 0, sizeof(tweets));
            printf("0\n");
            tweet_open=0;
        }else{
            fastRead(&click_tweet);
            if(tweets[click_tweet-1] == 1){
                tweets[click_tweet-1] = 0;
                tweet_open--;
                }else{
                    tweets[click_tweet-1] = 1;
                    tweet_open++;
                }
                printf("%d\n",tweet_open); 
        }
    }

    return EXIT_SUCCESS;
}



La solución tarda 0.03 segundos, quedando la 144 de 335, podría quedar mejor de entregarlo antes ya que los programas se ordenan por el tiempo que tardan y si hay empates el ultimo en subirse queda al final.

No hay comentarios:

Publicar un comentario