BZOJ4976 宝石镶嵌(动态规划)
显然被留下的宝石应该贡献至少一位,否则就可以扔掉。所以如果n-k>=logw,直接输出所有数的or。现在n变得和k同阶了。于是设f[i][j]为前i个数or为j时至少选几个数,转移显然。当然可以只开一维。
#include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define ll long long #define N 100010 #define M 120 char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;} int gcd(int n,int m){return m==0?n:gcd(m,n%m);} int read() { int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f; } int n,m,a[N],f[1<<17],ans; int main() { #ifndef ONLINE_JUDGE freopen("bzoj4976.in","r",stdin); freopen("bzoj4976.out","w",stdout); const char LL[]="%I64d\n"; #else const char LL[]="%lld\n"; #endif n=read(),m=n-read(); for (int i=1;i<=n;i++) ans|=a[i]=read(); if (m>=17) {cout<<ans;return 0;} memset(f,42,sizeof(f)); f[0]=0; for (int i=1;i<=n;i++) for (int j=0;j<(1<<17);j++) f[j|a[i]]=min(f[j|a[i]],f[j]+1); for (int i=(1<<17)-1;~i;i--) if (f[i]<=m) {cout<<i;break;} return 0; }