1. 程式人生 > >CodeForces 1107 F Vasya and Endless Credits

CodeForces 1107 F Vasya and Endless Credits

還需 離開 大小 隨著 int pla += operator inpu

題目傳送門

題解:

需要註意到的是 每個offer都獲益都是會隨著時間的增加而漸少(或不變)。

所以我們可以知道,最多在第n個月的時候這個人會買車離開。

solve1:最優2分圖匹配

我們可以把每個月都和每個offer建邊。

val[i][j]代表的是離開前倒數第i個月獲取了第j個月的offer, 所以邊權就是 max(0ll, a-min(j-1,k)*b).

最後跑一遍km。

所以復雜度是 n^3.

代碼:

技術分享圖片
 1 /*
 2 code by: zstu wxk
 3 time: 2019/02/02
 4 */
 5 #include<bits/stdc++.h>
 6 using namespace
std; 7 #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); 8 #define LL long long 9 #define ULL unsigned LL 10 #define fi first 11 #define se second 12 #define pb push_back 13 #define lson l,m,rt<<1 14 #define rson m+1,r,rt<<1|1 15 #define lch(x) tr[x].son[0] 16
#define rch(x) tr[x].son[1] 17 #define max3(a,b,c) max(a,max(b,c)) 18 #define min3(a,b,c) min(a,min(b,c)) 19 typedef pair<int,int> pll; 20 const int inf = 0x3f3f3f3f; 21 const int _inf = 0xc0c0c0c0; 22 const LL INF = 0x3f3f3f3f3f3f3f3f; 23 const LL _INF = 0xc0c0c0c0c0c0c0c0; 24 const LL mod = (int)1e9+7; 25 const
int N = 510; 26 int n; 27 LL val[N][N]; 28 LL lx[N], ly[N], slack[N]; 29 int linky[N]; 30 LL pre[N]; 31 bool vis[N], visx[N], visy[N]; 32 void bfs(int k){ 33 LL px, py = 0,yy = 0, d; 34 memset(pre, 0, sizeof(LL) * (n+2)); 35 memset(slack, inf, sizeof(LL) * (n+2)); 36 linky[py]=k; 37 do{ 38 px = linky[py],d = INF, vis[py] = 1; 39 for(int i = 1; i <= n; i++) 40 if(!vis[i]){ 41 if(slack[i] > lx[px] + ly[i] - val[px][i]) 42 slack[i] = lx[px] + ly[i] -val[px][i], pre[i]=py; 43 if(slack[i]<d) d=slack[i],yy=i; 44 } 45 for(int i = 0; i <= n; i++) 46 if(vis[i]) lx[linky[i]] -= d, ly[i] += d; 47 else slack[i] -= d; 48 py = yy; 49 }while(linky[py]); 50 while(py) linky[py] = linky[pre[py]] , py=pre[py]; 51 } 52 void KM(){ 53 memset(lx, 0, sizeof lx); 54 memset(ly, 0, sizeof ly); 55 memset(linky, 0, sizeof(int)*(n+2)); 56 for(int i = 1; i <= n; i++) 57 memset(vis, 0, sizeof(bool)*(n+2)), bfs(i); 58 } 59 void input(){ 60 scanf("%d", &n); 61 LL a, b, k; 62 for(int i = 1; i <= n; ++i){ 63 scanf("%I64d%I64d%I64d", &a, &b, &k); 64 for(LL j = 0; j < n; ++j){ 65 val[i][j+1] = max(0ll, a-min(j,k)*b); 66 } 67 } 68 } 69 int main(){ 70 input(); 71 KM(); 72 LL ans = 0; 73 for(int i = 1; i <= n; ++i) 74 ans += lx[i] + ly[i]; 75 printf("%lld\n", ans); 76 return 0; 77 }
View Code

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+7;
const int N = 550;
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 = 1; i <= n; ++i)
        scanf("%I64d%I64d%I64d", &A[i].a, &A[i].b, &A[i].k);
    sort(A+1, A+1+n);
    for(int i = 1; i <= n; ++i){
        for(int j = 0; j <= n; ++j){
            dp[i][j] = dp[i-1][j] + max(0ll, A[i].a - A[i].k*A[i].b);
            if(j) dp[i][j] = max(dp[i][j], dp[i-1][j-1] + max(0ll, A[i].a - (j-1)*A[i].b));
        }
    }
    LL ans = 0;
    for(int i = 0; i <= n; ++i)
        ans = max(ans, dp[n][i]);
    printf("%I64d\n", ans);
}
int main(){
    while(~scanf("%d", &n)){
        Ac();
    }
    return 0;
}
View Code

CodeForces 1107 F Vasya and Endless Credits