题目链接: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;
}