zoukankan      html  css  js  c++  java
  • 2-2-左右最值最大差

    题目描述:
      给定一个长度为N(N>1)的整型数组A,可以将A划分成左右两个部分,左部分A[0..K],
      右部分A[K+1..N-1],K可以取值的范围是[0,N-2]。
      求这么多划分方案中,左部分中的最大值减去右部分最大值的绝对值,最大是多少?
      给定整数数组A和数组的大小n,请返回题目所求的答案。
    测试样例:
      [2,7,3,1,1],5
      返回:6

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 
     5 int findMaxGap(vector<int> A, int n){
     6     int max = A[0];
     7     for (int i = 0; i < n; i++)
     8         if (A[i] > max)
     9             max = A[i];
    10     int minux = A[0] > A[n-1] ? A[n-1] : A[0];
    11     return max - minux;
    12 }
    13 
    14 int main(){
    15     vector<int> a;
    16     a.push_back(2);
    17     a.push_back(7);
    18     a.push_back(3);
    19     a.push_back(4);
    20     a.push_back(1);
    21     a.push_back(1);
    22     //a.push_back(1);
    23     //a.push_back(2);
    24     //a.push_back(3);
    25     //a.push_back(3);
    26     //a.push_back(8);
    27     //a.push_back(9);
    28     cout << findMaxGap(a, 6) << endl;
    29     return 0;
    30 }
  • 相关阅读:
    图论
    数学
    P2222 外婆婆~
    P2083 找人
    P1215 [USACO1.4]母亲的牛奶 Mother's Milk
    New Rap
    P2298 Mzc和男家丁的游戏
    P2040 打开所有的灯
    P1135 奇怪的电梯
    UVA10474 Where is the Marble?
  • 原文地址:https://www.cnblogs.com/qianmacao/p/4884763.html
Copyright © 2011-2022 走看看