Problem Description
Love in college is a happy thing but always have so many pity boys or girls can not find it.
Now a chance is coming for lots of single boys. The Most beautiful and lovely and intelligent girl in HDU,named Kiki want to choose K single boys to travel Jolmo Lungma. You may ask one girls and K boys is not a interesting thing to K boys. But you may not know Kiki have a lot of friends which all are beautiful girl!!!. Now you must be sure how wonderful things it is if you be choose by Kiki.
Problem is coming, n single boys want to go to travel with Kiki. But Kiki only choose K from them. Kiki every day will choose one single boy, so after K days the choosing will be end. Each boys have a Love value (Li) to Kiki, and also have a other value (Bi), if one boy can not be choose by Kiki his Love value will decrease Bi every day.
Kiki must choose K boys, so she want the total Love value maximum.
Input
The input contains multiple test cases.
First line give the integer n,K (1<=K<=n<=1000)
Second line give n integer Li (Li <= 100000).
Last line give n integer Bi.(Bi<=1000)
Output
Output only one integer about the maximum total Love value Kiki can get by choose K boys.
Sample Input
3 3
10 20 30
4 5 6
4 3
20 30 40 50
2 7 6 5
Sample Output
47
104
题意:在数组a中挑出k个数,第j个挑的数能拿到的贡献是a[i]-(j-1)*b[i],问最大能获得的贡献和 |
思路:
一开始会直观地想直接贪心,把b大的优先挑。但是这样不一定对,比如
1 100 50
1000 999 1
优先挑1就血亏了。
不过,按照b从大到小排序这一点是没错的,优先考虑嘛。但是,我可以不选啊。就比如上述例子中排序好后只挑后两个(k=2)。那么问题就变成了选or不选的背包问题了~
用dp[i][j]表示前i个数中,选到了j个数时的最优解。
那么转移方程很容易写成来,由“不选当前”的前一状态dp[i-1][j]转移过来,或者选了当前的dp[i-1][j-1]+当前贡献转移过来。即
dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + c[i].a - (j-1)*c[i].b);
结果即是dp[n][k](n个数里面挑k个的最优解)。
AC代码:
#include<iostream>
#include<string>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
#include <queue>
#include<sstream>
#include <stack>
#include <set>
#include <bitset>
#include<vector>
#define FAST ios::sync_with_stdio(false)
#define abs(a) ((a)>=0?(a):-(a))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define rep(i,a,n) for(int i=a;i<=n;++i)
#define per(i,n,a) for(int i=n;i>=a;--i)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> PII;
const int maxn = 1e6+200;
const int inf=0x3f3f3f3f;
const double eps = 1e-7;
const double pi=acos(-1.0);
const int mod = 1e9+7;
inline int lowbit(int x){return x&(-x);}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
void ex_gcd(ll a,ll b,ll &d,ll &x,ll &y){if(!b){d=a,x=1,y=0;}else{ex_gcd(b,a%b,d,y,x);y-=x*(a/b);}}//x=(x%(b/d)+(b/d))%(b/d);
inline ll qpow(ll a,ll b,ll MOD=mod){ll res=1;a%=MOD;while(b>0){if(b&1)res=res*a%MOD;a=a*a%MOD;b>>=1;}return res;}
inline ll inv(ll x,ll p){return qpow(x,p-2,p);}
inline ll Jos(ll n,ll k,ll s=1){ll res=0;rep(i,1,n+1) res=(res+k)%i;return (res+s)%n;}
inline ll read(){ ll f = 1; ll x = 0;char ch = getchar();while(ch>'9'||ch<'0') {if(ch=='-') f=-1; ch = getchar();}while(ch>='0'&&ch<='9') x = (x<<3) + (x<<1) + ch - '0', ch = getchar();return x*f; }
int dir[4][2] = { {1,0}, {-1,0},{0,1},{0,-1} };
ll dp[1050][1050];
typedef struct Pos
{
ll a;
ll b;
}P;
P c[1050];
bool cmp(P d, P e)
{
return d.b > e.b;
}
int main()
{
ll n , k;
while(cin>>n>>k)
{
mem(dp,0);
rep(i,1,n) c[i].a = read();
rep(i,1,n) c[i].b = read();
sort(c+1,c+1+n,cmp);
rep(i,1,n) rep(j,1,min(k,i)) dp[i][j] = max(dp[i-1][j], dp[i-1][j-1] + c[i].a - (j-1)*c[i].b);
cout<<dp[n][k]<<endl;
}
return 0;
}