练习cf2072BB. Having Been a Treasurer in the Past, I Help Goblins Deceive

题目如下
B. Having Been a Treasurer in the Past, I Help Goblins Deceive
time limit per test2 seconds
memory limit per test256 megabytes
After completing the first quest, Akito left the starting cave. After a while, he stumbled upon a goblin village.

Since Akito had nowhere to live, he wanted to find out the price of housing. It is well known that goblins write numbers as a string of characters '-' and '', and the value represented by the string 𝑠 is the number of distinct subsequences∗ of the string 𝑠 that are equal to the string "--" (this is very similar to goblin faces).

For example, the string 𝑠="----" represents the number 6, as it has 6 subsequences "-_-":

𝑠1+𝑠2+𝑠3
𝑠1+𝑠2+𝑠4
𝑠1+𝑠2+𝑠6
𝑠1+𝑠5+𝑠6
𝑠3+𝑠5+𝑠6
𝑠4+𝑠5+𝑠6
Initially, the goblins wrote a random string-number 𝑠 in response to Akito's question, but then they realized that they wanted to take as much gold as possible from the traveler. To do this, they ask you to rearrange the characters in the string 𝑠 so that the value of the number represented by the string 𝑠 is maximized.

∗A subsequence of a string 𝑎 is a string 𝑏 that can be obtained by deleting several (possibly 0) characters from 𝑎. Subsequences are considered different if they are obtained by deleting different sets of indices.
Input
The first line contains the number 𝑡 (1≤𝑡≤104) — the number of test cases.

In the first line of each test case, there is one number 𝑛 (1≤𝑛≤2⋅105) — the length of the string written by the goblins.

In the second line of each test case, there is a string 𝑠 of length 𝑛, consisting only of characters '-' and '_' — the string written by the goblins.

It is guaranteed that the sum of 𝑛 across all test cases does not exceed 2⋅105.

Output
For each test case, you need to output one number — the maximum number of subsequences equal to the string "-_-", if the characters in the string 𝑠 are optimally rearranged.

题目大意
现在有一串由“-”和“"组成的字符串,可以改变原字符串顺序的前提上,以嘴“"为分隔,眼睛“-”按照左右分配,每个构成“-_-"的子序列,怎么分配能使得这样的子序列最多最大?

题目分析
根据“--"的组成,““左边的一律算左眼,右边是右眼,要获得可以构成最多“--"子序列的串,只要把眼睛平均分配到左右,左右眼数目接近时乘积最大,构成的子序列也最多
先计算“
“嘴的数量,再分配两边

点击查看代码
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main(){
    int t;
    cin >> t;
    while(t--){
        int n;
        scanf("%d", &n);
        string s;
        cin >> s;
        long long mouth = count(s.begin(), s.end(), '_');
        long long eye = n - mouth;
        long long left = eye / 2;
        long long right = eye - left;
        long long total = mouth * left * right;
        printf("%llu\n", total);
    }
    return 0;
}
posted @ 2025-07-14 20:56  sirro1uta  阅读(9)  评论(0)    收藏  举报