Thứ Tư, 27 tháng 4, 2022

HCLN C++

#include <bits/stdc++.h>
using namespace std;

struct hcn
{
    int x1,y1,x2,y2,vt;
};

hcn h[105];
int n,f[105],t[105];

void nhap()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>h[i].x1>>h[i].y1>>h[i].x2>>h[i].y2;
        h[i].vt=i;
    }
}

int dientich(hcn a)
{
    return (a.x2-a.x1)*(a.y2-a.y1);
}

bool bao(hcn a,hcn b)   //kiem tra hcn a bao hcn b
{
    if(a.x1<=b.x1&&a.y1<=b.y1&&a.x2>=b.x2&&a.y2>=b.y2) return true;
    else return false;
}

bool ss(hcn a,hcn b)
{
    if(dientich(a)>dientich(b)) return true;
    return false;
}

void truy_vet(int i)
{
    while(i>0)
    {
        cout<<h[i].vt<<endl;
        i=t[i];
    }
}

void giai()
{
    int jmax;
    sort(h+1,h+1+n,ss);
   // for(int i=1;i<=n;i++)
  //      cout<<h[i].vt<<" ";
 //   cout<<endl;
    f[0]=0;f[1]=1;
    for(int i=2;i<=n;i++)
    {
        jmax=0;
        for(int j=1;j<=i-1;j++)
            if(bao(h[j],h[i])==true&&f[j]>f[jmax]) jmax=j;
        f[i]=f[jmax]+1;
        t[i]=jmax;
    }
    int res=0;
    for(int i=1;i<=n;i++)
        if(f[i]>f[res]) res=i;
    cout<<f[res]<<endl;
    truy_vet(res);

}

int main()
{
    freopen("hcnln.inp","r",stdin);
    freopen("hcnln.out","w",stdout);
    nhap();
    giai();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.