【BZOJ 4069】 [Apio2015]巴厘岛的雕塑

4069:[apio2015]巴厘岛的雕塑

Time limit: 1000 ms

Memory limit: 65536 KB

Description

The province of Bali has many sculptures located on its roads. Let’s focus on one of its main roads.

There are N sculptures on that main road, conveniently numbered 1 through N consecutively. The age of sculpture i is Yi years. To make the road more beautiful, the government wants to partition the sculptures into several groups. Then, the government will plant beautiful trees between the groups, to attract more tourists to Bali.

Here is the rule in partitioning the sculptures:

The sculptures must be partitioned into exactly X groups, where A ≤ X ≤ B. Each group must consist of at least one sculpture. Each sculpture must belong to exactly one group. The sculptures in each group must be consecutive sculptures on the road.
For each group, compute the sum of the ages of the sculptures in that group.
Finally, compute the bitwise OR of the above sums. Let’s call this the final beauty value of the partition.
What is the minimum final beauty value that the government can achieve?

Note: the bitwise OR of two non-negative integers P and Q is computed as follows:

Convert P and Q into binary.
Let nP = number of bits of P, and nQ = number of bits of Q. Let M = max(nP, nQ).
Represent P in binary as pM-1pM-2 .. p1p0 and Q in binary as qM-1qM-2 .. q1q0, where pi and qi are the i-th bits of p and q, respectively. The (M-1)st bits are the most significant bits, while the 0th bits are the least significant bits.
P OR Q, in binary, is defined as (pM-1 OR qM-1)(pM-2 OR qM-2)..(p1 OR q1)(p0 OR q0), where
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1
Input Format

The first line contains three space-separated integers N, A, and B. The second line contains N space-separated integers Y1, Y2, …, YN.

Output Format

A single line containing the minimum final beauty value.

Sample Input

6 1 3
8 1 2 1 5 4
Sample Output

11
Explanation

Partition the sculptures into 2 groups: (8 1 2) and (1 5 4). The sums are (11) and (10). The final beauty value is (11 OR 10) = 11.

Subtasks

Subtask 1 (9 points)

1 ≤ N ≤ 20
1 ≤ A ≤ B ≤ N
0 ≤ Yi ≤ 1,000,000,000
Subtask 2 (16 points)

1 ≤ N ≤ 50
1 ≤ A ≤ B ≤ min(20, N)
0 ≤ Yi ≤ 10
Subtask 3 (21 points)

1 ≤ N ≤ 100
A = 1
1 ≤ B ≤ N
0 ≤ Yi ≤ 20
Subtask 4 (25 points)

1 ≤ N ≤ 100
1 ≤ A ≤ B ≤ N
0 ≤ Yi ≤ 1,000,000,000
Subtask 5 (29 points)

1 ≤ N ≤ 2,000
A = 1
1 ≤ B ≤ N
0 ≤ Yi ≤ 1,000,000,000

贪心+dp。

从最高位开始枚举,能是1就是1,如何判断当前这一位(第 k 位)能否是1呢?

用dp来做!

f[i][j]表示前 i 个数分成j位满足之前 k1 位的答案(答案中是0的位任何一段不能是1)且第 k 位是1。

dp完后判断一下f[n][j],j[A,B]中有没有为 true 的即可。

但是这样做复杂度是 O(logMn3) ,过不了subtask5。

注意到这组数据中 A=1 ,是没有下限的,只要把dp函数变成 g[i] 表示前 i 个数符合条件至少要分几组,判断g[n]是否 B 即可。

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#define LL long long
#define inf 0x3f3f3f3f
using namespace std;
LL a[2005];
int n,A,B,f[105][105],g[2005];
int ok(LL x,LL y)
{
    return (x|y)==y;
}
int Log(LL x)
{
    int ans=0;
    LL b=1;
    while (b<x)
    {
        b<<=1LL;
        ans++;
    }
    return ans;
}
void Clear()
{
    for (int i=1;i<=n;i++)
        for (int j=1;j<=i;j++)
            f[i][j]=0;
}
void Work1()
{
    LL ans=0;
    for (int now=Log(a[n]);now;now--)
    {
        for (int i=1;i<=n;i++)
            g[i]=inf;
        g[0]=0;
        for (int i=1;i<=n;i++)
            for (int j=0;j<i;j++)
            {
                LL x=a[i]-a[j];
                if (x&(1LL<<(now-1))) continue;
                if (ok(x>>now,ans))
                    g[i]=min(g[i],g[j]+1);
            }
        ans<<=1LL;
        if (g[n]>B) ans|=1;
    }
    cout<<ans<<endl;
}
void Work2()
{
    LL ans=0;
    int tot=Log(a[n]);
    for (int now=tot;now;now--)
    {
        Clear();
        f[0][0]=1;
        for (int i=1;i<=n;i++)
            for (int j=1;j<=i;j++)
                for (int k=0;k<i;k++)
                    if (f[k][j-1])
                    {
                        LL x=a[i]-a[k];
                        if (x&(1LL<<(now-1))) continue;
                        if (ok(x>>now,ans))
                            f[i][j]=1;
                    }
        int t=0;
        for (int i=A;i<=B;i++)
            t|=f[n][i];
        ans<<=1LL;
        if (!t) ans|=1;
    }
    cout<<ans<<endl;
}
int main()
{
    //freopen("t.in","r",stdin);freopen("t.out","w",stdout);
    scanf("%d%d%d",&n,&A,&B);
    for (int i=1;i<=n;i++)
        scanf("%I64d",&a[i]),a[i]=a[i]+a[i-1];
    if (A==1) Work1();
    else Work2();
    return 0;
}

这里写图片描述

这是一道对dp灵活应用的好题啊。

对于前四个subtask的dp方程其实是递推,无法直接判断前 n 位是否可行,我们就从第1位开始判断,因为满足后面的前提是要满足前面的。

第五个subtask没有了下限A,变成了最优性问题,就可以dp来做了。

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值