zoukankan      html  css  js  c++  java
  • 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 3   Accepted Submission(s) : 3
    Problem 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 Kdifferent 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.

     
    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
     
    Sample Output
    4
     
    Source
    PKU
     
      1 #include <iostream>
      2 #include <stdio.h>
      3 #include <cstring>
      4 #include <cmath>
      5 #include <stdlib.h>
      6 #define maxn 100010
      7 #define seed 999983
      8 using namespace std;
      9 
     10 int n,k;
     11 int feature[maxn][31],sum[maxn][31],c[maxn][31],maxlen=0;
     12 
     13 struct Hash
     14 {
     15     int id,next;
     16 } h[1000000];
     17 
     18 bool compair(int a,int b)
     19 {
     20     int i;
     21     for(i=0; i<k; i++)
     22     {
     23         if(c[a][i]!=c[b][i])
     24             return false;
     25     }
     26     return true;
     27 }
     28 
     29 int count_key(int index)
     30 {
     31     int i,key=0;
     32     //Hash公式
     33     for(i=0; i<k; i++)
     34         key=((key<<2)+(c[index][i]>>4))^(c[index][i]<<10);
     35     key=key%seed;
     36     if(key<0)
     37         key=key+seed;
     38     return key;
     39 }
     40 
     41 //int count_key(int index)
     42 //{
     43 //    int i,key=0;
     44 //    for(i=0; i<k; i++)
     45 //    {
     46 //        key+=abs(c[index][i]);
     47 //        key%=seed;
     48 //    }
     49 //    return key;
     50 //}
     51 
     52 void InsertAndCheck(int index)
     53 {
     54     int i,key,last=0;
     55     key=count_key(index);
     56 //    cout<<"key="<<key<<endl;
     57     //    cout<<key<<endl;
     58     if(h[key].id==-1)
     59     {
     60         h[key].id=index;
     61     }
     62     else
     63     {
     64         bool found=false;
     65         for(i=key; i!=-1; i=h[i].next)
     66         {
     67 //            cout<<"run "<<index<<' '<<i<<endl;
     68             last=i;
     69             if(!found&&compair(index,h[i].id))
     70             {
     71                 if(index-h[i].id>maxlen)
     72                 {
     73                     maxlen=abs(h[i].id-index);
     74                     found=true;
     75                 }
     76             }
     77         }
     78         for(i=last; h[i].id!=-1; i=(i+1)%seed);
     79         h[last].next=i;
     80         h[i].id=index;
     81     }
     82 }
     83 
     84 int main()
     85 {
     86 //    freopen("in.txt","r",stdin);
     87     memset(h,-1,sizeof(h));
     88     scanf("%d%d",&n,&k);
     89     int i,j,tmp;
     90     n++;
     91     for(i=0; i<maxn; i++)
     92         h[i].next=-1;
     93     InsertAndCheck(1);
     94     for(i=2; i<=n; i++)
     95     {
     96         scanf("%d",&tmp);
     97         for(j=0; j<k; j++)
     98         {
     99             feature[i][j]=tmp%2;
    100             sum[i][j]=sum[i-1][j]+feature[i][j];
    101             c[i][j]=sum[i][j]-sum[i][0];
    102             tmp>>=1;
    103         }
    104         InsertAndCheck(i);
    105     }
    106 ////    for(i=0;i<=n;i++)
    107 ////    {
    108 ////        for(j=0;j<k;j++)
    109 ////            cout<<c[i][j]<<' ';
    110 ////        cout<<endl;
    111 ////    }
    112 //    for(i=0;i<6;i++)
    113 //        cout<<h[i].id<<' '<<h[i].next<<endl;
    114     printf("%d
    ",maxlen);
    115     return 0;
    116 }
  • 相关阅读:
    Java浮点数内存存储
    Spring Cloud(一)—— 一小时了解Spring Cloud
    Java基础(三)—— 常用类
    Oracle数据库(三)—— 表(一)
    Java资源记录
    Oracle数据库(一)—— 用户与表空间(常用命令)
    Oracle数据库(二)—— 用户与表空间(错误收集)
    Java Web(一)—— html
    Hibernate框架(二)—— Hibernate的持久化类
    项目 —— spring boot博客系统(一)—— 系统简介
  • 原文地址:https://www.cnblogs.com/aljxy/p/3450894.html
Copyright © 2011-2022 走看看