期望得分:100+100+30=230

实际得分:

 

2017 清北济南考前刷题Day 6 afternoon

正解:

枚举最高的位,这一位m是1但实际用了0

然后剩余的低位肯定是 正数就用1,负数用0

考场思路:数位DP

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

using namespace std;

typedef long long LL;

#define N 100001

int a[N];

char s[N];

LL dp[N][2];

void read(int &x)
{
    x=0; int f=1; char c=getchar();
    while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); }
    while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); }
    x*=f;
}

LL dfs(int dep,int num,bool lim)
{
    if(!dep) return 0;
    if(!lim && dp[dep][num]!=-1) return dp[dep][num];
    int up= lim ? s[dep]-'0' : 1; LL res=0;
    res=dfs(dep-1,0,lim && 0==s[dep]-'0');
    if(up) res=max(res,a[dep]+dfs(dep-1,1,lim && 1==s[dep]-'0'));
    if(!lim) dp[dep][num]=res;
    return res;
}

int main()
{
    freopen("maximum.in","r",stdin);
    freopen("maximum.out","w",stdout);
    int n;
    read(n);
    for(int i=1;i<=n;i++) read(a[i]);
    scanf("%s",s+1);
    memset(dp,-1,sizeof(dp));
    cout<<dfs(n,0,1);
}
View Code

相关文章:

  • 2022-02-24
  • 2022-01-10
  • 2021-11-13
  • 2022-01-29
  • 2021-10-03
  • 2021-11-05
  • 2021-12-15
  • 2021-10-12
猜你喜欢
  • 2021-07-13
  • 2021-11-09
  • 2022-02-20
  • 2021-09-23
  • 2021-08-13
  • 2021-11-11
  • 2021-10-27
相关资源
相似解决方案