205B Little Elephant and Sorting

贪心:每次对于比前面的数小的数累加差值即可,比前面大的可以通过加大区间范围保持大小关系不变;

# include <cstdio>



int n, a[100005];



void init(void)

{

    int i;



    scanf("%d", &n);

    for (i = 0; i < n; ++i)

        scanf("%d", &a[i]);

}



void solve(void)

{

    int i;

    long long int ans;



    ans = 0;

    for (i = 1; i < n; ++i) if (a[i] < a[i-1])

        ans += a[i-1]-a[i];

    printf("%I64d\n", ans);

}



int main()

{

    init();

    solve();



    return 0;

}

你可能感兴趣的:(sort)