12.5

发布时间 2023-12-05 21:07:20作者: 电棍otto

孔子:因为我说过的话都会被大家当做道理,所以我是说的道理。

今天学习了单调队列优化 DP。

股票交易

给定 \(T\) 天内的股票情况,求收益最大。

此题朴素 DP 复杂度似乎是 \(O(n^4)\),可以优化成 \(O(n^3)\)但这有什么用,显然需要单调队列优化。

动态转移方程
(不买不卖):\(f[i][j]=f[i−1][j]\)
(买入):$ f[i][j]=max ∑^{p<i}_{p=1}∑ ^{q<j} _ {q=0}f[p][q]−(j−q)×AP[i]$
(卖出): $f[i][j]=max ∑ ^{p<i} _{p=1}∑ ^{q>j} _{q=0}f[p][q]+(q−j)×BP[i] $

可以用单调队列维护两次,分别维护 \(AP[i]\)\(BP[i]\)

部分代码
for(int i=1;i<=n;++i)
    {
        for(int j=0;j<=maxp;++j)
            dp[i][j]=max(dp[i-1][j],dp[i][j]);
            if(i<=w+1) continue;
            int pre=i-w-1;
            head=tail=0;
            for(int j=0;j<=maxp;++j)
            {
                int mon=dp[pre][j]+j*a[i];
                while(head<tail && q[tail-1].f<mon)
                    tail--;
                q[tail].f=mon;q[tail++].noww=j;
                while(head<tail && q[head].noww+as[i]<j)
                    head++;
                dp[i][j]=max(dp[i][j],q[head].f-j*a[i]);
            }
            head=tail=0;
            for(int j=maxp;j>=0;--j)
            {
                int mon=dp[pre][j]+j*b[i];
                while(head<tail && q[tail-1].f<mon)
                    tail--;
                q[tail].f=mon;q[tail++].noww=j;
                while(head<tail && q[head].noww-bs[i]>j)
                    head++;
                dp[i][j]=max(dp[i][j],q[head].f-j*b[i]);
            }
    }

瑰丽华尔兹

好(sb)题。

还是单调队列优化,但是要找东南西北四个方向的最大值并且维护 \(ans\)

code
#include <bits/stdc++.h>
using namespace std;
const int N=1e6;
const int fx[5]={0,-1,1,0,0},fy[5]={0,0,0,-1,1};
#define inf 0x7ffffff
char ch[2001][2001];
int dp[210][210];
int n,m,x,y,k,ans;
struct node
{
    int s,t,d;
}sh[N];
struct Node
{
    int x,y,val;
}q[N];
int head=1,tail;
inline int get_dis(int ax,int ay,int bx,int by)
{
    return abs(ax-bx)+abs(ay-by);
}
void find_ans(int x,int y,int opt,int len)
{
    head=1,tail=0;
    while(x>=1 && x<=n && y>=1 && y<=m)
    {
        if(ch[x][y]=='x')
            head=1,tail=0;
        else 
        {
            while(head<=tail && q[tail].val+get_dis(x,y,q[tail].x,q[tail].y)<dp[x][y])
                tail--;
            q[++tail]=((Node){x,y,dp[x][y]});
            while(head<=tail && max(abs(x-q[head].x),abs(y-q[head].y))>len)        
                head++;
            dp[x][y]=max(dp[x][y],q[head].val+get_dis(x,y,q[head].x,q[head].y));
            ans=max(ans,dp[x][y]);
        }
        x+=fx[opt];
        y+=fy[opt];
    }
}
signed main()
{
    cin>>n>>m>>x>>y>>k;
    for(int i=1;i<=n;++i)
       scanf("%s",ch[i]+1);
    memset(dp,-0x3f,sizeof(dp));
    dp[x][y]=0;
    for(register int i=1;i<=k;++i)
    {
        cin>>sh[i].s>>sh[i].t>>sh[i].d;
        int len=sh[i].t-sh[i].s+1;
        if(sh[i].d==1)
            for(register int j=1;j<=m;++j)
                find_ans(n,j,sh[i].d,len);
        else if(sh[i].d==2)
            for(register int j=1;j<=m;++j)
                find_ans(1,j,sh[i].d,len);
        else if(sh[i].d==3)
            for(register int j=1;j<=n;++j)
                find_ans(j,m,sh[i].d,len);
        else if(sh[i].d==4)
            for(register int j=1;j<=n;++j)
                find_ans(j,1,sh[i].d,len);
    }
        cout<<ans;
}

今天旁边两位拍代码,拍了好几个小时还没拍出来呢,加油。

推歌:Lose Control -Hedley

Can I make a little toast

Can we get a little close

Can I get an amen

Can I get a hell yeah

Can I get a holy ghost

Somebody give me a beat

Let me see you on your feet

Somebody save your man

I don't give a god damn I wanna be a freak

Catch me if you can

I don't think you understand where we're going

Where we're going

When you feel it and you know

When this sh*t's about to blow

And it hits you

Ooh it hits you like

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

Like a shot to the face

Now you got a little taste

Baby never give a fuck

Get up

Stand up

Shake your buns to the bass

When you break it down

They're gonna love the sound

Everybody just stop

And bring it back around like

Catch me if you can

I don't think you understand where we're going

Where we're going

When you feel it and you know

When this sh*t's about to blow

And it hits you

Ooh it hits you like

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

Oh my god I lose control

Oh my god I lose control

Catch me if you can

I don't think you understand where we're going

When you feel it and you know

When this sh*t's about to blow

When it hits you

Hits you like

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

In my body

In my bones

Drop the beat and free my soul

When I hear that rock and roll

Oh my god I lose control

La la la la oh oh oh

La la la la oh oh oh

I had to let it go

Oh my god I lose control

Oh my god I lose control

Oh my god I lose control

非常好洗脑神曲,但是千万别看BGA。
怎么是2021年发布的,可是我2022年就已经在听了。