Uva-12589 Learning Vector

题目链接:Learning Vector

题目大意:给N个向量,选取K个,使得这些向量与x轴和最右侧的直线围成的封闭区间面积最大。

解题思路:一开始没有排序就DP没有过,后听菊苣按照斜率排序就A了。后来想了一下这样做是为了避免后效性,或者说在选N个的情况下按斜率排序才是最优解,在前提的最优解下DP才能找到最终的最优解。

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int inf  = 0x3f3f3f3f;
const int maxn = 2e4 + 15;

int dp[55][55][2550];
int vis[55][55][2550];
P a[55];
int n, k, kase = 1; 

int DP(int x, int cnt, int h){
    if(cnt == k) return 0;
    if(x == n + 1) return 0;
    int& ans = dp[x][cnt][h];
    if(vis[x][cnt][h] == kase) return ans;
    vis[x][cnt][h] = kase;
    ans = 0;
    ans = max(ans, DP(x + 1, cnt, h));
    ans = max(ans, DP(x + 1, cnt + 1, h + a[x].second) + a[x].first * 2 * h + a[x].second * a[x].first);
    return ans;
}

bool cmp(const P& a, const P& b){
    return a.second * b.first > b.second * a.first;
}

int main(){
#ifdef NEKO
    freopen("Nya.txt", "r", stdin);
#endif
    ios::sync_with_stdio(false);  cin.tie(0);
    int t; cin >> t;
    while(t--){
        cin >> n >> k;
        for(int i = 1; i <= n; i++) 
            cin >> a[i].first >> a[i].second;
        sort(a + 1, a + 1 + n, cmp);
        memset(dp, 0, sizeof dp);

        cout << "Case " << kase++ << ": " << DP(1, 0, 0) << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值