CodeChef--July Lunchtime 2014_And Operation

寻找最大位与运算对
本文探讨了在给定非负整数数组中,如何找到并计算最大位与运算值的策略,通过优化算法从O(n^2)复杂度降至O(n),并详细解释了实现过程。

                                               And Operation

Given an array of n non-negative integers: A1, A2, …, AN. Your mission is finding a pair of integers Au, Av (1 ≤ u < vN) such that (Au and Av) is as large as possible.
And is a bit-wise operation which is corresponding to & in C++ and Java.

 

Input

The first line of the input contains a single integer N. The ith line in the next N lines contains the Ai.

 

Output

Contains a single integer which is the largest value of Au and Av where 1 ≤ u < vN.

 

Constraints

50 points:

  • 2N5000
  • 0Ai109

50 points:

  • 2N3 × 105
  • 0Ai109

 

Example

Input:
4
2
4
8
10

Output:
8

 

Explanation

  • 2 and 4 = 0
  • 2 and 8 = 0
  • 2 and 10 = 2
  • 4 and 8 = 0
  • 4 and 10 = 0
  • 8 and 10 = 8

题目大意是给你n个整数,从中找出两个整数u,v,使得u&v得到最大值(&是c++中的和运算,等于pascal中的and)。

一开始我想到了暴力,枚举每一对然后比较,N最大是3*10^5,枚举是(1+3*10^5-1)*(3*10^5)/2,估计算一下应该是超过10^9,就TLE了。
暴力代码:
#include<iostream>
using namespace std;
const long maxx=3*100000;
long a[maxx];
long ans;
long n;
int main()
{
	cin>>n;
	for(int j=1; j<=n; j++)
	cin>>a[j];
	for (int i=1; i<=n; i++)
	  for (int j=i+1; j<=n; j++)
	  {
	  	long c;
	  	c=a[i]&a[j];
	  	ans=max(ans,c);
	  }
	  cout<<ans<<endl;
	  return 0;
}
然后我觉得应该有什么规律,应为我找不到别的算法来优化。这条题的时间复杂度应该可以从O(n^2)→O(n),然后我用程序枚举了1....2...100&8的值发现除了0,就是8了。
上面的例子是8&10为最大,比较一下看看。
 8的二进制是1000
10的二进制是1011
8&10     得到   1000
发现了没,如果u,v的二进制第一位相同那么他们得到的&运算会尽可能的大,u,v的二进制第一位要相同,那么他们之前的差距要尽可能的小。
所以我们每一次只需要比较排序后,每两两之间的&运算就行了。

O(n)代码:
#include<iostream>
#include<algorithm>
using namespace std;
long n;
long a[300005];
long maxx=0;
int main()
{
	cin>>n;
	for (int i=1; i<=n; i++)
	cin>>a[i];
	sort(a+1, a+n+1);
	for (int i=1; i<=n-1; i++)
	{
		long b=a[i]&a[i+1];
		maxx=max(maxx,b);
	}
	cout<<maxx<<endl;
	return 0;
}



Data Group Science Data Sets (HDF Layers (22)) Units Data Type Fill Value Valid Range Scale Fac￾tor 1 km num_observations_1km: Number of Observations none 8-bit signed integer -1 0 - 127 NA state_1km_1: Reflectance Data State (see Table 13) Bit Field 16-bit unsigned integer 65535 0 - 57343 NA SensorZenith_1 Degree 16-bit signed integer -32767 0 - 18000 0.01 SensorAzimuth_1 Degree 16-bit signed integer -32767 -18000 - 18000 0.01 Range_1: pixel to sensor Meter 16-bit unsigned integer 27000 - 65535 0.04 SolarZenith_1 Degree 16-bit signed integer -32767 0 - 18000 0.01 SolarAzimuth_1 Degree 16-bit signed integer -32767 -18000 - 18000 0.01 gflags_1: Geolocation flags (see Table 16) Bit Field 8-bit unsigned integer 255 0 - 248 NA orbit_pnt_1: Orbit Pointer none 8-bit signed integer -1 0 - 15 NA granule_pnt_1: Granule Pointer none 8-bit unsigned integer 255 0-254 NA MOD09 - Collection 6 - July 2020 15 500 m num_observations_500m none 8-bit signed integer -1 0 - 127 NA sur_refl_b01_1: 500m Surface Reflectance Band 1 (620-670 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b02_1: 500m Surface Reflectance Band 2 (841-876 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b03_1: 500m Surface Reflectance Band 3 (459-479 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b04_1: 500m Surface Reflectance Band 4 (545-565 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b05_1: 500m Surface Reflectance Band 5 (1230-1250 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b06_1: 500m Surface Reflectance Band 6 (1628-1652 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 sur_refl_b07_1: 500m Surface Reflectance Band 7 (2105-2155 nm) Reflectance 16-bit signed integer -28672 -100 - 16000 0.0001 QC_500m_1: 500m Reflectance Band Quality (see Table 10) Bit Field 32-bit unsigned integer 787410671 NA NA obscov_500m_1: Observation coverage Percent 8-bit signed integer -1 0 - 100 0.01 iobs_res_1: Observation number none 8-bit unsigned integer 255 0 - 254 NA q_scan_1: 250m scan value information (see Table 17)这是mod09GA 的指导指南里的所有变量,没有band11和band12
07-17
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值