Ural2110 : Remove or Maximize

设最大的数为$w$,若$n>k+\log w$,那么显然所有$1$都可以保留,否则现在$n\leq k+\log w$。

如果$w\leq 100000$,那么可以DP,设$f[i][j]$表示考虑前$i$个数,保留的数的$or$是$j$时,最多能删除多少个数,时间复杂度$O(nw)$。

如果$w>100000$,那么$k\leq 7$,爆搜即可,时间复杂度$O(C(n,k))$。

#include<cstdio>
#include<algorithm>
using namespace std;
const int N=100010;
int n,m,i,j,o,ans,mx,f[2][131100],a[N];
void dfs(int x,int y,int z){
  if(y+n-x+1<m)return;
  if(x>n){
    ans=max(ans,z);
    return;
  }
  dfs(x+1,y,z|a[x]);
  if(y<m)dfs(x+1,y+1,z);
}
int main(){
  scanf("%d%d",&n,&m);
  for(i=1;i<=n;i++)scanf("%d",&a[i]),mx=max(mx,a[i]);
  if(n>m+31){
    for(i=1;i<=n;i++)ans|=a[i];
    return printf("%d",ans),0;
  }
  if(mx<=100000){
    for(i=0;i<131072;i++)f[0][i]=-N;
    f[0][0]=0;
    for(i=o=1;i<=n;i++,o^=1){
      for(j=0;j<131072;j++)f[o][j]=f[o^1][j]+1;
      for(j=0;j<131072;j++)if(f[o^1][j]>=0)f[o][j|a[i]]=max(f[o][j|a[i]],f[o^1][j]);
    }
    for(i=131071;~i;i--)if(f[o^1][i]>=m)break;
    return printf("%d",i),0;
  }
  dfs(1,0,0);
  return printf("%d",ans),0;
}

  

时间: 2024-10-15 04:45:51

Ural2110 : Remove or Maximize的相关文章

Five ways to maximize Java NIO and NIO.2--reference

Java NIO -- the New Input/Output API package-- was introduced with J2SE 1.4 in 2002. Java NIO's purpose was to improve the programming of I/O-intensive chores on the Java platform. A decade later, many Java programmers still don't know how to make th

android导入其他工程源码包后出现大量错误提示remove @Override annotation 的解决办法

问题描述: GitHub 下载源码后将其com包内容导入自己android项目中,大量出现错误修改提示 remove @Override annotation 问题解法: 1. 2. 3. 将Compiler compliance level:设置在1.6及以上即可

LeetCode 19. Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5. Note:Given n

27. Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. AC代码: class Solution(object): def removeElement(self, num

leetcode 27 Remove Element

Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 题解:水题.遇到val就和最后一个不是val的数交换位置就好了. class Solution { public:

19 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head. For example,    Given linked list: 1->2->3->4->5, and n = 2.    After removing the second node from the end, the linked list becomes 1->2->3->5. Note:G

mv command:unable to remove target: Is a director

mv command:unable to remove target: Is a director This is somewhat simple as long as we understand the concept. mv or move does not actually move the file/folder to another location within the same device, it merely replaces the pointer in the first

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

Leetcode: Remove Element

题目: Given an array and a value, remove all instances of that value in place and return the new length. The order of elements can be changed. It doesn't matter what you leave beyond the new length. 这道题比较简单直接看代码,这里多写几个版本的作参考! C++版本 class Solution { pub