codeforces D. Make The Fence Great Again B The Number of Products

//思路:二维DP,因为木板的高度只需与相邻俩边不同,所以只需枚举0,1,2的状态即可(俺是SB)
#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
const int maxn = 3e5 + 32;
int a[maxn],b[maxn];
i64 dp[maxn][3];
int main()
{
    ios::sync_with_stdio(false);    cin.tie(0),cout.tie(0);
    int T,n;  cin>>T;
    while(T--)
    {
        cin>>n;
        for(int i=1;i<=n;++i)
        {
            cin>>a[i]>>b[i];
            dp[i][0] = dp[i][1] = dp[i][2] = 0x3f3f3f3f3f3f3f3f;
        }
        dp[1][0] = 0,dp[1][1] = b[1],dp[1][2] = 2*b[1];
        for(int i=2;i<=n;++i)
        {
            for(int j=0;j!=3;++j)
            {// i - 1的花费
                for(int k=0;k!=3;++k)
                {
                    if(a[i-1]+j != a[i] + k)
                    {
                        dp[i][k] = min(dp[i][k],dp[i-1][j] + k*b[i]);
                    }
                }
            }
        }
        cout<<min(min(dp[n][0],dp[n][1]),dp[n][2])<<'\n';
    }
}

  https://codeforces.com/contest/1221/problem/D

#include<bits/stdc++.h>
using namespace std;
typedef long long i64;
const int maxn = 2e5 + 32;
int dp[maxn][2];//0正 1负
int main()
{
    ios::sync_with_stdio(false); cin.tie(0),cout.tie(0);
    int n,value;  cin>>n;
    i64 pNum = 0,nNum = 0;
    for(int i=1;i<=n;++i)
    {
        cin>>value;
        if(value > 0)
        {
            dp[i][0] = dp[i-1][0] + 1;
            dp[i][1] = dp[i-1][1];
        }else{
            dp[i][0] = dp[i-1][1];
            dp[i][1] = dp[i-1][0] + 1;
        }
        pNum += dp[i][0];
        nNum += dp[i][1];
    }
    cout<<nNum<<" "<<pNum<<'\n';
}

  https://codeforces.com/problemset/problem/1215/B

posted on 2019-10-05 15:56  chengyulala  阅读(134)  评论(0编辑  收藏  举报

导航