• 【BZOJ】1637: [Usaco2007 Mar]Balanced Lineup(前缀和+差分+特殊的技巧)


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

    很神思想。。

    前缀和应用到了极点。。。

    我们可以发现当数量一定时,这个区间最前边的牛的前边一个牛的前缀和等于这个区间最后边的牛的前缀和。。(将0的牛变成-1,然后维护前缀和)

    然后扫过去就行了。。。

    orz

    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    using namespace std;
    #define rep(i, n) for(int i=0; i<(n); ++i)
    #define for1(i,a,n) for(int i=(a);i<=(n);++i)
    #define for2(i,a,n) for(int i=(a);i<(n);++i)
    #define for3(i,a,n) for(int i=(a);i>=(n);--i)
    #define for4(i,a,n) for(int i=(a);i>(n);--i)
    #define CC(i,a) memset(i,a,sizeof(i))
    #define read(a) a=getint()
    #define print(a) printf("%d", a)
    #define dbg(x) cout << #x << " = " << x << endl
    #define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
    inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
    inline const int max(const int &a, const int &b) { return a>b?a:b; }
    inline const int min(const int &a, const int &b) { return a<b?a:b; }
    const int N=50005;
    
    struct data { int w, x; }a[N];
    int vis[N+N+1], sum=N, n, ans;
    bool cmp(const data &a, const data &b) { return a.x<b.x; }
    
    int main() {
    	read(n);
    	for1(i, 1, n) { read(a[i].w); read(a[i].x); if(!a[i].w) a[i].w=-1; }
    	sort(a+1, a+1+n, cmp);
    	for1(i, 1, n) {
    		sum+=a[i].w;
    		if(!vis[sum]) vis[sum]=a[i+1].x;
    		else ans=max(ans, a[i].x-vis[sum]);
    	}
    	print(ans);
    	return 0;
    }
    

    Description

    Farmer John 决定给他的奶牛们照一张合影,他让 N (1 ≤ N ≤ 50,000) 头奶牛站成一条直线,每头牛都有它的坐标(范围: 0..1,000,000,000)和种族(0或1)。 一直以来 Farmer John 总是喜欢做一些非凡的事,当然这次照相也不例外。他只给一部分牛照相,并且这一组牛的阵容必须是“平衡的”。平衡的阵容,指的是在一组牛中,种族0和种族 1的牛的数量相等。 请算出最广阔的区间,使这个区间内的牛阵容平衡。区间的大小为区间内最右边的牛的坐标减去最做边的牛的坐标。 输入中,每个种族至少有一头牛,没有两头牛的坐标相同。

    Input

    行 1: 一个整数: N 行 2..N + 1: 每行两个整数,为种族 ID 和 x 坐标。

    Output

    行 1: 一个整数,阵容平衡的最大的区间的大小。

    Sample Input

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

    Sample Output

    11
    输入说明

    有7头牛,像这样在数轴上。


    1 1 0 1 0 1 1
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
    输出说明

    牛 #1 (at 11), #4 (at 12), #6 (at 13), #7 (at 22) 组成一个平衡的最大的区间,大小为 22-11=11 个单位长度。

    <-------- 平衡的 -------->
    1 1 0 1 0 1 1
    +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

    HINT

    Source

  • 相关阅读:
    js返回上个页面再刷新
    xm-select 获取选选择的值
    接口可以跨域访问
    gitlab 版本查看
    gitlab 在linux安装环境下存储地址
    iosUILable基本使用及其所有方法
    iOS富文本字符串AttributedString具体用法
    SQL Server 开发指南
    SQL Server 数据库安全
    SQL Server 触发器
  • 原文地址:https://www.cnblogs.com/iwtwiioi/p/3957949.html
Copyright © 2020-2023  润新知