小工具知识网

当前位置: 首页 > 行业知识 > 算法日志篇 week2

行业知识

算法日志篇 week2

2026-10-19 00:40:43 来源:互联网转载

A - Swap and Delete (CodeForces - 1913B)

You are given a binary string s(a string consisting only of 0-s and 1-s).

You can perform two types of operations on s:

1.delete one character from s. This operation costs 1 coin;

2.swap any pair of characters in s. This operation is free (costs 0 coins).

You can perform these operations any number of times and in any order.

Let’s name a string you’ve got after performing operations above as t. The string t is good if for each i from 1 to |t| ti≠si (|t| is the length of the string t). The empty string is always good. Note that you are comparing the resulting string t with the initial string s.

What is the minimum total cost to make the string t good?

题解

此题的中文大致意思是给出一个字符串,然后用两种方式来生成另一个字符串,新字符串t要和旧的字符串s前几位上的每一位都相反(0-1或1-0),第一种方式为删掉原字符串中的某几位,第二种方式为交换原字符串中的某几位。

此题的解法为:先统计出原字符串内有几个‘0’与‘1’,进行存储,随后对字符串进行遍历,包含如下三种情况:

1.原字符串大小为一,此时直接输出1就行。ps:删去

2.原字符串大小已经为 num_0 = num_1此时直接输出0即可,因为不需要进行删除操作,只需要交换就可以达到目的。

3.遍历原字符串,遇1减num_0,遇0减num_1,直到出现负值,此时输出原字符串位数-已经遍历的个数,则为需要删去的字符数量。

代码如下:

#includeusing namespace std;void solve(){    long long n;    cin >> n;    while(n --){        string s;        cin >> s;        long long num_0 = count(s.begin(),s.end(),'0');        long long num_1 = count(s.begin(),s.end(),'1');        long long n0 = num_0;        long long n1 = num_1;        if(s.size() == 1)cout             cout             for(long long i = 0;i                 if(n0  s[i];cj *= s[i];}if(cj == 1 || cj == 7 || cj == 17 || cj == 119 || cj == 289 || cj == 2023){cout cout cout coutcoutcoutcoutcoutcoutcoutcoutfor(int i=0;icoutcoutint t;cint;while(t--){solve();}return 0;}    int a, b;} nd[N];bool st[N];int last[N];void solve() {    memset(st, 0, sizeof st);    int n;    cin  n;    for (int i = 0; i > a >> b;    int last = 0;    for(int i = 0; i > x;        f -= min((LL)1 * b, (LL)1 * a * (x - last));        last = x;    }    cout  0 ? "YES" : "NO")     int x;    cin > x;    while(x --){        solve();    }    return 0;}

Q - Not Quite Latin Square (CodeForces - 1915B)

题解:

直接一边输入一边遍历,遍历九宫格,缺少的字符一定是少于3的,根据这个思想即可快速解题。

#includeusing namespace std;typedef long long LL;int n, m;void solve() {    char m[3][3];    int a = 0,b = 0,c = 0;    char t;    for(int i = 0;i > t;            if(t == 'A')a ++;            else if(t == 'B')b ++;            else if(t == 'C')c ++;        }    }    if(a  n;for(int i = 0 ; i > x;sum += x;}LL t = sqrt(sum);for(LL i = t - 1 ; i if(i  s;cout  1)cout if (s[i + 1] == 'a' || s[i + 1] == 'e' || s[i + 1] == 'u' || s[i + 1] == 'i' || s[i + 1] == 'o'){cout cout     int x;    cin  x;    while(x --){        solve();    }    return 0;}

上一篇:深圳个人房屋出租备案

下一篇:冰封王座为什么下不了?