1. 程式人生 > >[USACO07MAR]黃金陣容均衡Gold Balanced L… map

[USACO07MAR]黃金陣容均衡Gold Balanced L… map

最大的 有著 rdl 次數 lse com ive 天都 sizeof

題目描述

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.

神牛小R在許多方面都有著很強的能力,具體的說,他總共有m種能力,並將這些能力編號為1到m。他的能力是一天一天地提升的,每天都會有一些能力得到一次提升,R對每天的能力提升都用一個數字表示,稱之為能力提升數字,比如數字13,轉化為二進制為1101,並且從右往左看,表示他的編號為1,3,4的能力分別得到了一次提升。小R把每天表示能力提升的數字的記了下來,如果在連續的一段時間內,小R的每項能力都提升了相同的次數,小R就會稱這段時間為一個均衡時期,比如在連續5天內,小R的每種能力都提升了4次,那麽這就是一個長度為5的均衡時期。

於是,問題來了,給出 小R n天的能力提升數字,請求出均衡時期的最大長度。

【數據規模】對於50%的數據,N <= 1000。

輸入輸出格式

輸入格式:

第一行有兩個整數n,m,表示有n天,m種能力。接下來有n行,每行有一個整數,分別表示第1到n天的能力提升數字。能力提升數字轉化為二進制後,從右到左的每一位表示對應的能力是否在當天得到了一次提升。

n<=100000, m<=30

輸出格式:

輸出只有一個整數,表示長度最大的均衡時期的長度。

輸入輸出樣例

輸入樣例#1: 復制
7 3
7
6
7
2
1
4
2
輸出樣例#1: 復制
4

說明

【樣例解釋】

每天被提升的能力種類分別為:

第一天:1,2,3

第二天:2,3

第三天:1,2,3

第四天:2

第五天:1

第六天:3

第七天:2

第三天到第六天為長度最長的均衡時期

因為 這四天 每種能力分別提升了 2次

可以想到考慮一個前綴和;

對於均衡區間:

sum[r]_1-sum[l-1]_1=sum[r]_2-sum[l-1]_2=......=sum[r]_m-sum[l-1]_m;

我們用 map存入,然後如果在 map中出現了當前的結果 ,更新最大值;

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 200005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == ‘-‘) f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }


/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int n, m;
map<vector<int>, int>mp;

int main() {
	//ios::sync_with_stdio(0);
	rdint(n); rdint(m);
	int ans = 0;
	vector<int>vc(m);
	mp[vc] = 0;
	for (int i = 1; i <= n; i++) {
		int x; rdint(x);
		for (int j = 0; j < m; j++) {
			if (x&(1 << j))vc[j]++;
		}
		if (x & 1)for (int j = 0; j < m; j++)vc[j]--;
		if (mp.count(vc)) {
			ans = max(ans, i - mp[vc]);
		}
		else mp[vc] = i;
	}
	cout << ans << endl;
	return 0;
}

[USACO07MAR]黃金陣容均衡Gold Balanced L… map