首页 技术 正文
技术 2022年11月6日
0 收藏 988 点赞 668 浏览 2985 个字

题目传送门

题解:

需要注意到的是 每个offer都获益都是会随着时间的增加而渐少(或不变)。

所以我们可以知道,最多在第n个月的时候这个人会买车离开。

solve1:最优2分图匹配

我们可以把每个月都和每个offer建边。

val[i][j]代表的是离开前倒数第i个月获取了第j个月的offer, 所以边权就是 max(0ll, a-min(j-1,k)*b).

最后跑一遍km。

所以复杂度是 n^3.

代码:

 /*
code by: zstu wxk
time: 2019/02/02
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = ;
int n;
LL val[N][N];
LL lx[N], ly[N], slack[N];
int linky[N];
LL pre[N];
bool vis[N], visx[N], visy[N];
void bfs(int k){
LL px, py = ,yy = , d;
memset(pre, , sizeof(LL) * (n+));
memset(slack, inf, sizeof(LL) * (n+));
linky[py]=k;
do{
px = linky[py],d = INF, vis[py] = ;
for(int i = ; i <= n; i++)
if(!vis[i]){
if(slack[i] > lx[px] + ly[i] - val[px][i])
slack[i] = lx[px] + ly[i] -val[px][i], pre[i]=py;
if(slack[i]<d) d=slack[i],yy=i;
}
for(int i = ; i <= n; i++)
if(vis[i]) lx[linky[i]] -= d, ly[i] += d;
else slack[i] -= d;
py = yy;
}while(linky[py]);
while(py) linky[py] = linky[pre[py]] , py=pre[py];
}
void KM(){
memset(lx, , sizeof lx);
memset(ly, , sizeof ly);
memset(linky, , sizeof(int)*(n+));
for(int i = ; i <= n; i++)
memset(vis, , sizeof(bool)*(n+)), bfs(i);
}
void input(){
scanf("%d", &n);
LL a, b, k;
for(int i = ; i <= n; ++i){
scanf("%I64d%I64d%I64d", &a, &b, &k);
for(LL j = ; j < n; ++j){
val[i][j+] = max(0ll, a-min(j,k)*b);
}
}
}
int main(){
input();
KM();
LL ans = ;
for(int i = ; i <= n; ++i)
ans += lx[i] + ly[i];
printf("%lld\n", ans);
return ;
}

solve2:DP

首先需要明白一点,就是如果所有offer 还款的期限还没有到的话,那么肯定是bi越大的offer越后拿更优。

所以把所有的offer按bi大小sort一下,大的排在前面。

dp[i][j] 代表的是 处理到第i个物品, 倒数第j个月的花费是多少。

所以,最显然的转移方式就是 dp[i][j] = max(dp[i][j], dp[i-1][j-1] + max(0ll, A[i].a – (j-1)*A[i].b)).

还需要明白的一点就是,如果bi越大,理论上是放在越后拿越好,但是到如果完全还完贷款的那种offer,是越早拿越好,可以让别的offer少还一个月的贷款。

所以又存在另一种转移方式 dp[i][j] = dp[i-1][j] + max(0ll, A[i].a – A[i].k*A[i].b);

代码:

/*
code by: zstu wxk
time: 2019/02/02
*/
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod = (int)1e9+;
const int N = ;
struct Node{
LL a, b, k;
bool operator<(const Node & x) const{
return b > x.b;
}
}A[N];
int n;
LL dp[N][N];
void Ac(){
for(int i = ; i <= n; ++i)
scanf("%I64d%I64d%I64d", &A[i].a, &A[i].b, &A[i].k);
sort(A+, A++n);
for(int i = ; i <= n; ++i){
for(int j = ; j <= n; ++j){
dp[i][j] = dp[i-][j] + max(0ll, A[i].a - A[i].k*A[i].b);
if(j) dp[i][j] = max(dp[i][j], dp[i-][j-] + max(0ll, A[i].a - (j-)*A[i].b));
}
}
LL ans = ;
for(int i = ; i <= n; ++i)
ans = max(ans, dp[n][i]);
printf("%I64d\n", ans);
}
int main(){
while(~scanf("%d", &n)){
Ac();
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,085
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,560
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,409
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,182
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,819
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,902