码迷,mamicode.com
首页 > 其他好文 > 详细

[USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)

时间:2016-10-27 22:50:30      阅读:392      评论:0      收藏:0      [点我收藏+]

标签:七天   sizeof   ice   eve   mod   区间   数据   algorithm   for   

题目描述

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次

/*
  刚开始以为是求区间和%(2^m-1)=0的最大区间,后来发现是不对的,应该将状态全部储存进去。 
  当两个数相同时,说明在这两个数之间出现的数在每个特征上出现的数目相同,否则是不会两个数相
  同的。因为只有最右边的那个数增加了和左边所有的数增加的数字相同,他们才会减去最右边的数,
  出现相同。 
*/
#include<string>  
#include<cstring>  
#include<cstdio>
#include<algorithm>  
#define mod 100007
using namespace std;  
int hash[mod+10][34];    
int a[mod][31];  
int s[mod][31];  
int k;  
bool check(int t,int xt)  
{  
     int i;  
     bool flag=true;  
     for(i=0;i<=k-1;i++)  
       if(s[xt][i]!=hash[t][i])  
        return false;
     return true;  
}  
int find(int x,int xt,int xp)  
{  
     int t=x;  
     while(hash[t][32]!=-1)  
     {  
          if(!check(t,xt)) t=(t+1)%mod;  
          else break;
     }  
     if(hash[t][32]==-1)  
     {  
          int i;  
          for(i=0;i<=k-1;i++)  
               hash[t][i]=s[xt][i];  
          hash[t][33]=xp;  
          hash[t][32]=1;  
          return xp;  
     }  
     return hash[t][33];  
}  
int main()  
{  
     int n;  
     scanf("%d%d",&n,&k);  
     int i,j;  
     int x;  
     for(i=1;i<=n;i++)  
     {  
          scanf("%d",&x);  
          int p=0;  
          while(x!=0)  
          {  
               a[i][p]=x%2;  
               x=x/2;  
               p++;  
          }  
     }  
     for(i=1;i<=n;i++)  
        for(j=0;j<=k-1;j++)  
            s[i][j]=s[i-1][j]+a[i][j];  
     for(i=1;i<=n;i++)  
        for(j=k-1;j>=0;j--)  
            s[i][j]-=s[i][0];  
     memset(hash,-1,sizeof(hash));//hash的部分不是很懂 
     int ans=0;  
     for(i=0;i<=n;i++)  
     {  
          int p=0;  
          for(j=k-1;j>=0;j--)  
          {  
               p=(p*4+s[i][j])%mod;
               while(p<0)p=-p;  
          }   
          int loc=find(p,i,i);  
          ans=max(ans,i-loc);  
     }   
     printf("%d\n",ans);  
     return 0;  
} 

 

[USACO07MAR]黄金阵容均衡Gold Balanced L…(洛谷 1360)

标签:七天   sizeof   ice   eve   mod   区间   数据   algorithm   for   

原文地址:http://www.cnblogs.com/harden/p/6005760.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!