codeforces 981 D. Bookshelves(DP+贪心)

题目链接:http://codeforces.com/contest/981/problem/D

思路:从高位到低位枚举答案位数,然后dp验证哪一位能不能取到,emmm感觉自己dp是真的垃圾,思路很快想到,dp不会,之后参考别人的。。。有空再去练专题吧

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll inff = 0x3f3f3f3f3f3f3f3f;
#define FOR(i,a,b) for(int i(a);i<=(b);++i)
#define FOL(i,a,b) for(int i(a);i>=(b);--i)
#define REW(a,b) memset(a,b,sizeof(a))
#define inf int(0x3f3f3f3f)
#define si(a) scanf("%d",&a)
#define sl(a) scanf("%I64d",&a)
#define sd(a) scanf("%lf",&a)
#define ss(a) scanf("%s",a)
#define mod int(1e9+7)
#define lc (d<<1)
#define Pll pair
#define P pair
#define pi acos(-1)
int n,k,dp[58][58];
ll a[58],b[58],s=0;
bool as(ll x)
{
    REW(dp,0);
    dp[0][0]=1;
    FOR(i,1,k)
    {
        FOR(j,1,n)
        {
            FOR(z,1,j)
            {
                if(((b[j]-b[z-1])&x)==x&&dp[i-1][z-1]==1)  dp[i][j]=1;
            }
        }
    }
    return dp[k][n];
}
int main()
{
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    FOR(i,1,n)  sl(a[i]),b[i]=b[i-1]+a[i];
    FOL(i,60,0)
    {
        if(as(s|1ll<


你可能感兴趣的:(dp,贪心)