Codeforces Gym 101608 G WiFi Password(尺取/二分+ST表 )

1 篇文章 0 订阅
1 篇文章 0 订阅

G. WiFi Password
time limit per test  2.0 s
memory limit per test  256 MB
input  wifi.in
output  standard output

Just days before the JCPC, your internet service went down. You decided to continue your training at the ACM club at your university. Sadly, you discovered that they have changed the WiFi password. On the router, the following question was mentioned, the answer is the WiFi password padded with zeros as needed.

A subarray [l, r] of an array A is defined as a sequence of consecutive elements Al, Al + 1, ..., Ar, the length of such subarray is r - l + 1. The bitwise OR of the subarray is defined as: Al OR Al + 1 OR ... OR Ar, where OR is the bitwise OR operation (check the notes for details).

Given an array A of n positive integers and an integer v, find the maximum length of a subarray such that the bitwise OR of its elements is less than or equal to v.

Input

The first line contains an integer T (1 ≤ T ≤ 128), where T is the number of test cases.

The first line of each test case contains two space-separated integers n and v (1 ≤ n ≤ 105) (1 ≤ v ≤ 3 × 105).

The second line contains n space-separated integers A1, A2, ..., An (1 ≤ Ai ≤ 2 × 105), the elements of the array.

The sum of n overall test cases does not exceed 106.

Output

For each test case, if no subarray meets the requirement, print 0. Otherwise, print the maximum length of a subarray that meets the requirement.

Example
input
Copy
3
5 8
1 4 5 3 1
5 10
8 2 6 1 10
4 2
9 4 5 8
output
5
3
0
Note

To get the value of x OR y, consider both numbers in binary (padded with zeros to make their lengths equal), apply the OR operation on the corresponding bits, and return the result into decimal form. For example, the result of 10 OR 17 = 01010 OR 10001 = 11011 = 27.



【思路】

题目给了一个规模为1e5的序列,和一个数v,要求最长的满足区间或值小于等于v的最长区间的长度。

在此有两个思路:

由于每个数的大小都不超过2e5,所有数的或值不超过1e6,每个数的二进制表示也就不超过20位,可以将区间或值的每一位单独拿出来维护,维护每一位1的个数,我们需要得到的也就是这个或值不超过v的最长区间的长度,所以尺取可做,时间复杂度为O(N)级别。

因为或运算有只能使结果更大的性质,类似于求RMQ,我们可以用ST表维护区间或值,然后二分区间长度来得到满足条件的最长区间。求ST表的时间复杂度为O(NlogN),二分判断的时间复杂度为O(NlogN),所以总时间复杂度也是(NlogN)。


【代码1】

//************************************************************************
// File Name: H.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com 
// Created Time: 2018年03月24日 星期六 14时29分28秒
//************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;

struct num {
	int dig[21];

	num(int x)
	{
		for (int i = 1; i <= 20; i++) dig[i] = (x >> (i - 1)) & 1;
	}

	bool operator<(const num &another) const
	{
		for (int i = 20; i >= 1; i--) {
			if (dig[i] >= 1 && another.dig[i] >= 1)
				continue;
			else
			if (dig[i] == 0 && another.dig[i] == 0)
				continue;
			else
				return  dig[i] < another.dig[i];
		}
		return false;
	}
	
	bool operator==(const num &another) const
	{
		for (int i = 1; i <= 20; i++)
			if (dig[i] == 0 && another.dig[i] > 0 || dig[i] > 0 && another.dig[i] == 0)
				return false;
		return true;
	}
};

int t, n, k;
int a[MAXN];

int main()
{
	freopen("wifi.in", "r", stdin);
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &k);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		num stand(k);
		num now(0);
		int ans = 0, l = 0, r = 0;
		while (true) {
			r++;
			if (r > n) {
				ans = max(ans, r - l - 1);
				break;
			}
			num p(a[r]);
			for (int i = 1; i <= 20; i++) now.dig[i] += p.dig[i];
			if (stand < now) {
				ans = max(ans, r - l - 1);
				while (stand < now && l < r) {
					l++;
					num pp(a[l]);
					for (int i = 1; i <= 20; i++) now.dig[i] -= pp.dig[i];
				}
			}
		}
		printf("%d\n", ans);
	}
	return 0;
}

【代码2】

//************************************************************************
// File Name: main.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com 
// Created Time: 2018年03月25日 星期日 21时38分23秒
//************************************************************************

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 1e5 + 5;

int t, n, v;
int a[MAXN], log[MAXN];
int st[MAXN][32];

void st_prepare()
{
	log[1] = 0;
	for (int i = 2; i <= n; i++) {
		log[i] = log[i - 1];
		if (1 << (log[i] + 1) == i) log[i]++;
	}
	for (int i = n; i >= 1; i--) {
		st[i][0] = a[i];
		for (int j = 1; i + (1 << j) - 1 <= n; j++)
			st[i][j] = st[i][j - 1] | st[i + (1 << j - 1)][j - 1];
	}
}

int query(int l, int r)
{
	int k = log[r - l + 1];
	return (st[l][k] | st[r - (1 << k) + 1][k]);
}

bool check(int x)
{
	int k = log[x];
	for (int i = 1; i <= n - x + 1; i++)
		if (query(i, i + x - 1) <= v) return true;
	return false;
}

int main()
{
	freopen("wifi.in", "r", stdin);
	scanf("%d", &t);
	while (t--) {
		scanf("%d %d", &n, &v);
		for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
		st_prepare();
		int l = 1, r = n, ans = 0;
		while (l <= r) {
			int mid = (l + r) >> 1;
			if (check(mid)) {
				ans = max(ans, mid);
				l = mid + 1;
			}
			else
				r = mid - 1;
		}
		printf("%d\n", ans);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值