长长久久【C语言】


解题思路

复制代码
#include<stdio.h>
#define MAX_N 100005
int a[MAX_N];
int cnt[32];
int main(){
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=0;i<32;i++){
        cnt[i]=0;
    }
    for(int i=0;i<k;i++){
        int num=a[i];
        for(int j=0;j<32;j++){
            if(num&(1<<j)){
                cnt[j]++;
            }
        }
    }
    int max_or=0;
    for(int j=0;j<32;j++){
        if(cnt[j]>0){
            max_or|=(1<<j);
        }
    }
    for(int i=k;i<n;i++){
        int remove=a[i-k];
        for(int j=0;j<32;j++){
            if(remove&(1<<j)){
                cnt[j]--;
            }
        }
        int add=a[i];
        for(int j=0;j<32;j++){
            if(add&(1<<j)){
                cnt[j]++;
            }
        }
        int current_or=0;
        for(int j=0;j<32;j++){
            if(cnt[j]>0){
                current_or|=(1<<j);
            }
        }
        if(current_or>max_or){
            max_or=current_or;
        }
    }
    printf("%d\n",max_or);
    return 0;
}
相关推荐
炸膛坦客2 小时前
单片机/C/C++八股:(三十七)指针数组 和 数组指针
c语言·开发语言·c++
Navigator_Z7 小时前
LeetCode //C - 1320. Minimum Distance to Type a Word Using Two Fingers
c语言·算法·leetcode
发疯的技术鼠8 小时前
第21篇-联合和枚举
c语言
23图20 小时前
C 语言开散列表(拉链法)拆解:链挂得没错,key=-1 会去读 T[-1]
c语言·开发语言·散列表
小飞在努力21 小时前
【实现负载均匀在线判题③】从零散模块到统一,编译运行全链路封装
c语言·开发语言·c++·笔记
我是码代码1 天前
高分辨率ADC布局详解:走线压降、回流路径与地平面处理
linux·c语言·嵌入式
炸膛坦客1 天前
单片机/C/C++八股:(三十四)C 语言函数参数入栈顺序
c语言·开发语言·c++
风落青芜1 天前
排序算法(二):选择排序、堆排序与冒泡排序
c语言·开发语言·数据结构·算法·排序算法
Navigator_Z1 天前
LeetCode //C - 1319. Number of Operations to Make Network Connected
c语言·算法·leetcode
Zwawa1 天前
步进电机路径规划:梯形曲线与S型曲线
c语言·单片机·嵌入式