[luoguP1360] [USACO07MAR]黄金阵容均衡Gold Balanced L…

传送门

 

真的骚的一个题,看了半天只会个前缀和+暴力。。

纯考思维。。

良心题解

 

#include <cstdio>
#include <cstring>
#include <algorithm>
#define M 41
#define N 100011
#define max(x, y) ((x) > (y) ? (x) : (y))

int n, m, ans, last;

struct node
{
	int sum[M], id;
	node()
	{
		id = 0;
		memset(sum, 0, sizeof(sum));
	}
}p[N];

inline bool cmp(node x, node y)
{
	int i;
	for(i = 1; i <= m; i++)
		if(x.sum[i] != y.sum[i])
			return x.sum[i] < y.sum[i];
	return x.id < y.id;
}

inline bool check(int x, int y)
{
	int i;
	for(i = 1; i <= m; i++)
		if(p[x].sum[i] != p[y].sum[i])
			return 0;
	return 1;
}

int main()
{
	int i, j, x;
	scanf("%d %d", &n, &m);
	for(i = 1; i <= n; i++)
	{
		scanf("%d", &x);
		p[i].id = i;
		for(j = 1; j <= m; j++)
			p[i].sum[j] = p[i - 1].sum[j] + bool(x & (1 << j - 1));
	}
	for(i = 1; i <= n; i++)
		for(j = 1; j <= m; j++)
			p[i].sum[j] -= p[i].sum[m];
	std::sort(p + 1, p + n + 2, cmp);
	last = p[1].id;
	for(i = 2; i <= n + 1; i++)
	{
		if(check(i, i - 1))
			ans = max(ans, p[i].id - last);
		else last = p[i].id;
	}
	printf("%d\n", ans);
	return 0;
}

  

posted @ 2017-08-22 20:51  zht467  阅读(201)  评论(0编辑  收藏  举报