zoukankan      html  css  js  c++  java
  • [编程题-蘑菇街] 最大间隔

    [编程题] 最大间隔
    给定一个递增序列,a1 <a2 <...<an 。定义这个序列的最大间隔为d=max{ai+1 - ai }(1≤i<n),现在要从a2 ,a3 ..an-1 中删除一个元素。问剩余序列的最大间隔最小是多少?

    输入描述:
    第一行,一个正整数n(1<=n<=100),序列长度;接下来n个小于1000的正整数,表示一个递增序列。


    输出描述:
    输出答案。

    输入例子:
    5
    1 2 3 7 8

    输出例子:
    4
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n;
        while(cin>>n)
        {
            vector<int> a(n);
            for(int i=0;i<n;i++)
                cin>>a[i];
            vector<int> b(n-1);
            for(int i=0;i<n-1;i++)
                b[i]=a[i+1]-a[i];
            vector<int> c=b;
            sort(c.begin(),c.end());
            if(c[n-2]==b[0] || c[n-2]==b[n-2])
                cout<<c[n-3]<<endl;
            else 
                cout<<c[n-2]<<endl;
        }
        return 0;
    }
  • 相关阅读:
    正则
    cookie、sesion
    POJ-1509
    HDU-3374
    ZOJ-3822
    HDU-5492
    在什么情况下Java比C++快?
    HDU-5451
    SPOJ-913
    莫比乌斯反演入门
  • 原文地址:https://www.cnblogs.com/learning-c/p/5743028.html
Copyright © 2011-2022 走看看