codeforces 935e Fafa and Ancient Mathematics

本文详细解析了Codeforces竞赛中的一道难题E题,通过将问题转化为树形DP问题,利用二叉树结构和动态规划算法求解字符串中合理安排+-使表达式值最大的策略。代码实现展示了完整的算法流程。

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

链接: http://codeforces.com/contest/935/problem/E

题意: 现在给你一个字符串保证里边所有的数字都在0到9 范围内,并且每一个?对应着一对括号,一共有n+m 个?,并且其中的n为+ m个为 - ,你要合理的安排 + -  使得式子的值最大。 

思路: 每个问号对应着一对括号,那么我们就可以把整个字符串看成一棵二叉树,每个叶子节点为 0 到 9 每个非叶子节点为 ? ,那么就转化成一个  树形dp 的问题了。dp[ i ] [ j ][2] 分别表示节点 i 的加号或者减号为j 的最小最大值。因为这里的+ -  只保证一个<= 100,一开始就给,,。。。 搞错了。

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int,int > pii;
const int inf =10000000;

struct node
{
    int ls,rs;
}tr[10005];

char s[10005];
int id[10005];
ll dp[10005][105][2];
map<pii,int >mp;
stack<int >st;
stack<int >ss;
int n,m;
int len;
int tot;
int op;

void init()
{
    tot=0;
    for(int i=1;i<=len;i++)
    {
        if(s[i]>='0'&&s[i]<='9') continue;
        else if(s[i]=='?'){
            id[i]=++tot;
            ss.push(i);
        }
        else if(s[i]=='(')
        {
            st.push(i);
        }
        else if(s[i]==')'){
            int l=st.top();
            st.pop();
            mp[pii(l,i)]=ss.top();
            ss.pop();
        }
    }
    int cnt=0;
    for(int i=1;i<=len;i++){
        if(s[i]==')'||s[i]=='(') continue;
        cnt++;
    }

    for(int i=0;i<=cnt+2;i++) tr[i].ls=tr[i].rs=-1;
    for(int i=0;i<=cnt+3;i++){
        for(int j=0;j<=101;j++){
            dp[i][j][0]=inf;
            dp[i][j][1]=-inf;
        }
    }

}

int tree(int l,int r)
{
    if(l==r){
        ++tot;
        dp[tot][0][0]=s[l]-'0';
        dp[tot][0][1]=s[l]-'0';
        return tot;
    }
    int pos=mp[pii(l,r)];
    int idd=id[pos];
    int ls=tree(l+1,pos-1);
    int rs=tree(pos+1,r-1);
    tr[idd].ls=ls;
    tr[idd].rs=rs;
    return idd;
}

void dfs(int id)
{
	if(tr[id].ls==-1) return ;

    int ls=tr[id].ls;
    int rs=tr[id].rs;
    dfs(ls); dfs(rs);
    for(int i=0;i<=100;i++){
        for(int j=0;j<=100;j++){
        	if(i+j>100) break;
        	if(op==0)
        	{
        		dp[id][i+j][0]=min(dp[id][i+j][0],dp[ls][i][0]-dp[rs][j][1]);
                dp[id][i+j][1]=max(dp[id][i+j][1],dp[ls][i][1]-dp[rs][j][0]);
                if(i+j+1>n) continue;
                dp[id][i+j+1][0]=min(dp[id][i+j+1][0],dp[ls][i][0]+dp[rs][j][0]);
                dp[id][i+j+1][1]=max(dp[id][i+j+1][1],dp[ls][i][1]+dp[rs][j][1]);
			}
			else{
				dp[id][i+j][0]=min(dp[id][i+j][0],dp[ls][i][0]+dp[rs][j][0]);
                dp[id][i+j][1]=max(dp[id][i+j][1],dp[ls][i][1]+dp[rs][j][1]);
                if(i+j+1>n) continue;
                dp[id][i+j+1][0]=min(dp[id][i+j+1][0],dp[ls][i][0]-dp[rs][j][1]);
                dp[id][i+j+1][1]=max(dp[id][i+j+1][1],dp[ls][i][1]-dp[rs][j][0]);
			}
        }
    }
}

int main()
{
    scanf("%s",s+1);
    scanf("%d %d",&n,&m);
    if(n>100) op=1;
    
    len=strlen(s+1);
    init();
    int rt=tree(1,len);
    dfs(rt);
    
	ll ans;
	if(op==0) ans=dp[rt][n][1];
	else ans=dp[rt][m][1];
    printf("%lld\n",ans);
    return 0;
}

/*

((1?(5?7))?((6?2)?7))
2 3

*/

 

### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值