Codeforces Round # 320 "Or"게임 비트 운영 접두사 및 폭력

주제 링크

http://codeforces.com/problemset/problem/578/B

표제

2e5 배열, 모든 요소에 대해 * x 연산을 최대 k 번 수행 할 수 있으며 처리 후 각 요소의 비트 단위 OR의 최대 값을 찾습니다.

아이디어

첫째, 동일한 수에 대해 k 번 수행해야 함을 증명할 수있다.

x가 2보다 크므로 이진 문자열은 연산 후 최소 1 비트 씩 증가해야하며, 모두 하나에 할당되지 않은 경우 가장 높은 비트가 더 크고 응답이 더 큰 상황으로 다시 할당해야합니다.

가장 큰 숫자에 대해 k 연산을 단순히 수행 할 수 없습니다. 예를 들어 100110, x = 2, k = 1이고 100에 대한 답은 1110이고 110에 대한 답은 1100입니다.

폭력을 고려하여 특별한 접두사와 최적화를 사용합니다 .pr은 비트 or의 접미사 합을 유지하고 ta는 비트 or의 접미사 합을 유지하므로 O (1)에서 요소 당 k 연산을 처리하고 각 요소를 순회 할 수 있습니다. , 답을 기록하십시오. 시간 복잡도는 O (n)입니다.

암호

#include<cstdio>
#include<iostream>
#include<iomanip>
#include<map>
#include<string>
#include<queue>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib> 
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl "\n"
#define int long long
//#define double long double
using namespace std;
	typedef long long ll;
	const int maxn=200500;
	const int inf=0x3f3f3f3f;
	int n,k,x;
	int a[maxn];
	int pr[maxn],ta[maxn];
	signed main(){
    
    
		IOS
		
		cin>>n>>k>>x; 
		for(int i=1;i<=n;i++)
			cin>>a[i];
		a[0]=a[n+1]=0;
		for(int i=1;i<=n;i++){
    
    
			pr[i]=a[i]|pr[i-1];
		}
		for(int i=n;i>=1;i--){
    
    
			ta[i]=a[i]|ta[i+1];
		}
		int p=1;
		while(k--){
    
    
			p*=x;
		}
		int ans=-inf;
		for(int i=1;i<=n;i++){
    
    
			int t=pr[i-1]|(a[i]*p)|ta[i+1];
			ans=max(ans,t);
		}
		cout<<ans<<endl;
	}


추천

출처blog.csdn.net/TheSunspot/article/details/109138880