JZOJ 5185. 【NOIP2017提高组模拟6.30】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
 
做法:本题可以用线段树暴力打,但下面给出的代码不是,而是用一种比较巧妙的方法(详情看代码)。
代码如下:
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #define N 2500007
 5 #define LL long long
 6 using namespace std;
 7 int n, m;
 8 LL a[N], Ans1, Ans2, M;
 9 LL list[N], List[N];
10 
11 LL max(LL x, LL y) 
12 {
13     if (x > y)    return x;
14     return y;
15 }
16 
17 void Pre_work()
18 {
19     for (int i = 1; i <= n; i += m)
20     {
21         list[i] = a[i];
22         for (int j = i + 1; j <= min(i + m - 1, n); j++)
23             list[j] = list[j - 1] & a[j];
24     }
25     for (int i = 1; i <= n; i += m)
26     {
27         List[min(i + m - 1, n)] = a[min(i + m - 1, n)];
28         for (int j = min(i + m - 1, n) - 1; j >= i; j--)
29             List[j] = List[j + 1] & a[j];
30     }
31 }
32 
33 int main()
34 {
35     scanf("%d%d", &n, &m);
36     for (int i = 1; i <= n; scanf("%lld", &a[i++]), Ans1 = Ans1 | a[i - 1]);
37     Pre_work();
38     for (int i = m; i <= n; i++)
39     {
40         if (i % m == 0)
41             Ans2 = max(Ans2, list[i]);
42         else Ans2 = max(Ans2, List[i - m + 1] & list[i]);
43     }
44     printf("%lld %lld", Ans1, Ans2);
45 }
View Code

猜你喜欢

转载自www.cnblogs.com/traveller-ly/p/9478083.html