zoukankan      html  css  js  c++  java
  • CodeForces 205B

     - Little Elephant and Sorting
    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

    Description

    The Little Elephant loves sortings.

    He has an array a consisting of n integers. Let's number the array elements from 1 to n, then the i-th element will be denoted as ai. The Little Elephant can make one move to choose an arbitrary pair of integers l and r (1 ≤ l ≤ r ≤ n) and increase ai by 1 for all i such that l ≤ i ≤ r.

    Help the Little Elephant find the minimum number of moves he needs to convert array a to an arbitrary array sorted in the non-decreasing order. Array a, consisting of n elements, is sorted in the non-decreasing order if for any i (1 ≤ i < n) ai ≤ ai + 1 holds.

    Input

    The first line contains a single integer n (1 ≤ n ≤ 105) — the size of array a. The next line contains n integers, separated by single spaces — array a (1 ≤ ai ≤ 109). The array elements are listed in the line in the order of their index's increasing.

    Output

    In a single line print a single integer — the answer to the problem.

    Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

    Sample Input

    Input
    3
    1 2 3
    Output
    0
    Input
    3
    3 2 1
    Output
    2
    Input
    4
    7 4 1 47
    Output
    6

    Hint

    In the first sample the array is already sorted in the non-decreasing order, so the answer is 0.

    In the second sample you need to perform two operations: first increase numbers from second to third (after that the array will be: [3, 3, 2]), and second increase only the last element (the array will be: [3, 3, 3]).

    In the third sample you should make at least 6 steps. The possible sequence of the operations is: (2; 3), (2; 3), (2; 3), (3; 3), (3; 3), (3; 3). After that the array converts to [7, 7, 7, 47].


    //这题开始以为是要求递减子序列什么的、结果自己在本子上写了下、发现
    //其实只要看相邻2项就可以了
    #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> #include <queue> #include <cmath> using namespace std; int main() { int n,i,x,y; __int64 sum; while(scanf("%d",&n)!=EOF) { sum=0;scanf("%d",&x); for(i=1;i<n;i++) { scanf("%d",&y); if(y<x) sum+=x-y; x=y; } printf("%I64d\n",sum); } }
  • 相关阅读:
    在TNSNAMES.ORA文件中配置本机装的oracle
    Eclipse编辑jsp、js文件时,经常出现卡死现象解决汇总
    ExtJs GridPanel 给表格行或者单元格自定义样式
    Ext.core.DomQuery Dom选择器
    JavaScript 常用方法
    ExtJs Ext.data.Model 学习笔记
    JavaScript 深入理解作用域链
    Spring 网路搜集的情报
    SpringMVC 之类型转换Converter详解转载
    SpringMVC @RequestMapping 用法详解之地址映射
  • 原文地址:https://www.cnblogs.com/372465774y/p/2624533.html
Copyright © 2011-2022 走看看