페이지가 로드되지 않나요? 여기를 눌러보면 고쳐질 수도 있어요.
Placeholder

#4951
스페셜 저지
인터랙티브

4sum0 2s 150MB

문제

[문제]

주어진 코드를 분석하여 process함수의 성능을 개선하시오.

 

[제약 사항]

전역 및 static변수를 사용할 수 없다.

1M의 지역변수를 사용할 수 있다.

 

 

/// ***** main.cpp *****
#ifndef _CRT_SECURE_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#endif
 
#include <cstdio>
#include <ctime>
 
const int MAX_SIZE = 4000;
const int BUF_SIZE = 34000000;
const int LIMIT = 1 << 28;
 
static int AN, arr[4][MAX_SIZE];
static int buf[BUF_SIZE];
 
extern long long process(int n, int arr[][MAX_SIZE], int buf[]);
 
int main() {
    //freopen("input.txt", "r", stdin);
    int i, j, val;
 
    scanf("%d", &AN);
 
    for (i = 0; i < AN; ++i) {
        for (j = 0; j < 4; ++j) {
            scanf("%d", &val);
            if (val > LIMIT) val = LIMIT;
            if (val < -LIMIT) val = -LIMIT;
            arr[j][i] = val;
        }
    }
 
    long long userAns, score = 0;
 
    clock_t  st = clock();
    userAns = process(AN, arr, buf);
    score = (clock() - st) / (CLOCKS_PER_SEC / 1000);
 
    long long ans;
    scanf("%lld", &ans);
 
    if (ans != userAns) score += (int)1e9;
 
    //printf("SCORE : %lld\n", score);
 
    if (score <= 2000) puts("PASS");
    else puts("FAIL");
 
    return 0;
}
 
/// ***** user.cpp *****
long long process(int n, int arr[][4000], int buf[]) {
    long long result = 0;
 
    ///////////////////////////////////////////////////////
 
    for (int a = 0; a < n; ++a) {
        for (int b = 0; b < n; ++b) {
            for (int c = 0; c < n; ++c) {
                for (int d = 0; d < n; ++d) {
                    if (arr[0][a] + arr[1][b] + arr[2][c] + arr[3][d] == 0) {
                        result++;
                    }
                }
            }
        }
    }
 
    ///////////////////////////////////////////////////////
 
    return result;
}
 

입력

 

 


예제

4

1 -2 3 4
-2 -3 -1 2
-1 0 2 -1
4 -3 1 -5
19
PASS

출처

comkiwer
로그인해야 코드를 작성할 수 있어요.