# [BZOJ1637][Usaco2007 Mar]Balanced Lineup

## Description

Farmer John 决定给他的奶牛们照一张合影，他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线，每头牛都有它的

0和种族1的牛的数量相等。 请算出最广阔的区间，使这个区间内的牛阵容平衡。区间的大小为区间内最右边的牛

7
0 11
1 10
1 25
1 12
1 4
0 13
1 22

11

## Source

Silver

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long ll ;
#define gc getchar
ll res=0;char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch)){res=(res<<3)+(res<<1)+(ch^48);ch=gc();}
return res;
}
int n;
struct date{
int typ, x;
}d[50005];
int vis[100005];
int sum[50005];
ll ans;

inline bool cmp(date a, date b){return a.x < b.x;}

int main()
{
for (int i = 1 ; i <= n ; i ++)
sort(d+1,d+1+n, cmp);
memset(vis, -1, sizeof vis);
sum[0] = 50000;
for (int i = 1 ; i <= n ; i ++)
{
sum[i] = sum[i-1] + d[i].typ;
if (vis[sum[i]] == -1) vis[sum[i]] = i + 1;
else ans = max(ans, (ll)(d[i].x - d[vis[sum[i]]].x));
}
cout<<ans<<endl;
return 0;
}

posted @ 2018-07-23 22:53  zZhBr  阅读(111)  评论(0编辑  收藏  举报