zoukankan      html  css  js  c++  java
  • POJ1821 Fence

    传送门

    Fence
    Time Limit: 1000MS   Memory Limit: 30000K
         

    Description

    A team of k (1 <= K <= 100) workers should paint a fence which contains N (1 <= N <= 16 000) planks numbered from 1 to N from left to right. Each worker i (1 <= i <= K) should sit in front of the plank Si and he may paint only a compact interval (this means that the planks from the interval should be consecutive). This interval should contain the Si plank. Also a worker should not paint more than Li planks and for each painted plank he should receive Pi $ (1 <= Pi <= 10 000). A plank should be painted by no more than one worker. All the numbers Si should be distinct.

    Being the team's leader you want to determine for each worker the interval that he should paint, knowing that the total income should be maximal. The total income represents the sum of the workers personal income.

    Write a program that determines the total maximal income obtained by the K workers.

    Input

    The input contains:
    Input

    N K
    L1 P1 S1
    L2 P2 S2
    ...
    LK PK SK

    Semnification

    N -the number of the planks; K ? the number of the workers
    Li -the maximal number of planks that can be painted by worker i
    Pi -the sum received by worker i for a painted plank
    Si -the plank in front of which sits the worker i

    Output

    The output contains a single integer, the total maximal income.

    Sample Input

    8 4
    3 2 2
    3 2 3
    3 3 5
    1 1 7 
    

    Sample Output

    17

    Hint

    Explanation of the sample:

    the worker 1 paints the interval [1, 2];

    the worker 2 paints the interval [3, 4];

    the worker 3 paints the interval [5, 7];

    the worker 4 does not paint any plank

    Source

     
    比较简单的单调队列优化dp ,朴素的dp很好想,用dp[i][j] 表示前j个fence由前i个人来喷,那么无非就是三种转移:
    1、第j个不喷 则dp[i][j] = dp[i][j-1]
    2、第i个人不喷,则dp[i][j] = dp[i-1][j]
    3、第i个人喷第j个fence,则dp[i][j] = max{dp[i-1][k] + (j-k)*w[i]} (j-l[i] <= k <= j-1)
    显然对于1、2两个方程,转移都是O(1)的,对于第3个方程,转移是O(n)的。
    对第三个方程简单变形,就有dp[i][j] = max{dp[i-1][k] - k*w[i]} + j*w[i];不难发现红色部分只与k有关。
    由于k存在取值范围,就可以考虑线段树或者单调队列进行优化。显然这题中线段树不如单调队列优秀。
     
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstdlib>
     4 #include <algorithm>
     5 #include <iostream>
     6 using namespace std;
     7 #define N 120
     8 #define M 17000
     9 #define For(i,n) for(int i=1;i<=n;i++)
    10 #define Rep(i,l,r) for(int i=l;i<=r;i++)
    11 
    12 struct People{
    13     int l,p,s;
    14 }a[N];
    15 
    16 int q[M],l[N],r[N],dp[N][17000],n,k;
    17 
    18 bool cmp(People A,People B){
    19     return A.s<B.s;
    20 }
    21 
    22 void init(){
    23     scanf("%d%d",&n,&k);
    24     For(i,k)
    25         scanf("%d%d%d",&a[i].l,&a[i].p,&a[i].s);
    26     sort(a+1,a+1+k,cmp);
    27     For(i,k){
    28         l[i]=max(0,a[i].s-a[i].l);
    29         r[i]=min(n,a[i].s+a[i].l-1);
    30     }
    31 }
    32 
    33 void DP(){
    34     Rep(i,0,n)  dp[0][i]=0;
    35     For(i,k){
    36         Rep(j,0,a[i].s-1) dp[i][j]=dp[i-1][j];
    37         int h=1,t=0;
    38         for(int j=l[i],tmp;j<a[i].s;j++){
    39             tmp=dp[i-1][j]-j*a[i].p;
    40             while(h<=t&&dp[i-1][q[t]]-q[t]*a[i].p<=tmp) t--;
    41             q[++t]=j;
    42         }
    43         for(int j=a[i].s,tmp;j<=r[i];j++){
    44             while(h<=t&&j-q[h]>a[i].l) h++;
    45             dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    46             dp[i][j]=max(dp[i][j],dp[i-1][q[h]]+(j-q[h])*a[i].p);
    47         }
    48         Rep(j,r[i]+1,n) dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
    49     }
    50     int ans=0;
    51     for(int i=1;i<=n;i++) ans=max(ans,dp[k][i]);
    52     printf("%d
    ",ans);
    53 }
    54 
    55 int main()
    56 {
    57     init();
    58     DP();
    59     return 0;
    60 }
  • 相关阅读:
    解决PKIX:unable to find valid certification path to requested target 的问题
    Linux 上的常用文件传输方式介绍与比较
    用VNC远程图形化连接Linux桌面的配置方法
    红帽中出现”This system is not registered with RHN”的解决方案
    linux安装时出现your cpu does not support long mode的解决方法
    CentOS SSH配置
    es6扩展运算符及rest运算符总结
    es6解构赋值总结
    tortoisegit安装、clon、推送
    es6环境搭建
  • 原文地址:https://www.cnblogs.com/zjdx1998/p/3948126.html
Copyright © 2011-2022 走看看