zoukankan      html  css  js  c++  java
  • 【刷题】【dp】 Make The Fence Great Again

    二维dp

    %%%%大佬,最近没啥时间写博客了,以后补上

    https://www.cnblogs.com/bianjunting/p/11556876.html

    结果最后tle靠快读卡过去了

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    using namespace std;
    #define ll long long
    int n;
    const int N=3e5+10;
    ll f[N][3];
    
    inline ll read()
    {
        ll x=0;char c=getchar();
        while(c< '0' || c> '9' ) c=getchar();
        while(c>='0' && c<='9' ) x=(x<<3)+(x<<1)+c-'0',c=getchar();
        return x;
    }
    
    int main()
    {
        int q;q=read();
        while(q--)
        {
            n=read();
            ll pre,now=read(),v=read(); 
            f[1][1]=v,f[1][2]=(v<<1);
            
            for(int i=2;i<=n;i++)
            {
                pre=now;
                now=read(),v=read();
                f[i][0]=f[i][1]=f[i][2]=-1;
                
                for(int j=0;j<3;j++)
                    for(int k=0;k<3;k++)
                    {
                        if((pre+j) != (now+k) )
                            if(f[i][k]==-1 ) f[i][k]=f[i-1][j]+v*k;
                            else f[i][k]=min(f[i][k],f[i-1][j]+v*k);
                    }
            }
            
            cout<<min(f[n][0],min(f[n][1],f[n][2] ) )<<endl;
        }
        
        return 0;
    }
    View Code
  • 相关阅读:
    CodeForces-1263D Secret Passwords 并查集 求连通分量
    Virtual Friends HDU
    AreYouBusy HDU
    Jack Straws POJ
    Divisibility by 25 CodeForces
    逃离迷宫 HDU
    Find a way HDU
    Stall Reservations POJ
    Three displays CodeForces
    Radar Installation POJ
  • 原文地址:https://www.cnblogs.com/xwww666666/p/15480435.html
Copyright © 2011-2022 走看看