제출 #1315744

#제출 시각아이디문제언어결과실행 시간메모리
1315744vixtor지구 온난화 (NOI13_gw)C11
40 / 40
151 ms10812 KiB
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>

int compare(const void *a, const void *b) {
    int int_a = *(const int *)a;
    int int_b = *(const int *)b;
    
    if (int_a < int_b) return 1;
    if (int_a > int_b) return -1;
    return 0;
}

int main() {
    int n, N, *h, hh, *topo, cmin=INT_MAX, cmax=0, c, r, i, j, res, resmax=0, *peaks, *valleys, peakcnt=0, valleycnt=0, p, v, islands, cnt;
    char *f;
    scanf("%d", &N);
    h = (int *)calloc(N, sizeof(int));
    topo = (int *)calloc(N, sizeof(int));
    f = (char *)calloc(134217728, sizeof(char));
    scanf("%d", &h[0]); n=1;
    for(i=1; i<N; i++) {
        scanf("%d", &hh);
        if(h[n-1]!=hh) {
            h[n] = hh;
            n++;
        }
    }

    N=0;
    if(h[0]>h[1]) {
        topo[N++] = h[0];
        peakcnt++;
    }

    for(i=1; i<n-1; i++) {
        if(topo[N-1]<h[i] && h[i]>h[i+1]) { //avem un varf;
            topo[N++] = h[i];
            peakcnt++;
        } else if(topo[N-1]>h[i] && h[i]<h[i+1]) {
            topo[N++] = h[i];
            valleycnt++;
        }
    }
    
    if(h[n-1]>h[n-2]) {
        topo[N++] = h[n-1];
        peakcnt++;
    }
    
    peaks = (int *) calloc(peakcnt+1, sizeof(int));
    valleys = (int *) calloc(valleycnt+1, sizeof(int));
    p=0; v=0;
    if(topo[0]<topo[1]) valleys[v++]=topo[0];
    else peaks[p++]=topo[0];

    for(i=1; i<N; i++) {
        if(topo[i]>topo[i-1]) peaks[p++]=topo[i];
        else valleys[v++]=topo[i];
    }

    qsort(peaks, peakcnt, sizeof(int), compare);
    qsort(valleys, valleycnt, sizeof(int), compare);


    islands=0;
    i=0; j=0;
    while(i<peakcnt || j<valleycnt) {
        if(peaks[i]>valleys[j]) {
            p = peaks[i];
            cnt=1;
            while(peaks[++i]==p) cnt++;
            islands+=cnt;
        } else {
            v = valleys[j];
            cnt=1;
            while(valleys[++j]==v) cnt++;
            islands-=cnt;
        }
        if(islands>resmax) resmax=islands;
    }
    printf("%d\n", resmax);
    
}

컴파일 시 표준 에러 (stderr) 메시지

gw.c: In function 'main':
gw.c:17:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   17 |     scanf("%d", &N);
      |     ^~~~~~~~~~~~~~~
gw.c:21:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |     scanf("%d", &h[0]); n=1;
      |     ^~~~~~~~~~~~~~~~~~
gw.c:23:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
   23 |         scanf("%d", &hh);
      |         ^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...