UVA 12589 Learning Vector

題目鏈接:http://acm.hust.edu.cn/vjudge/problem/35247


題意:有n個向量裏面選k個,從(0,0)開始畫,求與x軸圍成的最大面積,輸出面積的二倍。


思路:優先畫斜率大的,這樣高度會盡量高,每當x增加的時候,面積也會增加的多一些。計算的時候直接算面積的二倍,一定是整數。

dp[i][h]表示已經選了i個且當前高度爲h時的最大面積。dp[i][h] = max( dp[i-1][h-a[i].y] + 2 * ( h-a[i].y ) * a[i].x +  area(a[i])*2 )


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

#define Clean(x,y) memset(x,y,sizeof(x))
#define LL long long
#define ULL unsigned long long
#define inf 0x7fffffff
#define mod 100000007

const int maxn = 55;

struct node
{
    int x,y;
    int area;
}p[maxn];
int n,m,H;

int dp[maxn][maxn*maxn];
bool flag[maxn][maxn*maxn];

bool cmp(node a,node b)
{
    return a.y*b.x > a.x*b.y;
}

void init()
{
    H = 0;
    cin>>n>>m;
    rep(i,1,n)
    {
        cin>>p[i].x>>p[i].y;
        p[i].area = p[i].x * p[i].y;
        H += p[i].y;
    }
    sort(p+1,p+1+n,cmp);
    Clean(flag,false);
}

int solve()
{
    Clean(dp,0);
    flag[0][0] = true;
    rep(i,1,n)
        Rrep(j,m,1)
        {
            Rrep(h,H,p[i].y)
            if ( flag[j-1][h-p[i].y] )
            {
                flag[j][h] = true;
                dp[j][h] = max( dp[j][h] , dp[j-1][h-p[i].y] + 2*p[i].x *(h-p[i].y) +  p[i].area );
            }
        }
    int ans = 0;
    rep(i,0,H) ans = max( ans , dp[m][i] );
    return ans;
}

int main()
{
    int T;
    cin>>T;
    rep(kase,1,T)
    {
        init();
        printf("Case %d: %d\n",kase,solve());
    }
    return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章