题目传送门

题解:

需要注意到的是 每个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. java并发之ConcurrentLinkedQueue

    在并发编程中,我们可能经常需要用到线程安全的队列,JDK提供了两种模式的队列:阻塞队列和非阻塞队列.阻塞队列使用锁实现,非阻塞队列使用CAS实现.ConcurrentLinkedQueue是一个基于链 ...

  2. Vue+Typescript中在Vue上挂载axios使用时报错

    Vue+Typescript中在Vue上挂载axios使用时报错 在vue项目开发过程中,为了方便在各个组件中调用axios,我们通常会在入口文件将axios挂载到vue原型身上,如下: main.t ...

  3. 【Java例题】5.3 线性表的使用

    3.线性表的使用.使用ArrayList模拟一个一维整数数组.数据由Random类随机产生.进行对输入的一个整数进行顺序查找.并进行冒泡排序. package chapter6; import jav ...

  4. vue之手把手教你写日历组件

    ---恢复内容开始--- 1.日历组件 1.分析功能:日历基本功能,点击事件改变日期,样式的改变 1.结构分析:html 1.分为上下两个部分 2.上面分为左按钮,中间内容展示,右按钮 下面分为周几展 ...

  5. 第五章-处理多窗口 | Electron实战

    本章主要内容: 使用JavaScript Set数据结构跟踪多个窗口 促进主进程和多个渲染器进程之间的通信 使用Node APIs检查应用程序运行在那个平台上 现在,当Fire Sale启动时,它为U ...

  6. Maven安装配置及其插件m2e(Eclipse Indigo 和 MyEclipse8.5)的安装配置

    Maven安装配置及其插件m2e(Eclipse Indigo 和 MyEclipse8.5)的安装配置   系统:Windows7 使用软件: Maven3.0.3 + Eclipse Indigo ...

  7. python对常见数据类型的遍历

    本文将通过for ... in ...的语法结构,遍历字符串.列表.元组.字典等数据结构. 字符串遍历 >>> a_str = "hello itcast" &g ...

  8. 怎么把PicPick设置成中文版?

    1.首先打开软件 2.在File文件中中点击能看到Program Options这一选项,单击打开 3.右下方有个Language选项,改成简体中文

  9. cs231n---CNN架构

    1 LeNet-5 (1998) 第一个被提出的卷积网络架构,深度较浅,用于手写数字识别. 2 AlexNet (2012) 架构为: CONV1 ->MAX POOL1 ->NORM1 ...

  10. ZooKeeper实现生产-消费者队列

    [欢迎关注公众号:程序猿讲故事 (codestory),及时接收最新文章] 生产-消费者队列,用于多节点的分布式数据结构,生产和消费数据.生产者创建一个数据对象,并放到队列中:消费者从队列中取出一个数 ...