CodeForces 1107 F Vasya and Endless Credits
题解:
需要注意到的是 每个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的更多相关文章
- CodeForces 1107F. Vasya and Endless Credits
题目简述:给定 $n \leq 500$ 个贷款方式,其中第$i$个贷款额为$a_i$元,需要$k_i$个月偿还,每月还贷$b_i$元.在每个月月初可申请其中一个贷款,而在每个月月底时需要还贷.求:( ...
- Vasya and Endless Credits CodeForces - 1107F (二分图完美匹配)
大意: n中贷款, 每种只能买一次, 第$i$种给$a_i$元, 要还款$k_i$个月, 每个月底还$b_i$元. 每个月可以在月初申请一种贷. 求某一时刻能得到的最大钱数.
- Codeforces 1107 E - Vasya and Binary String
E - Vasya and Binary String 思路:区间dp + 记忆化搜索 转移方程看上一篇博客. 代码: #pragma GCC optimize(2) #pragma GCC opti ...
- CodeForces 1107 - G Vasya and Maximum Profit 线段树
题目传送门 题解: 枚举 r 的位置. 线段树每个叶子节点存的是对应的位置到当前位置的价值. 每次往右边移动一个r的话,那么改变的信息有2个信息: 1. sum(a-ci) 2.gap(l, r) 对 ...
- 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 ], 这样 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array
题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...
随机推荐
- Android Studio 'AIDL is missing' 且 不识别R文件
最近刚开始用Android Studio,出的问题还真不少.昨天不知为何不能新建项目了,这两天重装了几次才搞定. 可又出了这个问题: 原因:Compile Sdk Version和Build Tool ...
- 什么是redis的缓存雪崩与缓存穿透
今天来分享一下Redis几道常见的面试题: 如何解决缓存雪崩? 如何解决缓存穿透? 如何保证缓存与数据库双写时一致的问题? 一.缓存雪崩 1.1 什么是缓存雪崩? 首先我们先来回答一下我们为什么要用缓 ...
- 探秘最小生成树&&洛谷P2126题解
我在这里就讲两种方法 Prim 和 Kruscal Kruscal kruscal的本质其实是 排序+并查集 ,是生成树中避圈法的推广 算法原理如下 (1)将连通带权图G=<n,m>的各条 ...
- .net core web api部署到Linux系统CentOS 7
一.创建一个.net core web api 的Demo 完成后的项目结构如图 修改下监听端口 发布代码 二.发布到CentOS 7上并运行 下一步需要一定的虚拟机知识了,我这里使用了windows ...
- 使用Jasypt对SpringBoot配置文件加密
# **前言** 在日前安全形势越来越严重的情况下,让我意识到在项目中存在一个我们经常忽略的漏洞,那就是我们的项目的配置文件中配置信息的安全,尤其是数据库连接的用户名和密码的安全.所以这里我们就需要对 ...
- JAVA基础知识(九)Java 异常
Throwable是Error和Exception的基类 Exception(异常) :是程序本身可以处理的异常. Error(错误): 是程序无法处理的错误.这些错误表示故障发生于虚拟机自身.或者发 ...
- n的阶乘 -牛客
题目描述 输入一个整数n,输出n的阶乘(每组测试用例可能包含多组数据,请注意处理) 输入描述: 一个整数n(1<=n<=20) 输出描述: n的阶乘 解题思路 采用递归求解,也可以使用循环 ...
- CSS:抗锯齿 font-smoothing
本文引自:http://www.cnblogs.com/sunshq/p/4595673.html -webkit-font-smoothing 这个属性可以使页面上的字体抗锯齿,使用后字体看起来会更 ...
- LeetCode——540. Single Element in a Sorted Array
题目:Given a sorted array consisting of only integers where every element appears twice except for one ...
- Flink 源码解析 —— JobManager 处理 SubmitJob 的过程
JobManager 处理 SubmitJob https://t.zsxq.com/3JQJMzZ 博客 1.Flink 从0到1学习 -- Apache Flink 介绍 2.Flink 从0到1 ...