Algoogle

Algorithm for Programming Contest

PKU 2181 Jumping Cows

Category: PKU Tag: implementation

Jumping Cows

問題概要


解法


数値を山みたいにみて, てっぺんで足して, 凹んでるとこで引くのを繰り返す.

コード


(2181.cpp) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <algorithm>
#include <cstdio>

using namespace std;

int P, portion[150010];

int main(){
    scanf("%d", &P);
    for(int i = 0; i < P; i++)
        scanf("%d", portion + i);
    portion[P] = 0;
    P++;
    bool plus = 1;
    int ans = 0, prev = 0;
    for(int i = 0; i < P; i++){
        if(plus && prev > portion[i]) {
            ans += prev;
            plus = 0;
        }
        else if(!plus && prev < portion[i]){
            ans -= prev;
            plus = 1;
        }
        prev = portion[i];
    }
    printf("%d\n", ans);
    return 0;
}

Comments