【#247_DIV2】-A B C

本文详细解析了Codeforces平台上的三个不同难度级别的题目,包括解题思路、代码实现和核心算法应用,旨在帮助读者提升算法思维和编程能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/contest/431

解题报告:

A - Black Square

一道神经病的题。。不知道为何水到如此地步。。。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn =1e5+50;

char str[maxn];
int a[10];

int main()
{
    cin>>a[1]>>a[2]>>a[3]>>a[4];
    scanf("%s",str);
    int i,ans=0;
    for(i=0;i<strlen(str);i++)  ans+= a[str[i]-'0'];
    cout<<ans<<endl;
    return 0;
}

B -  Shower Line

直接DFS搜出所有排列,找最大值即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
typedef long long ll;
using namespace std;

int g[6][6],que[6],vis[6];
int ans;

int cal()
{
     return g[que[0]][que[1]]+g[que[1]][que[0]] + g[que[2]][que[3]]+g[que[3]][que[2]] + g[que[1]][que[2]]+g[que[2]][que[1]]
        + g[que[3]][que[4]]+g[que[4]][que[3]] + g[que[2]][que[3]]+g[que[3]][que[2]] + g[que[3]][que[4]]+g[que[4]][que[3]];
}

void dfs(int dep)
{
    int i;
    if(dep==5)
    {
        int tans = cal();
        ans = max(ans,tans);
        //for(i=0;i<5;i++)cout<<que[i]<<" "; cout<<endl;
        return ;
    }
    for(i=0;i<5;i++)
    {
        if(!vis[i])
        {
            vis[i] = 1;
            que[dep] = i;
            dfs(dep+1);
            vis[i] = 0;
        }
    }
}

int main()
{
    freopen("input.txt","r",stdin);
    memset(g,0,sizeof(g));
    memset(vis,0,sizeof(vis));
    int i,j;
    for(i=0;i<5;i++)
        for(j=0;j<5;j++) cin>>g[i][j];
    ans=0;
    dfs(0);
    cout<<ans<<endl;
    return 0;
}

C -  k-Tree

一道DP题,没做出来。

以后切记!不会的题大胆猜DP!

dp[ n ][ 0 ] 表示权值和为 n 且路径中没有 >= d 的边的最多路径数。

dp[ n ][ 1 ] 表示权值和为 n 且路径中有 >= d 的边的最多路径数。

输入 n ,k ,d 之后,先把能一步到的 n 给赋上值。

dp[ 1~(d-1) ][ 0 ] = 1

dp[ d~k ][ 1 ] = 1

接下来,多于一步的路径由以下公式推出:

dp[ i ][ 0 ] = dp[ i -1 ][ 0 ] + dp[ i -2 ][ 0 ] + .... + dp[ i - d + 1 ][ 0 ]

dp[ i ][ 1 ] = dp[ i -1 ][ 1 ] + dp[ i -2 ][ 1 ] + .... + dp[ i - k ][ 1 ] + dp[ i -d ][ 0 ] + dp[ i - d - 1 ][ 0 ] + .... + dp[ i - k ][ 0 ]

然后每一步都要取模,取模。。取模。。。。真是烦啊,像我代码里那样就行了

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MOD = 1e9+7;

int mol(int x){
    return x%MOD;
}

int dp[120][2];

int main()
{
    //freopen("input.txt","r",stdin);
    memset(dp,0,sizeof(dp));
    int n,k,d,i,j;
    cin>>n>>k>>d;
    for(i=1;i<d;i++) dp[i][0]=1;
    for(i=d;i<=k;i++) dp[i][1]=1;
    for(i=1;i<=n;i++)
    {
        for(j=i-1; j>=0 && j>=i-d+1; j--) {
            dp[i][0] = mol(mol(dp[i][0]) + mol(dp[j][0]));
        }
        for(j=i-1; j>=0 && j>=i-k; j--) {
            dp[i][1] = mol(mol(dp[i][1]) + mol(dp[j][1]));
        }
        for(j=i-d; j>=0 && j>=i-k; j--) {
            dp[i][1] = mol(mol(dp[i][1]) + mol(dp[j][0]));
        }
        //cout<<i<<": "<<dp[i][0]<<" "<<dp[i][1]<<endl;
    }
    cout<<dp[n][1]<<endl;
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值