5185. 【NOIP2017提高组模拟6.30】tty's sequence (Standard IO)
Time Limits:
1000 ms Memory Limits: 262144 KB Detailed Limits
Goto ProblemSet
做法:本题可以用线段树暴力打,但下面给出的代码不是,而是用一种比较巧妙的方法(详情看代码)。
代码如下:
View Code
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 }