JZOJ5185 tty's sequence

Description

    

Input

Output

Sample Input

input 1:
  6 3
  1 1 1 0 0 0
input 2:
  6 3
  1 1 0 1 0 0
input 3:
  6 3
  11 8 2 1 3 9

Sample Output

output 1
  1 1
output 2
  1 0
output 3
  11 1

Data Constraint

    

Solution

  不难发现一个数字and越多的数会不变或者变小,or越多的数会不变或者变大,所以全部的数or就是答案,对于and就取刚好k位,把数列全部转成二进制,逐位处理是否形成连续k个1。
 1 #include<cstdio>
 2 using namespace std;
 3 int n,m,h,a[1100000],f[1100000][40],l,s[40],ma;
 4 int main()
 5 {
 6     scanf("%d%d",&n,&m);
 7     for (int i=1;i<=n;i++)
 8     {
 9         scanf("%d",&a[i]);
10          int x=a[i],ll=0,p=1;
11          while (x!=0)
12          {
13              f[i][++ll]=x%2;
14              x=x/2;
15              s[ll]=p;
16              p=p*2;
17         }
18         h=h|a[i];
19         if (ll>l) l=ll;
20     }
21     for (int i=1;i<=n;i++)
22         for (int j=1;j<=l;j++)
23             if (f[i][j]!=0)
24                 f[i][j]=f[i-1][j]+1;
25     int ans=0;
26     for (int i=m;i<=n;i++)
27     {
28         int ans=0;
29         for (int j=1;j<=l;j++)
30             if (f[i][j]>=m)
31                 ans=ans+s[j];
32         if (ans>ma) ma=ans;
33     }
34     printf("%d %d\n",h,ma);
35 }
View Code

 

 
posted @ 2018-08-16 07:52  kasiruto  阅读(131)  评论(0编辑  收藏  举报