(中等) HDU 3335 , DLX+重复覆盖。

  Description

  As we know,the fzu AekdyCoin is famous of math,especially in the field of number theory.So,many people call him "the descendant of Chen Jingrun",which brings him a good reputation. 
  AekdyCoin also plays an important role in the ACM_DIY group,many people always ask him questions about number theory.One day,all members urged him to conduct a lesson in the group.The rookie daizhenyang is extremely weak at math,so he is delighted. 
  However,when AekdyCoin tells us "As we know, some numbers have interesting property. For example, any even number has the property that could be divided by 2.",daizhenyang got confused,for he don‘t have the concept of divisibility.He asks other people for help,first,he randomizely writes some positive integer numbers,then you have to pick some numbers from the group,the only constraint is that if you choose number a,you can‘t choose a number divides a or a number divided by a.(to illustrate the concept of divisibility),and you have to choose as many numbers as you can. 
  Poor daizhenyang does well in neither math nor programming.The responsibility comes to you!

  题目就是找到几个数,然后他们之间都不能整除。。。。。。

  这个题卡了一段时间。。。。。。看了大神们的题解之后才会的。。。。。。

  虽然这个题目让我们求得是最大值,其实是在最小值里面的最大值,DLX可重复覆盖的模板是为了找到最小值,所以找到最小值的过程中默认了选择某一个之后和他相关连的都不用在选择了。。。而这样正好就符合了这个题目的要求,也就是我们以找最小值的方法,找到的值就是符合题意的一个值。。。。。。然后改一下getH()函数,也就是获得启发值得函数,就OK了。。。

  构造的话就是n行n列就好,代表这n个数,(所以要求每一列都要被覆盖掉。。。。。。)。。。。。。

代码如下:

#include<iostream>
#include<cstring>

using namespace std;

const int MaxN=1010;
const int MaxM=1010;
const int MaxNode=MaxN*MaxM;

struct DLX
{
    int L[MaxNode],R[MaxNode],U[MaxNode],D[MaxNode],col[MaxNode];
    int H[MaxN],S[MaxM];
    int ansnum;
    int n,m,size;

    void init(int _n,int _m)
    {
        n=_n;
        m=_m;

        for(int i=0;i<=m;++i)
        {
            U[i]=D[i]=i;
            L[i]=i-1;
            R[i]=i+1;

            S[i]=0;
        }
        L[0]=m;
        R[m]=0;

        size=m;

        ansnum=0;

        for(int i=0;i<=n;++i)
            H[i]=-1;
    }

    void Link(int r,int c)
    {
        col[++size]=c;
        ++S[c];

        U[size]=U[c];
        D[size]=c;
        D[U[c]]=size;
        U[c]=size;

        if(H[r]==-1)
            H[r]=L[size]=R[size]=size;
        else
        {
            L[size]=L[H[r]];
            R[size]=H[r];
            R[L[H[r]]]=size;
            L[H[r]]=size;
        }
    }

    void remove(int c)
    {
        for(int i=D[c];i!=c;i=D[i])
        {
            L[R[i]]=L[i];
            R[L[i]]=R[i];
        }
    }

    void resume(int c)
    {
        for(int i=U[c];i!=c;i=U[i])
            L[R[i]]=R[L[i]]=i;
    }

    int getH()
    {
        int ret=0;

        for(int i=R[0];i!=0;i=R[i])
            ++ret;

        return ret;
    }

    void Dance(int d)
    {
        if(R[0]==0)
            if(d>ansnum)
                ansnum=d;

        if(getH()+d<=ansnum)
            return;

        int c=R[0];

        for(int i=R[0];i!=0;i=R[i])
            if(S[i]<S[c])
                c=i;

        for(int i=D[c];i!=c;i=D[i])
        {
            remove(i);

            for(int j=R[i];j!=i;j=R[j])
                remove(j);

            Dance(d+1);

            for(int j=L[i];j!=i;j=L[j])
                resume(j);

            resume(i);
        }
    }

};

DLX dlx;
unsigned long long num[1010];
int N;

void slove()
{
    dlx.init(N,N);

    for(int i=1;i<=N;++i)
        for(int j=1;j<=N;++j)
            if(num[i]%num[j]==0 || num[j]%num[i]==0)
                dlx.Link(i,j);

    dlx.Dance(0);

    cout<<dlx.ansnum<<endl;
}

int main()
{
    ios::sync_with_stdio(false);

    int T;
    cin>>T;

    while(T--)
    {
        cin>>N;

        for(int i=1;i<=N;++i)
            cin>>num[i];

        slove();
    }

    return 0;
}

时间: 2024-08-06 12:03:04

(中等) HDU 3335 , DLX+重复覆盖。的相关文章

(中等) HDU 2295 , DLX+重复覆盖+二分。

Description N cities of the Java Kingdom need to be covered by radars for being in a state of war. Since the kingdom has M radar stations but only K operators, we can at most operate K radars. All radars have the same circular coverage with a radius

hdu 2295 dlx重复覆盖+二分答案

题目大意: 有一堆雷达工作站,安放至多k个人在这些工作站中,找到一个最小的雷达监控半径可以使k个工作人所在的雷达工作站覆盖所有城市 二分半径的答案,每次利用dlx的重复覆盖来判断这个答案是否正确 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <algorithm> 5 #include <queue> 6 #include <climits

HDU 5046 Airport(DLX重复覆盖)

HDU 5046 Airport 题目链接 题意:给定一些机场,要求选出K个机场,使得其他机场到其他机场的最大值最小 思路:二分+DLX重复覆盖去判断即可 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 4005; const int MAXM = 65; const int MAXN = 65; const int INF = 0x3f3f3f3f; int K;

[ACM] HDU 2295 Radar (二分+DLX 重复覆盖)

Radar Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2593    Accepted Submission(s): 1012 Problem Description N cities of the Java Kingdom need to be covered by radars for being in a state of

[DLX重复覆盖] hdu 2828 Lamp

题意: 有N个灯M个开关 每个灯的ON和OFF状态都能控制一个灯是否亮 给出N行,代表对于每个灯 哪些开关的哪个状态可以使得第i个灯亮 思路: 这里需要注意一个问题 如果开关1的ON 状态和开关2的ON状态能使得1号灯亮 那么开关1.2同时处于ON的时候 1号灯也是亮的.意思就是只要有一个开关使得灯亮,灯就亮了. 简单的DLX 重复覆盖 行为每个开关的两个状态2*m行,列为n个灯 在搜索的同时标记一下哪个开关被用过了 那么另一个状态也不能用了 代码: #include"stdio.h"

FZU 1686 神龙的难题(DLX重复覆盖)

FZU 1686 神龙的难题 题目链接 题意:中文题 思路:每一个1看成列,每个位置作为左上角的矩阵看成行,dlx重复覆盖即可 代码: #include <cstdio> #include <cstring> using namespace std; const int MAXNODE = 66666; const int INF = 0x3f3f3f3f; const int MAXM = 230; const int MAXN = 230; int K; struct DLX

FZU 1686 神龙的难题 DLX重复覆盖

DLX重复覆盖: 需要一个A*函数剪支 Problem 1686 神龙的难题 Accept: 462    Submit: 1401 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 这是个剑与魔法的世界.英雄和魔物同在,动荡和安定并存.但总的来说,库尔特王国是个安宁的国家,人民安居乐业,魔物也比较少.但是.总有一些魔物不时会进入城市附近,干扰人民的生活.就要有一些人出来守护居民们不被魔物侵害.魔法使艾米莉

HDOJ 2828 Lamp DLX重复覆盖

DLX重复覆盖模版题: 每个开关两个状态,但只能选一个,建2m×n的矩阵跑DLX模版.... Lamp Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 855    Accepted Submission(s): 265 Special Judge Problem Description There are several switch

HDU 2295.Radar (DLX重复覆盖)

2分答案+DLX判断可行 不使用的估计函数的可重复覆盖的搜索树将十分庞大 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <vector> using namespace std; #define FOR(i,A,s) for(int i = A[s]; i != s; i = A[i]) #define exp 1e-8 con