1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 510  Solved: 196
[Submit][Status][Discuss]

Description

Farmer John‘s N cows (1 <= N <= 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared by his cows to a list of only K different features (1 <= K <= 30). For example, cows exhibiting feature #1 might have spots, cows exhibiting feature #2 might prefer C to Pascal, and so on. FJ has even devised a concise way to describe each cow in terms of its "feature ID", a single K-bit integer whose binary representation tells us the set of features exhibited by the cow. As an example, suppose a cow has feature ID = 13. Since 13 written in binary is 1101, this means our cow exhibits features 1, 3, and 4 (reading right to left), but not feature 2. More generally, we find a 1 in the 2^(i-1) place if a cow exhibits feature i. Always the sensitive fellow, FJ lined up cows 1..N in a long row and noticed that certain ranges of cows are somewhat "balanced" in terms of the features the exhibit. A contiguous range of cows i..j is balanced if each of the K possible features is exhibited by the same number of cows in the range. FJ is curious as to the size of the largest balanced range of cows. See if you can determine it.

N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,
每头牛有多种特色,用二进制01表示它的特色ID。比如特色ID为13(1101),
则它有第1、3、4种特色。[i,j]段被称为balanced当且仅当K种特色在[i,j]内
拥有次数相同。求最大的[i,j]段长度。

Input

* Line 1: Two space-separated integers, N and K.

* Lines 2..N+1: Line i+1 contains a single K-bit integer specifying the features present in cow i. The least-significant bit of this integer is 1 if the cow exhibits feature #1, and the most-significant bit is 1 if the cow exhibits feature #K.

Output

* Line 1: A single integer giving the size of the largest contiguous balanced group of cows.

Sample Input

7 3
7
6
7
2
1
4
2

INPUT DETAILS:

The line has 7 cows with 3 features; the table below summarizes the
correspondence:
Feature 3: 1 1 1 0 0 1 0
Feature 2: 1 1 1 1 0 0 1
Feature 1: 1 0 1 0 1 0 0
Key: 7 6 7 2 1 4 2
Cow #: 1 2 3 4 5 6 7

Sample Output

4

OUTPUT DETAILS:

In the range from cow #3 to cow #6 (of size 4), each feature appears
in exactly 2 cows in this range:
Feature 3: 1 0 0 1 -> two total
Feature 2: 1 1 0 0 -> two total
Feature 1: 1 0 1 0 -> two total
Key: 7 2 1 4
Cow #: 3 4 5 6

HINT

鸣谢fjxmyzwd

Source

Gold

题解:一开始狠狠的逗比了一下——一开始我看到了这题,想当然认为问题可以转化为求最长的和为\( {2}^{M} - 1 \)的倍数的子段,结果狠狠的WA了TT。。。这种想法有个最典型的反例,那就是连续\( {2}^{M} - 1 \)个1,但是很明显不符合题意

于是发现如果某段内各个位相等的话,那么对于各个位的前缀和之差必然完全相等,其实我们也不必直接去求前缀和之差,直接可以用平衡树进行形态存储——形态存储指的是将各个位上的累加数字关于第一个元素进行个相对化——比如(2,4,6)可以转化为(0,2,4),而(4,6,8)也可以转为(0,2,4)这样如果两个前缀和数组可以构成形态相等的话,那就意味着中间这一段符合题目中所述的各个位累加和相等,于是用一颗平衡树存储即可,时间复杂度\( O\left(N M \log N \right) \)

  1 /**************************************************************
  2     Problem: 1702
  3     User: HansBug
  4     Language: Pascal
  5     Result: Accepted
  6     Time:1016 ms
  7     Memory:13116 kb
  8 ****************************************************************/
  9
 10 type
 11     list=array[1..30] of longint;
 12 var
 13    i,j,k,l,m,n,head:longint;
 14    a:array[0..100005] of list;
 15    fix,lef,rig:array[0..100005] of longint;
 16 function putin(x:longint;var a:list):longint;
 17          var i:longint;
 18          begin
 19               fillchar(a,sizeof(a),0);
 20               i:=0;
 21               while x>0 do
 22                     begin
 23                          inc(i);
 24                          a[i]:=x mod 2;
 25                          x:=x div 2;
 26                     end;
 27          end;
 28 function min(x,y:longint):longint;
 29          begin
 30               if x<y then min:=x else min:=y;
 31          end;
 32 function max(x,y:longint):longint;
 33          begin
 34               if x>y then max:=x else max:=y;
 35          end;
 36 function fc(a,b:list):longint;
 37          var i,j,k:longint;
 38          begin
 39               fc:=0;
 40               for i:=1 to m do
 41                   begin
 42                        j:=(a[i]-a[1])-(b[i]-b[1]);
 43                        if j>0 then exit(1);
 44                        if j<0 then exit(-1);
 45                   end;
 46          end;
 47 procedure lt(var x:longint);
 48           var f,r:longint;
 49           begin
 50                if (x=0) or (rig[x]=0) then exit;
 51                f:=x;r:=rig[x];
 52                rig[f]:=lef[r];
 53                lef[r]:=f;
 54                x:=r;
 55           end;
 56 procedure rt(var x:longint);
 57           var f,l:longint;
 58           begin
 59                if (x=0) or (lef[x]=0) then exit;
 60                f:=x;l:=lef[x];
 61                lef[f]:=rig[l];
 62                rig[l]:=f;
 63                x:=l;
 64           end;
 65 function ins(var x:longint;y:longint):longint;
 66          begin
 67               if x=0 then
 68                  begin
 69                       x:=y;
 70                       exit(y);
 71                  end;
 72               j:=fc(a[x],a[y]);
 73               case j of
 74                    0:exit(x);
 75                    1:begin
 76                           if lef[x]=0 then
 77                              begin
 78                                   lef[x]:=y;
 79                                   ins:=y;
 80                              end
 81                           else ins:=ins(lef[x],y);
 82                           if fix[lef[x]]<fix[x] then rt(x);
 83                    end;
 84                    -1:begin
 85                            if rig[x]=0 then
 86                               begin
 87                                    rig[x]:=y;
 88                                    ins:=y;
 89                               end
 90                            else ins:=ins(rig[x],y);
 91                            if fix[rig[x]]<fix[x] then lt(x);
 92                    end;
 93               end;
 94          end;
 95 begin
 96      readln(n,m);randomize;
 97      fillchar(lef,sizeof(lef),0);
 98      fillchar(rig,sizeof(rig),0);
 99      for i:=1 to n+1 do
100          begin
101               if i=1 then putin(0,a[i]) else
102                  begin
103                       readln(j);
104                       putin(j,a[i]);
105                  end;
106               for j:=1 to m do a[i][j]:=a[i-1][j]+a[i][j];
107               fix[i]:=random(maxlongint);
108          end;
109      head:=0;l:=0;
110      for i:=1 to n+1 do
111          begin
112               j:=ins(head,i);
113               l:=max(l,i-j);
114          end;
115      writeln(l);
116      readln;
117 end.    
时间: 2024-11-04 13:35:50

1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列的相关文章

【BZOJ】1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

[题意]给定n头牛,k个特色,给出每头牛拥有哪些特色的二进制对应数字,[i,j]平衡当且仅当第i~j头牛的所有特色数量都相等,求最长区间长度. [算法]平衡树+数学转化 [题解]统计前缀和sum[i][j]表示前i头牛特色为j的数量,则区间i~j平衡需要满足: sum[j][1]-sum[i-1][1]=sum[j][2]-sum[i-1][2]=sum[j][3]-sum[i-1][3]=... 移项可得,只须 sum[j][1]-sum[j][2]=sum[i-1][1]-sum[i-1][

bzoj 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列【hash】

我%&&--&()&%????? 双模hashWA,unsigned long longAC,而且必须判断hash出来的数不能为0???? 我可能学了假的hash 这个题求个前缀和,然后目标是找到距离当前位置最远,且能使这两个数组差分后2-k位相同 hash把差分后数组的2到k位压起来即可,用map存这个hash值最早出现的位置 但是我还是不明白为啥hash值不能为0啊?? #include<iostream> #include<cstdio> #i

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列*

bzoj1702[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 题意: N头牛,一共K种特色.每头牛有多种特色.[i,j]段被称为balanced当且仅当K种特色在[i,j]内拥有次数相同.求最大的[i,j]段长度.n≤100000,k≤30. 题解: 得到式子:a[i][l]-a[j][l]=a[i][l-1]-a[j][l-1],l在2..k之间,移项得a[i][l]-a[i][l-1]=a[j][l]-a[j][l-1],l在2..k之间,故可以定义一个

[Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则它有第1.3.4种特色.[i,j]段被称为balanced当且仅当K种特色在[i,j]内拥有次数相同.求最大的[i,j]段长度. Input 第一行给出数字N,K 下面N行每行给出一个数字,代表这头牛的特征值 Output 求出一个区间值,在这个区间中,所有牛的这K种特征值的总和是相等的. Sample In

poj 3274 -- Gold Balanced Lineup

Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12110   Accepted: 3553 Description Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow down the list of features shared

poj3274(Gold Balanced Lineup)

题目地址:Gold Balanced Lineup 题目大意: 一个农场有N个奶牛,每个奶牛都有不同的特征,聪明的农夫给奶牛 feature ID.代表奶牛所具有的特征.将feature ID 写成为K位的二进制的数,其中有1的位置代表奶牛具有此特征,0代表没有此特征.从i->j 使这个区间的奶牛所有特征的个数是相等的.其中最大的区间差就是题图所求的. 解题思路: 解题思路: 经典题,不转化问题很难做,先根据官方的方法转化问题,把“求最远的两行间各个特征出现次数相等”转化为“求最远的相同两行”,

poj 3274 Gold Balanced Lineup, 拉链式hash

sum[i][j] 表示从第1到第i头cow属性j的出现次数 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j][0] ...... sum[i][k-1]-sum[i][0

Gold Balanced Lineup - poj 3274 (hash)

这题,看到别人的解题报告做出来的,分析: 大概意思就是: 数组sum[i][j]表示从第1到第i头cow属性j的出现次数. 所以题目要求等价为: 求满足 sum[i][0]-sum[j][0]=sum[i][1]-sum[j][1]=.....=sum[i][k-1]-sum[j][k-1] (j<i) 中最大的i-j 将上式变换可得到 sum[i][1]-sum[i][0] = sum[j][1]-sum[j][0] sum[i][2]-sum[i][0] = sum[j][2]-sum[j]

POJ 3474 Gold Balanced Lineup Hash

题意一开始不是很明确, 后来发现是每一种特征出现的次数相同 这样一来就变成简单hash问题了,如果把每个特征看看做是一个(n+1)进制数的话,对奶牛序列求一下前缀和,如果i - j这一段每一种特征出现的次数相同的话,把i - 1点和j点的每一位减去所有位中的最小值之后,必然相等,所以hash判断一下就好. #include <cstdio> #include <cstring> #include <iostream> #include <map> #incl