zoukankan      html  css  js  c++  java
  • poj3616 Milking Time(状态转移方程,类似LIS)

    https://vjudge.net/problem/POJ-3616

    猛刷简单dp的第一天第二题。

    这道题乍一看跟背包很像,不同的在于它是一个区间,背包是定点,试了很久想往背包上套,都没成功。

    这题的思路感觉有点陌生,又有点类似于求最长不降子序列的题。

    dp[i]为到第i个区间为止(该区间肯定有i)的最大挤奶量,最后从m个里面取最大。

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<queue>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 #include<stack>
     8 #define lson l, m, rt<<1
     9 #define rson m+1, r, rt<<1|1
    10 #define INF 0x3f3f3f3f
    11 typedef unsigned long long ll;
    12 using namespace std;
    13 struct Node{
    14     int from, to, w;
    15 }node[100010];
    16 int dp[100010];
    17 bool cmp(const Node a, const Node b)
    18 {
    19     return a.to < b.to;
    20 }
    21 int main()
    22 {
    23     int n, m, r;
    24     cin >> n >> m >> r;
    25     for(int i = 0; i < m; i++){
    26         cin >> node[i].from >> node[i].to >> node[i].w;
    27     }
    28     memset(dp, 0, sizeof(dp));
    29     sort(node, node+m, cmp);//按结束时间升序 
    30     for(int i = 0; i < m; i++){
    31         dp[i] = node[i].w;
    32         for(int j = 0; j < i; j++){
    33             if(node[i].from >= node[j].to+r){
    34                 dp[i] = max(dp[i], dp[j]+node[i].w);//人人为我 
    35             }
    36         } 
    37     }
    38     int maxm = -INF;
    39     for(int i = 0; i < m; i++){
    40         maxm = max(maxm, dp[i]);
    41     }
    42     cout << maxm << endl;
    43     return 0;
    44 }
  • 相关阅读:
    AcWing 199. 余数之和
    AcWing 295. 清理班次
    AcWing 294. 计算重复
    Acwing 393. 雇佣收银员
    AcWing 362. 区间
    AcWing 361. 观光奶牛
    CSP-S 2019 Emiya 家今天的饭
    CSP-S 2019游记
    AcWing 345. 牛站 Cow Relays
    java 环境配置
  • 原文地址:https://www.cnblogs.com/Surprisezang/p/9026899.html
Copyright © 2011-2022 走看看