题目传送门

题解:

需要注意到的是 每个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 ;
}

CodeForces 1107 F Vasya and Endless Credits的更多相关文章

  1. CodeForces 1107F. Vasya and Endless Credits

    题目简述:给定 $n \leq 500$ 个贷款方式,其中第$i$个贷款额为$a_i$元,需要$k_i$个月偿还,每月还贷$b_i$元.在每个月月初可申请其中一个贷款,而在每个月月底时需要还贷.求:( ...

  2. Vasya and Endless Credits CodeForces - 1107F (二分图完美匹配)

    大意: n中贷款, 每种只能买一次, 第$i$种给$a_i$元, 要还款$k_i$个月, 每个月底还$b_i$元. 每个月可以在月初申请一种贷. 求某一时刻能得到的最大钱数.

  3. Codeforces 1107 E - Vasya and Binary String

    E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...

  4. CodeForces 1107 - G Vasya and Maximum Profit 线段树

    题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...

  5. Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题

    F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...

  6. Codeforces 959 F. Mahmoud and Ehab and yet another xor task

    \(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...

  7. Codeforces 835 F. Roads in the Kingdom

    \(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...

  8. Codeforces 731 F. Video Cards(前缀和)

    Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...

  9. Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array

    题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...

随机推荐

  1. __int64与long long、long的区别

    首先来看一看int.long.long long的取值范围 int                  所占字节数为:4                   表示范围为:-2147483648~2147 ...

  2. MemCached的工具类。获取cached中的所有key

    package com.ibs.auth.controller; import java.io.UnsupportedEncodingException; import java.util.Date; ...

  3. 非web下的PowerMockito单元测试

    一.介绍 PowerMockito 可以用来 Mock 掉 final 方法(变量).静态方法(变量).私有方法(变量).想要使用 PowerMockito Mock掉这些内容,需要在编写的测试类上使 ...

  4. 深入Apache NiFi 之源码学习

    前言 要问 Hortonworks 这家公司最有产品力的产品是什么,我觉得是 Apache NiFi.去年Cloudera 和 Hortonworks 合并之后,以 Cloudera 为主,两家公司进 ...

  5. Web项目如何做单元测试

    你可能会用单元测试框架,python的unittest.pytest,Java的Junit.testNG等. 那么你会做单元测试么!当然了,这有什么难的? test_demo.py def inc(x ...

  6. 【Java例题】7.6文件题3-文本文件统计

    6.文本文件统计.已有一个文本文件文件,请统计数字.大写字母.小写字母.汉字及其它字符出现的次数:然后将这些次数由大到小写到另一个文件之中.说明:将次数为零的过滤掉排序 package chapter ...

  7. weblogic10.3.6漏洞修改方案

    1.CVE-2018-2628漏洞 CVE-2018-2628漏洞利用的第一步是与weblogic服务器开放在服务端口上的T3服务建立socket连接,可通过控制T3协议的访问来临时阻断攻击行为. W ...

  8. 【转】C++文件读写详解(ofstream,ifstream,fstream)

    转:http://blog.csdn.net/kingstar158/article/details/6859379 摘要:具体用法,上面链接中,文章写的很详细,讲解ofstream,ifstream ...

  9. alluxio源码解析-层次化存储(4)

    层次化存储-特性介绍: https://www.alluxio.org/docs/1.6/cn/Tiered-Storage-on-Alluxio.html 引入分层存储后,Alluxio管理的数据块 ...

  10. 在MAC终端下打开Finder:

    在Terminal中打开Finder: open . 在Finder中打开Terminal: 系统偏好设置 -> 键盘 -> 快捷键 -> 服务,勾选「新建位于文件夹位置的终端窗口」