2021-09-24每天刷题打卡

一、POJ-1821:Fence

1.1 问题描述

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.

题目大意:

求工人总工资的最大值,每个工人从Si块木开始上漆,但不能涂超过Li块,且只能涂连续的木板,每个工人涂一块木板就给他Pi美元。

1.2 问题解决

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
using namespace std;
int dp[110][16000+50];
 
struct person
{
    int l,p,s;
}P[110];
int cmp(person p1,person p2)
{
    return p1.s<p2.s;
}
 
int Q[16000+50];
int main()
{
    int N,K;
    while(scanf("%d%d",&N,&K)!=EOF)
    {
        for(int i=1;i<=K;i++)
            scanf("%d%d%d",&P[i].l,&P[i].p,&P[i].s);
        sort(P+1,P+K+1,cmp);
 
        int front,rear;
        memset(dp,0,sizeof(dp));
        int ans=0;
        for(int i=1;i<=K;i++)
        {
            front=0;rear=1;
            Q[0]=max(P[i].s-P[i].l,0);
            for(int j=1;j<=N;j++)
            {
                dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
                if(j>=P[i].s+P[i].l)
                    continue;
                while(front<rear&&Q[front]+P[i].l<j)
                    front++;
                if(j<P[i].s)
                {
                    int temp=dp[i-1][j]-j*P[i].p;
                    while(front<rear&&dp[i-1][Q[rear-1]]-Q[rear-1]*P[i].p<temp)
                        rear--;
                    Q[rear++]=j;
                    continue;
                }
                dp[i][j]=max(dp[i][j],dp[i-1][Q[front]]+P[i].p*(j-Q[front]));
            }
        }
        printf("%d\n",dp[K][N]);
    }
    return 0;
}

二、Leetcode-203:移除链表元素

2.1 问题描述

img

2.2 问题解决

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeElements(ListNode head, int val) {
        ListNode headnode = new ListNode();
        headnode.next = head;
        ListNode cur = headnode;

       while(cur.next != null)
       {       
           if(cur.next.val == val)
                cur.next = cur.next.next;
           else
                cur = cur.next;
       }

       return headnode.next; 
    }
}

三、生词

fence n. 栅栏

plank n. 厚木板

compact adj. 紧凑的

consecutive adj 连续的

distinct adj. 截然不同的

maximal adj. 最大的

四、参考文章

  1. POJ1821-Fence
  2. poj 1821(单调队列优化DP)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值