黄金平衡(poj3274)
经典题(没过)
题意:经典题,不转化问题很难做,先根据官方的方法转化问题,把“求最远的两行间各个特征出现次数相等”转化为“求最远的相同两行”,再用Hash查找。
这是官方解题报告——
Consider the partial sum sequence of each of the k features built by taking the
sum of all the values up to position i. The problem is equivalent to:
Given an array s[n][k], find i,j, with the biggest separation for which s[ i ]
[l]-s[j][l] is constant for all l.
The problem is now to do this efficiently. Notice that s[ i ][l]-s[j][l] being
constant for all l is equivalent to s[ i ][l]-s[j][l]=s[ i ][1]-s[j][1] for all
l, which can be rearranged to become s[ i ][l]-s[ i ][1]=s[j][l]-s[j][1] for all
l. Therefore, we can construct another array a[n][k] where a[ i ][j]=s[ i ][j]-
s[ i ][1] and the goal is to find i and j with the biggest separation for which
a[ i ][l]=a[j][l] for all l.
This can be done by sorting all the a[ i ] entries, which takes O(nklogn) time
(although in practice rarely will all k elements be compared). Another
alternative is to go by hashing, giving an O(nk) solution. Both solutions are
fairly straightforward once the final array is constructed.
大概意思就是:
数组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]
中最大的i-j(j< i)
将上式变换可得到
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] = sum[j][k-1]-sum[j][0]
令C[i][y]=sum[i][y]-sum[i][0] (0 < y < k)
初始条件C[0][0~k-1]=0
所以只需求满足C[i][]==C[j][] 中最大的i-j,其中0<=j < i <=n。
C[i][]==C[j][] 即二维数组C[][]第i行与第j行对应列的值相等,
那么原题就转化为求C数组中 相等且相隔最远的两行的距离i-j。
以样例为例
7 3
7
6
7
2
1
4
2
先把7个十进制特征数转换为二进制,并逆序存放到特征数组feature[ ][ ],得到:
7 à 1 1 1
6 à 0 1 1
7 à 1 1 1
2 à 0 1 0
1 à 1 0 0
4 à 0 0 1
2 à 0 1 0
(行数为cow编号,自上而下从1开始;列数为特征编号,自左到右从0开始)
再求sum数组,逐行累加得,sum数组为
1 1 1
1 2 2
2 3 3
2 4 3
3 4 3
3 4 4
3 5 4
再利用C[i][y]=sum[i][y]-sum[i][0]求C数组,即所有列都减去第一列
注意C数组有第0行,为全0
0 0 0 à 第0行
0 0 0
0 1 1
0 1 1
0 2 1
0 1 0
0 1 1
0 2 1
显然第2行与第6行相等,均为011,且距离最远,距离为6-2=4,这就是所求。
但是最大数据有10W个,即10W行,因此不能直接枚举找最大距离,必须用Hash查找相同行,找到相同行再比较最大距离。
注意C数组的值可能为负数,因此生成key值时要注意保证key为非负数。
Source修正:
USACO 2007 March Gold
http://www.cppblog.com/Felicia/archive/2007/12/29/39923.html
官方对Hint的解释:
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
OUTPUT FORMAT:
- Line 1: A single integer giving the size of the largest contiguous
balanced group of cows.
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
以上参考博客:
優YoU http://user.qzone.qq.com/289065406/blog/1309220772
#
下面加一些:一定要用C[i][j],原因是因为只有sum[i][j]每次减 去sum[i][0]才可能相等,这时就可以比较是否相同
#
同时这里用到了哈希表,原因前面提到过,最大的数据有10W个,及不能用枚举,必须用哈希表查找,同时,c数组的值得可能为负数,所以必须要变成正数。
代码还没过
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
using namespace std;
const int size = 100001;
const int mod = 99991;
int feature[size][30];
int sum[size][30];
int C[size][30];
int Maxlen;
struct Node
{
int pi;
Node *next;
Node()
{
pi = 0;
}
};
int K;
Node hash1[mod];
bool cmp(int ai,int bi)
{
for(int i=0;i<K;i++)
{
if(C[ai][i] != C[bi][i])
return false;
}
return true;
}
void Hash(int ci)
{
int key=0;
for(int j=0;j<K;j++)
key += C[ci][j];
int t = abs(key);
key = t % mod;
if(!hash1[key].pi)
{
hash1[key].pi = ci;
hash1[key].next = NULL;
}
else
{
if(cmp(hash1[key].pi,ci))
{
int dist = ci - (hash1[key].pi);
if(Maxlen < dist)
Maxlen = dist;
return;
}
else
{
Node *pn = new Node;
pn = &hash1[key];
while(pn->next)
{
if(cmp(pn->next->pi,ci))
{
int dist = ci - (pn->next->pi);
if(Maxlen < dist)
Maxlen = dist;
return;
}
else
{
pn = pn->next;
}
}
pn->next->pi = ci;
pn->next->next = NULL;
}
return;
}
}
int main()
{
int N;
while(~scanf("%d%d",&N,&K))
{
for(int p=0;p<K;p++)
{
C[0][p] = 0;
sum[0][p] = 0;
}
memset(hash1,0,sizeof(hash1));
Hash(0);
Maxlen = 0;
for(int i=1;i<=N;i++)
{
int Dexf;
cin >> Dexf;
for(int j=0;j<K;j++)
{
feature[i][j] = Dexf % 2;
Dexf /= 2;
sum[i][j] = sum[i-1][j] + feature[i][j];
C[i][j] = sum[i][j] - sum[i][0];
}
Hash(i);
}
cout << Maxlen << endl;
}
return 0;
}