답안 #26614

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
26614 2017-07-03T12:51:45 Z model_code Shift (POI11_prz) C
100 / 100
269 ms 16752 KB
/*************************************************************************
 *                                                                       *
 *                    XVIII Olimpiada Informatyczna                      *
 *                                                                       *
 *   Zadanie:           Przekladanka                                     *
 *   Autor:             Jacek Migdal                                     *
 *   Zlozonosc czasowa: O(n^2)                                           *
 *   Opis:              Rozwiazanie wzorcowe                             *
 *                                                                       *
 *************************************************************************/

#include <stdlib.h>
#include <stdio.h>

#define MAXN 2000
int answer[MAXN * MAXN];
int answerN;
int data[MAXN];
int dataN;
int begin;

void addAnswer(int num)
{
    if (answerN > 0 && answer[answerN - 1] * num > 0) {
        answer[answerN - 1] += num;
    } else {
        answer[answerN++] = num;
    }
}

void operationPlus() {
    begin--;
    if (begin < 0)
        begin += dataN;
    addAnswer(1);
}

void operationPlus2() {
    begin++;
    if (begin >= dataN)
        begin -= dataN;
    addAnswer(dataN - 1);
}

void operationMinus() {
    int next[2] = {(begin + 1) % dataN, (begin + 2) % dataN};
    int tmp = data[begin];
    data[begin] = data[next[1]];
    data[next[1]] = data[next[0]];
    data[next[0]] = tmp;
    addAnswer(-1);
}

int main()
{
    int i, j, n;
    scanf("%d", &n);
    dataN = n;
    for (i = 0 ; i < n ; ++i) {
        scanf("%d", &data[i]);
    }

    if (n <= 2) {
        if (n == 1 || 1 ==data[0]) {
            printf("0\n");
        } else {
            printf("1\n1a\n");
        }
        return 0;
    }

    for (j = 2 ; j <= (n - 2) ; ++j) {
        while (data[begin] != j) {
            operationPlus();
        }

        while (data[(begin - 1 + dataN) % dataN] != j - 1) {
            operationPlus();
            if (data[(begin - 1 + dataN) % dataN] != j - 1) {
                operationPlus();
                operationMinus();
            } else {
                operationMinus();
                operationMinus();
            }
        }
    }

    while (data[begin] != 1) {
        operationPlus();
    }

    if (data[(begin - 1 + dataN) % dataN] != n) {
        if (n % 2 == 1) {
            printf("NIE DA SIE\n");
            return 0;
        } else {
            operationPlus();

            while (data[(begin + 1) % dataN] != n) {
                operationMinus();
                operationMinus();
                operationPlus2();
                operationPlus2();
            }
            for (i = 0 ; i < n - 2 ; ++i) {
                operationPlus();
            }
        }
    }

    int nonZeroAns = 0;
    for(i = 0; i < answerN; ++i) {
        if (answer[i] % n != 0) nonZeroAns++;
    }
    printf("%d\n",nonZeroAns);
    
    for (i = 0 ; i < answerN ; ++i) {
        if (answer[i] % n != 0) {
            int x = answer[i] % n;
            if (x > 0) printf("%da ", x); else printf("%db ",x * (-1));
        }
    }
    printf("\n");

    return 0;
}

Compilation message

prz.c: In function 'main':
prz.c:57:5: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &n);
     ^
prz.c:60:9: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d", &data[i]);
         ^
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16752 KB Output is correct
2 Correct 0 ms 16752 KB Output is correct
3 Correct 0 ms 16752 KB Output is correct
4 Correct 0 ms 16752 KB Output is correct
5 Correct 0 ms 16752 KB Output is correct
6 Correct 0 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16752 KB Output is correct
2 Correct 0 ms 16752 KB Output is correct
3 Correct 0 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16752 KB Output is correct
2 Correct 0 ms 16752 KB Output is correct
3 Correct 0 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 16752 KB Output is correct
2 Correct 0 ms 16752 KB Output is correct
3 Correct 0 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 9 ms 16752 KB Output is correct
2 Correct 6 ms 16752 KB Output is correct
3 Correct 9 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 16752 KB Output is correct
2 Correct 109 ms 16752 KB Output is correct
3 Correct 113 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 83 ms 16752 KB Output is correct
2 Correct 113 ms 16752 KB Output is correct
3 Correct 133 ms 16752 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 149 ms 16752 KB Output is correct
2 Correct 149 ms 16752 KB Output is correct
3 Correct 269 ms 16752 KB Output is correct
4 Correct 13 ms 16752 KB Output is correct
5 Correct 29 ms 16752 KB Output is correct