2019贝壳找房秋招编程题

第一题,找出相邻的绝对值最小组

/*
输入

第一行 数字个数
    9

    -1
    1
    3
    7
    2
    4
    6
    111111111111110
    111111111111111

输出
3 4
*/

#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
using namespace std;
class Solution {
public:
 void soulution(int n,vector<long>so)
 {
  long min = abs(so[0]-so[1]);
  long a, b;
  a = so[0]; b = so[1];
  for (int i = 1; i < n; i++)
  {
   if (abs(so[i] - so[i - 1]) < min)
   {
    a = so[i - 1];
    b = so[i];
    min = abs(so[i] - so[i - 1]);
   }
   else
    continue;
  }
  cout << a <<' '<< b << endl;
 }
};
int main()
{
 int n;
 cin >> n;
 vector<long>so;
 so.resize(n);
 for (int i = 0; i < n; i++)
  cin >> so[i];
 Solution s;
 s.soulution(n, so);

}

第二题 服务器负载增加

/*输入
5 (个数)
1 2 2 2 5
输出 (最小加负载)
6

规则,服务器负载只能递增,递减或者先递增再递减
*/


#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
 void soulution(int n,vector<int>so)
 {
  int num = 0;
  int st = 0;int en = n - 1;
        if(n==1)
        {
            cout<<0<<endl;
            return;
        }
  auto maxPosition = max_element(so.begin(), so.end());
  cout << *maxPosition << " at the postion of " << maxPosition - so.begin() <<endl;
  gradientK=*maxPosition/maxPosition;



  if (gradientK < 1)
  {
  while (st < en)
  {
   while (st + 1<n&&  so[st + 1] > so[st])
    st++;
   while (en - 1 >= 0&&so[en] < so[en - 1]  )
    en--;
   if (st == en) break;
   st++;
   en--;
   num =num+ so[st - 1] + 1 - so[st];
   so[st] = so[st - 1] + 1;
   num = num + so[en + 1] + 1 - so[en];
   so[en] = so[en + 1] + 1;
  }
}
  else
  {
    for(int i =0;i<n;i++)
    {
      if(so[i]<so[i+1])
        ;
      else
        num=num+ so[st - 1] + 1 - so[st]
    }
  }
  cout << num << endl;

 }
};
int main()
{
 int n;
 cin >> n;
 vector<int>so;
 so.resize(n);
 for (int i = 0; i < n; i++)
  cin >> so[i];
 Solution s;
 s.soulution(n, so);

}