当前位置:网站首页>Poj1821 fence problem solving Report
Poj1821 fence problem solving Report
2022-07-07 10:58:00 【Aurora_ yxy】
1 Title Description
A team of $k (1 <= K <= 100) $workers should paint a fence which contains N ( 1 < = N < = 16000 ) N (1 <= N <= 16 000) N(1<=N<=16000) planks numbered from 1 1 1 to N N N from left to right. Each worker i ( 1 < = i < = K ) i (1 <= i <= K) i(1<=i<=K) should sit in front of the plank S i S_i 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 P i ( 1 < = P i < = 10000 ) P_i (1 <= P_i <= 10 000) Pi(1<=Pi<=10000). 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.
Yes N N N The planks line up from left to right , Yes K K K A craftsman painted the boards , Each board shall be painted at most once . The first i i i A craftsman either doesn't paint , Either the whitewash contains wood i i i Of , Length not exceeding L i L_i Li A continuous piece of wood , Each piece of painted wood can get $P_i $ The reward for .
Ask how to arrange to make the craftsmen get the maximum total remuneration .
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.
Examples
The sample input 1
8 4
3 2 2
3 2 3
3 3 5
1 1 7
Sample output 1
17
Tips
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
2 Ideas
2.1 Violence without optimization
First of all, all the craftsmen should follow Si Sort from small to large
• state :f[i][j], front i A craftsman before painting j A board ( Allow null ) The maximum reward for .
• State transition equation :
The first i i i A craftsman does nothing : f [ i ] [ j ] = f [ i − 1 ] [ j ] f[i][j]=f[i-1][j] f[i][j]=f[i−1][j]
The first j j j A piece of wood can be left empty without painting : f [ i ] [ j ] = f [ i ] [ j − 1 ] f[i][j]=f[i][j-1] f[i][j]=f[i][j−1]
Consider the $ i $ A craftsman painted the first k + 1 j k+1~j k+1 j block ,
𝑓[𝑖][𝑗] =𝑚𝑎𝑥{𝑓[𝑖 − 1][𝑘] + 𝑃𝑖 ∗ (𝑗 − 𝑘)}, among 𝑗−𝐿𝑖≤𝑘≤𝑆𝑖−1,𝑗 ≥ 𝑆𝑖
enumeration i i i, j j j, k k k, The time complexity is O ( n 2 k ) O(n^2k) O(n2k)
Because we can see that the data is not so small : 1 < = K < = 100 1 <= K <= 100 1<=K<=100, 1 < = N < = 16000 1 <= N <= 16 000 1<=N<=16000
Obviously not
So , We consider optimizing with priority queues
2.2 Priority queue optimization dp
Optimize :𝑓[𝑖][𝑗] =𝑚𝑎𝑥{𝑓[𝑖 − 1][𝑘] + 𝑃𝑖 ∗ (𝑗 − 𝑘)}, among 𝑗 ≥ 𝑆𝑖,𝑗−𝐿𝑖≤𝑘≤𝑆𝑖−1
• Loop the outermost layer i Regarded as fixed value , Consider inner circulation j And decision making k , Convert to get :
• 𝑓[𝑖][𝑗] = 𝑃𝑖 ∗ 𝑗 +𝑚𝑎𝑥{𝑓[𝑖 − 1][𝑘] − 𝑃𝑖 ∗ 𝑘)}, among 𝑗−𝐿𝑖≤𝑘≤𝑆𝑖−1,𝑗 ≥ 𝑆𝑖
Maintain a decision point k Monotone increasing , The number f [ i − 1 ] [ k ] − P i × k f[i-1][k]-Pi×k f[i−1][k]−Pi×k Monotonic decline Queues ,
Only the decision in this queue can be the best decision at a certain moment
• Monotonic queue support :
When j When it gets bigger , Inspection team leader , Will be less than j-Li Out of the team .
When querying the optimal decision , The team leader is the maximum value
When new decisions join the team , Check the tail of the team f [ i − 1 ] [ k ] − P i × k f[i-1][k]-Pi×k f[i−1][k]−Pi×k The monotonicity of , Useless decisions come out of the team , new
Make a decision to join the team
2 Concrete realization
- For each outer loop i,
When the inner loop starts ( j = S i j=S_i j=Si), Create an empty monotone queue ,
The interval [ m a x ( j − L i , 0 ) , S i − 1 ] [max(j-Li,0),Si-1] [max(j−Li,0),Si−1] The decision is added to the monotone queue , perform 3
- For each of these j = S i N j=Si~N j=Si N,
First, check whether the team leader is legal (1), Then take the team leader as the optimal strategy ( operation 2) Make a state transition , Then join the team (3)
The total time complexity is zero O(NK)
code
#include<bits/stdc++.h>
using namespace std;
const int N=2e4+2,K=1e2+2;
struct node{
int l,s,p;
}a[K];
bool cmp(node x,node y){
return x.s<y.s;
}
int f[K][N],q[N],n,k;
int main(){
while(~scanf("%d%d",&n,&k)){
for(int i=1;i<=k;i++) {
scanf("%d%d%d",&a[i].l,&a[i].p,&a[i].s);
}
sort(a+1,a+k+1,cmp);
memset(f,0,sizeof(f));
for(int i=1;i<=k;i++){
int hd=0,tl=0;
q[tl++]=max(0,a[i].s-a[i].l);
for(int j=1;j<=n;j++){
f[i][j]=max(f[i-1][j],f[i][j-1]);
if(j>=a[i].s+a[i].l) continue;
while(hd<tl&&q[hd]+a[i].l<j) hd++;
if(j>=a[i].s) f[i][j]=max(f[i][j],f[i-1][q[hd]]+a[i].p*(j-q[hd]));
else{
int x=f[i-1][j]-j*a[i].p;
while(hd<tl&&f[i-1][q[tl-1]]-q[tl-1]*a[i].p<x) tl--;
q[tl++]=j;
}
}
}
printf("%d\n",f[k][n]);
}
return 0;
}
End of the flower *
·*,°*:.*( ̄▽ ̄)/$:.°* .
边栏推荐
- Unity websocket client
- ArrayList thread insecurity and Solutions
- 请问申购新股哪个证券公司开户是最好最安全的
- [machine learning 03] Lagrange multiplier method
- China Southern Airlines pa3.1
- seata 1.3.0 四種模式解决分布式事務(AT、TCC、SAGA、XA)
- Unity script visualization about layout code
- 无法打开内核设备“\\.\VMCIDev\VMX”: 操作成功完成。是否在安装 VMware Workstation 后重新引导? 模块“DevicePowerOn”启动失败。 未能启动虚拟机。
- Unity websocket server
- Static semantic check of clang tidy in cicd
猜你喜欢

2021 summary and 2022 outlook

想考中级软考,一般需要多少复习时间?

Network engineer test questions and answers in May of the first half of 2022

Arduino board description

ArrayList thread insecurity and Solutions
![[OneNote] can't connect to the network and can't sync the problem](/img/28/9a02b1da0f43889989a9539c9fb6b6.png)
[OneNote] can't connect to the network and can't sync the problem

Deeply understand the characteristics of database transaction isolation

Mysql的json格式查询

枪出惊龙,众“锁”周之

Multithreaded asynchronous orchestration
随机推荐
2022年上半年5月网络工程师试题及答案
Those confusing concepts (3): function and class
Mpx 插件
[OneNote] can't connect to the network and can't sync the problem
"Dream Cup" 2017 Jiangsu information and future primary school summer camp it expert PK program design questions
高级软考(网络规划设计师)该如何备考?
Socket communication principle and Practice
How to get hardware information in unity
JS实现链式调用
Bookmarking - common website navigation for programmers
路由器开发知识汇总
POJ1821 Fence 题解报告
Applet jump to H5, configure business domain name experience tutorial
Using tansformer to segment three-dimensional abdominal multiple organs -- actual battle of unetr
2022年7月10日“五心公益”活动通知+报名入口(二维码)
Deep understanding of Apache Hudi asynchronous indexing mechanism
[machine learning 03] Lagrange multiplier method
MONAI版本更新到 0.9 啦,看看有什么新功能
SQL Server knowledge gathering 9: modifying data
【亲测可行】error while loading shared libraries的解决方案