HDU 4562 守护雅典娜(动态规划)

来源:互联网 发布:vb中怎么计算运行次数 编辑:程序博客网 时间:2024/05/04 23:37

答案分为三种只能只有包含雅典娜的塔,只有包含怪兽的塔,包含雅典娜和怪兽的塔的和。

使用dp可以分别计算包含雅典娜、怪兽的最厚塔层数,过程类似LIS。枚举这两种情况的塔数,求和计算第三种情况。


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <string>#include <map>#include <set>using namespace std;typedef long long LL;const int maxn=1005;struct Circle{    int x,y,r;    bool checkIn(int sx,int sy)    {        sx-=x;        sy-=y;        return sx*sx+sy*sy<r*r;    }    bool checkOut(int sx,int sy)    {        sx-=x;        sy-=y;        return sx*sx+sy*sy>r*r;    }    bool operator <(const Circle & c) const    {        return r<c.r;    }};bool judgeIn(const Circle &a,const Circle &b){    int r=max(a.r,b.r)-min(a.r,b.r);    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)<r*r;}bool judgeOut(const Circle &a,const Circle &b){    int r=a.r+b.r;    return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)>r*r;}vector<Circle> a,b;int dp1[maxn],dp2[maxn];void solve(vector<Circle>& v,int *dp,int &ans){    sort(v.begin(),v.end());    for(int i=0; i<v.size(); ++i)    {        dp[i]=1;        for(int j=0; j<i; ++j)        {            if(judgeIn(v[i],v[j]))                dp[i]=max(dp[i],dp[j]+1);        }        ans=max(ans,dp[i]);    }}int main(){    int T,kase=0;    scanf("%d",&T);    while(T--)    {        int n,x,y;        scanf("%d%d%d",&n,&x,&y);        a.clear();        b.clear();        for(int i=1; i<=n; ++i)        {            Circle c;            scanf("%d%d%d",&c.x,&c.y,&c.r);            if(c.checkIn(0,0)&&c.checkOut(x,y))                a.push_back(c);            else if(c.checkIn(x,y)&&c.checkOut(0,0))                b.push_back(c);        }        int ans=0;        solve(a,dp1,ans);        solve(b,dp2,ans);        for(int i=0; i<a.size(); ++i)        {            for(int j=0; j<b.size(); ++j)            {                if(judgeOut(a[i],b[j]))                    ans=max(dp1[i]+dp2[j],ans);            }        }        printf("Case %d: %d\n",++kase,ans);    }    return 0;}


0 0
原创粉丝点击