zoukankan      html  css  js  c++  java
  • USACO 2014 Open Silver Fairphoto

    这道题只是银牌组的第一题而我就写了 3K 的代码。唉。

    Description - 问题描述

    FJ's N cows (2 <= N <= 100,000) are standing at various positions
    along a long one-dimensional fence. The ith cow is standing at
    position x_i (an integer in the range 0...1,000,000,000) and is either
    a plain white cow or a spotted cow. No two cows occupy the same
    position, and there is at least one white cow.

    FJ wants to take a photo of a contiguous interval of cows for the
    county fair, but in fairness to his different cows, he wants to ensure
    there are equal numbers of white and spotted cows in the photo. FJ
    wants to determine the maximum size of such a fair photo, where the
    size of a photo is the difference between the maximum and minimum
    positions of the cows in the photo.

    To give himself an even better chance of taking a larger photo, FJ has
    with him a bucket of paint that he can use to paint spots on an
    arbitrary subset of his white cows of his choosing, effectively
    turning them into spotted cows. Please determine the largest size of
    a fair photo FJ can take, given that FJ has the option of painting
    some of his white cows (of course, he does not need to paint any of
    the white cows if he decides this is better).

    在X的非负轴上有N个不在同一位置上的数,0或1.至少有1个0.
    可以先任意把0变成1.
    区间长度定义为,[L,R]中最右和最左的数的差的绝对值.
    求一个最长区间,满足区间中所有数0和1的个数相同.
    输出这个最长区间的长度.

    Solution - 解题报告

     

    首先,我的第一反应是用线段树维护几个字段来搞,但是想想感觉不太可行。

    然后从前缀和的角度分析一下题目,用 a(i) 表示 1...i 中 0 的个数,用 b(i) 表示 1...i 中 1 的个数,则符合条件的区间 [L, R] 可以表示为

    a(R)-a(L-1)>=b(R)-b(L-1) 且 (a(R)-a(L-1))-(b(R)-b(L-1)) mod 2=0

    解释一下:因为我们可以把任意的 0 变成 1,那么当且仅当区间中 0 的个数不少于 1,而且 0 与 1 的个数相差为偶数时区间可以变为一个合法区间。

    把上面的第一个式子变形一下就是:

    a(R)-b(R)>=a(L-1)-b(L-1)

    那么我们就维护每个位置的 val = a(i)-b(i),那么只要比较两个位置的 val 值大小就能确定这两个位置之间的区间是否满足第一个条件。

    具体做法依然是用线段树:

    将数据按位置排序后,从右往左扫描,把信息插入到线段树中。

    线段树的端点值代表的是 val = a(i)-b(i) 的值,而线段树中的每个结点 [a, b] 维护的信息 max_pos 代表「val 值大于等于 a 小于等于 b 的所有位置中的最靠右的位置」。

    这样描述太抽象,举个栗子:

    假如线段树中某个叶节点为 [a, a],它维护的 max_pos 值是 5,那就意味着,在所有的 val 值等于 a 的位置中,5 是最靠右的那个位置。这里有一点点贪心的想法:如果有多个位置都能与当前位置构成一个合法区间,我们当然应该选择最靠右的那个位置来构成一个最长的区间。

    那么从右往左扫描的时候,如果当前位置 i 的 val = x,那么我们就在线段树里查询 [x, n] 中的 max_pos,设其为 j,那么区间 [i+1, j] 就是一个合法的区间,而且是以 i 开头的最长的区间。然后我们再把该位置的 val 值连同位置信息插入到线段树中。(注意我们是从右往左扫,那么我们从线段树中得到的 j 必然是大于 i 的)(这个想法得益于之前向 LZW 大神请教的一道题)

    但是别忘了我们还有第二个条件:(a(R)-a(L-1))-(b(R)-b(L-1)) mod 2=0

    其实要满足这个条件是很简单的:分奇偶性讨论。

    也就是说我们把 val 值为奇数的单独建一棵线段树,val 值为偶数的另外单独建一棵线段树,根据当前位置 i 的 val 值的奇偶性来决定在哪棵树里查询。

    还有一个注意点就是别忘了在线段树中插入位置 0 处的信息。因为上面我们对于位置 i 得到的区间是 [i+1, j],那么以第一个位置开头的区间并没有被我们得到。所以还要插入位置 0。

    至此问题解决。复杂度 O(NlogN)。

    而官方题解用的是一种我看不懂的算法(orz)。下面介绍另外一种 O(N) 神算法,来自http://www.cnblogs.com/zyfzyf/p/4006874.html

    我们先不考虑事先把 0 修改成 1。

    首先,我们把 0 看做 -1,然后做前缀和 s(i)。

    那么一个满足条件的区间 [L, R] 必然有 s(R)=s(L-1)。

    然后我们用一个数组 first[x] 记录前缀和为 x 的最左边的位置。然后从左到右扫一遍。

    如果考虑修改,那么满足条件的区间有 s(R)-s(L-1)<=0 且 s(R)-s(L-1) mod 2=0,表现在对于某个位置 i,first[s(i)],first[s(i)+2],first[s(i)+4],... 都能与 i 构成合法的区间。那么在扫描的时候就改变一下 first[x] 的含义:

    1 for(int i=2*n;i>=0;i--)
    2     first[i]=min(first[i+2], first[i]);

    (注意,因为前缀和可能小于 0 但不可能小于 -n,所以在保存到 first 数组的时候加上一个偏移量 n,那么 first 数组的下标范围就是 0...2n)

    代码量 1K。蒟蒻表示五体投地 orz。

  • 相关阅读:
    从零开始学 ASP.NET Core 与 EntityFramework Core 介绍
    Spring Cloud Stream
    基于vue实现的三级联动下拉框
    中国十大瓜子品牌排行榜
    春秋战国2020
    spring boot 2 + shiro 实现权限管理
    轻松搭建CAS 5.x系列(1)-使用cas overlay搭建SSO SERVER服务端
    springboot+maven+thymeleaf配置实战demo
    Springboot系列:Springboot与Thymeleaf模板引擎整合基础教程
    idea使用maven install命令打包(springboot),jar运行时出现没有主清单属性
  • 原文地址:https://www.cnblogs.com/lsdsjy/p/fair-photo.html
Copyright © 2011-2022 走看看