bzoj1637: [Usaco2007 Mar]Balanced Lineup

链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1637

题意:中文题。。

分析:将0变成-1,然后按x从小到大排序,然后算前缀和,找离当前前缀和相同切最远的前缀和即可。

代码:

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<math.h>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int N=50010;
const int MAX=151;
const int MOD=1000007;
const int MOD1=100000007;
const int MOD2=100000009;
const int INF=2100000000;
const double EPS=0.00000001;
typedef long long ll;
typedef unsigned long long ull;
int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
struct node {
    int b,x;
}a[N];
int cmd(node a,node b) {
    return a.x<b.x;
}
int w[2*N],q[2*N],sum[N];
int main()
{
    int i,n,ans=0;
    scanf("%d", &n);
    for (i=1;i<=n;i++) {
        scanf("%d%d", &a[i].b, &a[i].x);
        if (a[i].b==0) a[i].b=-1;
    }
    sort(a+1,a+n+1,cmd);
    sum[0]=n;
    for (i=1;i<=n;i++) sum[i]=sum[i-1]+a[i].b;
    memset(q,0,sizeof(q));
    q[n]=1;w[n]=0;
    for (i=1;i<=n;i++)
    if (q[sum[i]]) {
        ans=max(ans,a[i].x-a[w[sum[i]]+1].x);
    } else {
        q[sum[i]]=1;w[sum[i]]=i;
    }
    printf("%d\n", ans);
    return 0;
}

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



*/


你可能感兴趣的:(bzoj1637: [Usaco2007 Mar]Balanced Lineup)