一组数据差值之和最大

#include <bits/stdc++.h>
using namespace std;
class Solution{
    std::vector<std::string> split(std::string &str, char ch){
        std::vector<std::string> ans;
        str += ch;
        int i = 0;
        for (int j = 0; j < str.size(); ++j) {
            if(str[j] == ch){
                ans.push_back(str.substr(i, j - i));
                i = j + 1;
            }
        }
        return ans;
    }
public:
    int process(std::string str){
        std::vector<std::string> tmp = split(str, ' ');
        std::vector<int> prices;
     //string容器转int容器保存
for (int i = 0; i < tmp.size(); i++) { prices.push_back(atoi(tmp[i].c_str())); }   int min_ = prices[0], max_ = prices[0], answer = 0;   for (int i = 1; i < prices.size(); ++i) {//连续降序找到最小,然后连续升序找到最大。--循环 if (prices[i] < max_) { answer += max_ - min_; max_ = min_ = prices[i]; }
     else max_ = prices[i];   }   answer += max_ - min_;   return answer; } };

 

int main()
{
  // please write your code here
  std::string str, str1;
  getline(std::cin, str1);
  getline(std::cin, str);
  Solution a;
  std::cout << a.process(str);
  return 0;
}

posted @ 2023-08-03 16:03  Lunais  阅读(11)  评论(0编辑  收藏  举报