Codeforces Round #508 (Div. 2)

A. Equality

Description:

You are given a string s s s of length n n n, which consists only of the first k k k letters of the Latin alphabet. All letters in string s s s are uppercase.
A subsequence of string s s s is a string that can be derived from s s s by deleting some of its symbols without changing the order of the remaining symbols. For example, “ADE” and “BD” are subsequences of “ABCDE”, but “DEA” is not.
A subsequence of s s s called good if the number of occurences of each of the first k k k letters of the alphabet is the same.
Find the length of the longest good subsequence of s s s.

Input:

The first line of the input contains integers n n n ( 1 n 1 0 5 1\le n \le 10^5 1n105) and k k k ( 1 k 26 1 \le k \le 26 1k26).
The second line of the input contains the string s s s of length n n n. String s s s only contains uppercase letters from ‘A’ to the k k k-th letter of Latin alphabet.

Output

Print the only integer — the length of the longest good subsequence of string s s s.

Sample Input:

9 3
ACAABCCAB

Sample Output:

6

Sample Input:

9 4
ABCABCABC

Sample Output:

0

题目链接

直接统计字母表中前K个字母在字符串中出现的最小次数即可。

AC代码:

#include <bits/stdc++.h>
using namespace std;

const int INF = 0x3f3f3f3f;

int main(int argc, char *argv[]) {
    int N, K;
    scanf("%d%d", &N, &K);
    string str;
    cin >> str;
    map<int, int> Cnt;
    for (int i = 0; i < N; ++i) {
        Cnt[str[i] - 'A']++;
    }
    int Ans = INF;
    for (int i = 0; i < K; ++i) {
        Ans = min(Ans, Cnt[i]);
    }
    printf("%d\n", Ans * K);
    return 0;
}

B. Non-Coprime Partition

Description:

Find out if it is possible to partition the first n n n positive integers into two non-empty disjoint sets S 1 S_1 S1 and S 2 S_2 S2 such that:
g c d ( s u m ( S 1 ) , s u m ( S 2 ) ) &gt; 1 \mathrm{gcd}(\mathrm{sum}(S_1), \mathrm{sum}(S_2)) &gt; 1 gcd(sum(S1),sum(S2))>1 Here s u m ( S ) \mathrm{sum}(S) sum(S) denotes the sum of all elements present in set S S S and g c d \mathrm{gcd} gcd means thegreatest common divisor.
Every integer number from 1 1 1 to n n n should be present in exactly one of S 1 S_1 S1 or S 2 S_2 S2.

Input:

The only line of the input contains a single integer n n n ( 1 n 45 &ThinSpace; 000 1 \le n \le 45\,000 1n45000)

Output

If such partition doesn’t exist, print “No” (quotes for clarity).
Otherwise, print “Yes” (quotes for clarity), followed by two lines, describing S 1 S_1 S1 and S 2 S_2 S2 respectively.
Each set description starts with the set size, followed by the elements of the set in any order. Each set must be non-empty.
If there are multiple possible partitions — print any of them.

Sample Input:

1

Sample Output:

No

Sample Input:

3

Sample Output:

Yes
1 2
2 1 3

题目链接

若n为偶数则必定可以分为奇数组和偶数组,其两组之和的最大公约数为2,若n为奇数有两种情况,1.n是第偶数个奇数,则还是可以分为奇数组与偶数组使其两组之和的最大公约数为2,2.n是第奇数个奇数,通过找规律可以发现1n中的中位数单独分为一组时两组的最大公约数即为1n的中位数。

AC代码:

#include <bits/stdc++.h>
using namespace std;

int main(int argc, char *argv[]) {
    int N;
    scanf("%d", &N);
    if (N <= 2) {
        printf("No\n");
    }
    else {
        printf("Yes\n");
        if (N & 1) {
            int Num = (N - 1) / 2 + 1;
            if (Num & 1) {
                printf("%d %d\n", 1, (N + 1) / 2);
                printf("%d ", N - 1);
                for (int i = 1; i <= N; ++i) {
                    if (i == (N + 1) / 2) {
                        continue;
                    }
                    printf("%d ", i);
                }
                printf("\n");
            }
            else {
                printf("%d ", (N + 1) / 2);
                for (int i = 1; i <= N; i += 2) {
                    printf("%d ", i);
                }
                printf("\n");
                printf("%d ", N / 2);
                for (int i = 2; i <= N; i += 2) {
                    printf("%d ", i);
                }
                printf("\n");
            }
        }
        else {
            printf("%d ", N / 2);
            for (int i = 1; i < N; i += 2) {
                printf("%d ", i);
            }
            printf("\n");
            printf("%d ", N / 2);
            for (int i = 2; i <= N; i += 2) {
                printf("%d ", i);
            }
            printf("\n");
        }
    }
    return 0;
}
全部评论

相关推荐

繁华的街道两旁,湿漉漉的下午,两个青涩的脸庞互相张望。宽大卫衣下娇小的她,向我奔来。不约而同的卫衣,斯文的半框眼镜掩饰着一个穷臭屌丝气息。这是我和我牛爱网第一死忠粉兼专属女嘉宾最初的见面。火速恋爱,但是没有所谓的快节奏,相识半年,还是一样的热恋。吃着肉夹馍坐过西安的小三轮洱海边自行车的气球胖吃着她最喜欢的酸酸水果和小乳扇在南山某店爷爷穿孙子衣服,摸肥猫就算我在忙也要抽出时间陪她去吃他喜欢的漂亮饭生活总是平凡,但平凡不平淡还记得见面第一件事儿:“我去上个厕所。”现在早上第一件事儿:“拉*”第一次上我车的她:“我可以坐副驾吗?”现在的她:“老子把jio翘到上面得得挡到你后视镜。”这小孩,虽然花了我...
Stan_蹒跚者:确很厉害,但是有一个小问题:谁问你了?我的意思是,谁在意?我告诉你,根本没人问你,在我们之中0人问了你,我把所有问你的人都请来 party 了,到场人数是0个人,誰问你了?WHO ASKED?谁问汝矣?誰があなたに聞きましたか?누가 물어봤어?我爬上了珠穆朗玛峰也没找到谁问你了,我刚刚潜入了世界上最大的射电望远镜也没开到那个问你的人的盒,在找到谁问你之前我连癌症的解药都发明了出来,我开了最大距离渲染也没找到谁问你了我活在这个被辐射蹂躏了多年的破碎世界的坟墓里目睹全球核战争把人类文明毁灭也没见到谁问你了
点赞 评论 收藏
分享
用户64975461947315:这不很正常吗,2个月开实习证明,这个薪资也还算合理,深圳Java好多150不包吃不包住呢,而且也提前和你说了没有转正机会,现在贼多牛马公司骗你说毕业转正,你辛辛苦苦干了半年拿到毕业证,后面和你说没hc了😂
点赞 评论 收藏
分享
有没有友友知道hr面会问什么我应该反问什么?还有如何防止hr套话啊?还有应该如果催hr推进快一点#字节#OPPO#hr面
牛客989988346号:职业规划,优缺点,为什么选择这个岗,对应聘公司产品的了解和满意度,如果让你改进公司产品你会怎么做,对ai(新技术)的了解,有无其他offer,什么时候能到岗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务