[51nod1272]最大距离(贪心)

解题关键:对num进行排序,从前往后扫id,及时更新

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<iostream>
#include<cmath>
using namespace std;
typedef long long ll;
#define maxn 50002
struct node{
    ll num,id;
}a[maxn];
bool cmp(const node &a,const node &b){
    return a.num<b.num||(a.num==b.num&&a.id<b.id);
}
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++) cin>>a[i].num,a[i].id=i;
    sort(a, a+n, cmp);
    ll mi=a[0].id,ans=0;
    for(int i=1;i<n;i++){
        if(a[i].id>mi) ans=max(ans,a[i].id-mi);
        else mi=a[i].id;
    }
    cout<<ans<<"\n";
    return 0;
}

 

posted @ 2017-11-06 19:38  Elpsywk  阅读(200)  评论(0编辑  收藏  举报