zoukankan      html  css  js  c++  java
  • codeforces 632B B. Alice, Bob, Two Teams(暴力)

    B. Alice, Bob, Two Teams
    time limit per test
    1.5 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Alice and Bob are playing a game. The game involves splitting up game pieces into two teams. There are n pieces, and the i-th piece has a strength pi.

    The way to split up game pieces is split into several steps:

    1. First, Alice will split the pieces into two different groups A and B. This can be seen as writing the assignment of teams of a piece in an n character string, where each character is A or B.
    2. Bob will then choose an arbitrary prefix or suffix of the string, and flip each character in that suffix (i.e. change A to B and B to A). He can do this step at most once.
    3. Alice will get all the pieces marked A and Bob will get all the pieces marked B.

    The strength of a player is then the sum of strengths of the pieces in the group.

    Given Alice's initial split into two teams, help Bob determine an optimal strategy. Return the maximum strength he can achieve.

    Input

    The first line contains integer n (1 ≤ n ≤ 5·105) — the number of game pieces.

    The second line contains n integers pi (1 ≤ pi ≤ 109) — the strength of the i-th piece.

    The third line contains n characters A or B — the assignment of teams after the first step (after Alice's step).

    Output

    Print the only integer a — the maximum strength Bob can achieve.

    Examples
    input
    5
    1 2 3 4 5
    ABABA
    output
    11
    input
    5
    1 2 3 4 5
    AAAAA
    output
    15
    input
    1
    1
    B
    output
    1
    Note

    In the first sample Bob should flip the suffix of length one.

    In the second sample Bob should flip the prefix or the suffix (here it is the same) of length 5.

    In the third sample Bob should do nothing.

    题意:最多改变一次,改变为A变为B,B变为A,且为前缀或者后缀,问B的最大的和是多少;

    思路:找出所有的前缀和后缀的A的和和B的和,暴力找最大值;

    AC代码:

    #include <bits/stdc++.h>
    using namespace std;
    const int N=1e5+3;
    long long p[5*N],pa[5*N],pb[5*N],sa[5*N],sb[5*N];
    int n;
    char str[5*N];
    int main()
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            cin>>p[i];
        }
        scanf("%s",str+1);
        pa[0]=0;
        pb[0]=0;
        for(int i=1;i<=n;i++)
        {
            if(str[i]=='A')
            {
                pa[i]=pa[i-1]+p[i];
                pb[i]=pb[i-1];
            }
            else
            {
                pa[i]=pa[i-1];
                pb[i]=pb[i-1]+p[i];
            }
        }
        sa[n+1]=0;
        sb[n+1]=0;

    for(int i=n;i>0;i--)
        {
            if(str[i]=='A')
            {
                sa[i]=sa[i+1]+p[i];
                sb[i]=sb[i+1];
            }
            else
            {
                sa[i]=sa[i+1];
                sb[i]=sb[i+1]+p[i];
            }
        }
        long long ans=0,x=0;
        for(int i=0;i<=n;i++)
        {
            x=max(sa[i+1],sb[i+1]);
            ans=max(ans,x+pb[i]);
        }
        for(int i=n+1;i>0;i--)
        {
            x=max(pa[i-1],pb[i-1]);
            ans=max(ans,x+sb[i]);
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    Java Image Processing
    贝塞尔曲线开发的艺术
    Ubuntu中Hadoop环境搭建
    FIRST集合、FOLLOW集合、SELECT集合以及预测分析表地构造
    Linux环境下使用VSCode编译makefile文件的注意事项
    神经记忆模型
    深度学习推荐阅读的论文
    博客园无法发布文章解决办法
    计算机各个方向名校公开课
    软件过程基础
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5236173.html
Copyright © 2011-2022 走看看