一、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 问题描述
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. 最大的