ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1220. Stacks

Time limit on test #11
Posted by cra3y 2 Dec 2013 23:15
i got 0.531 sec for this code
#include <stdio.h>
#include <stdlib.h>

typedef unsigned int type;

struct stack{
    type* value;
    int cnt;
    int size;
};

#define delta 4

#define getnum(num) do {\
    (num) = 0; \
    char c; \
    while(1) { \
        c = fgetc(stdin); \
        if((c<'0')||(c>'9')) break; \
        (num)*=10; \
        (num)+=c-'0'; \
    }\
}while(0)

#define putnum(num) do { \
    char str[16]; \
    char *it = &str[15];*it = 0; \
    if((num)==0) { fputc('0', stdout); } \
    else { \
        while(num) { \
            --it; \
            *it = (char)((num)%10)+'0'; \
            (num)/=10; \
        } \
        while(*it) { \
            fputc(*it, stdout); \
            ++it; \
        } \
    }\
    fputc('\n', stdout); \
} while(0);

struct stack st[1000] = {0};
int main() {
    int cnt;
    getnum(cnt);
    while(cnt--){
        int id;
        char c,i;
        i=0;
        while((c = getchar())!=' ')
            ++i;
        getnum(id);
        --id;
        if(i>3) {
            st[id].cnt++;
            if(st[id].cnt > st[id].size) {
                st[id].size+=delta;
                st[id].value = (type*)realloc(st[id].value,
                                st[id].size*sizeof(type));
            }
            getnum(st[id].value[st[id].cnt-1]);
        }
        else {
            st[id].cnt--;
            putnum(st[id].value[st[id].cnt]);
            if(st[id].cnt+delta<st[id].size) {
                st[id].size-=delta;
                st[id].value = (type*)realloc(st[id].value,
                            st[id].size*sizeof(type));
            }
        }
    }
    return 0;
}

if set delta to 6, then i got MLE on test #12 =/
i broke my brain.
Sorry for my English
Re: Time limit on test #11
Posted by Иван 20 Jun 2014 20:49
у меня точно такой же алгоритм :) и точно такая же проблема :)