Codeforces 2B - The least round way

本文介绍了一种解决2B-Theleastroundway问题的方法,通过动态规划计算矩阵中每个位置2因子和5因子的最小数量,以找到路径上0的最大数量。文章详细解释了状态定义、初始及目标状态,并给出了代码实现。

2B - The least round way

思路:

dp。

先算出每个数2因子的个数,和5因子的个数

因为要出现0那么要1个2乘1个5,那么最后的答案是min(2的个数,5的个数)

所以我们可以分开考虑,先算出使2最小的方案,再算出使5最小的方案,然后再取最小就可以了。

注意特判一种情况,如果经过一次0,那么答案只有1个0,再和上面那种情况比较一下,取个最小

mp[i][j][0]表示(i,j)这个位置2因子的个数

mp[i][j][1]表示(i,j)这个位置5因子的个数

状态:dp[i][j][0]表示到(i,j)这个位置2的最小个数

         dp[i][j][1]表示到(i,j)这个位置5的最小个数

初始状态:dp[1][1][0]=mp[1][1][0]

                dp[1][1][1]=mp[1][1][1]

目标状态:dp[n][n][0],dp[n][n][1]

状态转移:dp[i][j][s]=min(dp[i-1][j][s]+mp[i][j][s],dp[i][j-1][s]+mp[i][j][s])

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long 
#define pb push_back
#define mkp make_pair
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))

const int N=1e3+5;
int dp[N][N][2],mp[N][N][2];
pii pre[N][N][2];
void dfs(int x,int y,int s){
    if(x==1&&y==1)return ;
    dfs(pre[x][y][s].first,pre[x][y][s].second,s);
    if(pre[x][y][s].first==x-1)cout<<'D';
    else cout<<'R';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,T,ix,iy;
    cin>>n;
    bool f=false;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>T;
            if(T==0){
                mp[i][j][0]=mp[i][j][1]=1;
                ix=i,iy=j;
                f=true;
                continue;
            }
            int t=0,tt=0;
            while(T%2==0){
                T/=2;
                t++;
            } 
            while(T%5==0){
                T/=5;
                tt++;
            }
            mp[i][j][0]=t;
            mp[i][j][1]=tt;
        }
    }
    mem(dp,0x3f);
    for(int i=0;i<2;i++){
        dp[1][1][i]=mp[1][1][i];
        for(int j=1;j<=n;j++){
            for(int k=1;k<=n;k++){
                if(j==1&&k==1)continue;
                if(dp[j-1][k][i]+mp[j][k][i]<dp[j][k][i]){
                    dp[j][k][i]=dp[j-1][k][i]+mp[j][k][i];
                    pre[j][k][i]=mkp(j-1,k);
                }
                if(dp[j][k-1][i]+mp[j][k][i]<dp[j][k][i]){
                    dp[j][k][i]=dp[j][k-1][i]+mp[j][k][i];
                    pre[j][k][i]=mkp(j,k-1);
                }
            }
        }
    }
    //cout<<dp[n][n][0]<<' '<<dp[n][n][1]<<endl; 
    int s,ans;
    if(dp[n][n][0]<dp[n][n][1])ans=dp[n][n][0],s=0;
    else ans=dp[n][n][1],s=1;
    if(f){
        if(ans>1){
            cout<<1<<endl;
            for(int i=0;i<ix-1;i++)cout<<'D';
            for(int i=0;i<n-1;i++)cout<<'R';
            for(int i=ix-1;i<n-1;i++)cout<<'D';
        }
        else{
            cout<<ans<<endl;
            dfs(n,n,s);
        } 
    }
    else{
        cout<<ans<<endl;
        dfs(n,n,s);
    }
    
    return 0; 
}

 

转载于:https://www.cnblogs.com/widsom/p/8418502.html

### Codeforces Problem 976C Solution in Python For solving problem 976C on Codeforces using Python, efficiency becomes a critical factor due to strict time limits aimed at distinguishing between efficient and less efficient solutions[^1]. Given these constraints, it is advisable to focus on optimizing algorithms and choosing appropriate data structures. The provided code snippet offers insight into handling string manipulation problems efficiently by customizing comparison logic for sorting elements based on specific criteria[^2]. However, for addressing problem 976C specifically, which involves determining the winner ('A' or 'B') based on frequency counts within given inputs, one can adapt similar principles of optimization but tailored towards counting occurrences directly as shown below: ```python from collections import Counter def determine_winner(): for _ in range(int(input())): count_map = Counter(input().strip()) result = "A" if count_map['A'] > count_map['B'] else "B" print(result) determine_winner() ``` This approach leverages `Counter` from Python’s built-in `collections` module to quickly tally up instances of 'A' versus 'B'. By iterating over multiple test cases through a loop defined by user input, this method ensures that comparisons are made accurately while maintaining performance standards required under tight computational resources[^3]. To further enhance execution speed when working with Python, consider submitting codes via platforms like PyPy instead of traditional interpreters whenever possible since they offer better runtime efficiencies especially important during competitive programming contests where milliseconds matter significantly.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值