URAL 2024 Adventure Time 思维题、Interesting
来源:互联网 发布:温什么知什么的成语 编辑:程序博客网 时间:2024/05/18 01:21
A - Adventure Time
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64uDescription
‘Here’s for you!’ shouted Finn fighting the Shadow Guardians. ‘Jake! Collect the Darkness Rocks and let’s run from here!’
Jake would have been glad to finish as soon as possible, because it was wet and sad in the Dangerous Cave, but it was not so easy to
gather Rocks. So he called BMO and asked him to help with choosing an optimal set of Rocks. Jake told BMO that there were n Rocks
in the Cave, and they were numbered with different integers from 1 to n. Each Rock was painted one of 26 colors. Jake also reminded BMO of Princess Bubblegum’s warning: there should be at most k Rocks of pairwise different colors among the Rocks taken from
the Cave. Only a set of Rocks with this property is safe. If one takes an unsafe set of Rocks from the Cave, Cosmic Evil will be awakened! Of course, Jake doesn’t want to awaken the Evil, but he wants to take as many Darkness Rocks from the Cave as possible.
Help BMO to find the size of the largest safe set of Rocks and the number of different safe sets of this size. Two sets of Rocks are different if one of them contains a Rock with a number that is not contained in the other set.
Input
The first line contains n lowercase English letters (1 ≤ n ≤ 10 5); each letter denotes the color of a Rock. In the second line you are
given the integer k (1 ≤ k ≤ 26).
Output
Output two integers separated with a space: the size of the largest safe set of Darkness Rocks and the number of different safe sets of
this size.
Sample Input
abcde1
1 5
ababac2
5 1
Source
UESTC 2016 Summer Training #17 Div.2
URAL 2024
My Solution
统计每个字母出现的次数, 然后从大到小排序
最大的k的总个数就是 he size of the largest safe set of Darkness Rocks
然后1)如果color[k-1] != 0, 则 统计这k个里的 color[i] == color[k-1] 记为cnt1, 并统计所有的里面的color[i] == color[k-1] 记为 cnt2, 然后C[cnt2][cnt1]就是the number of different safe sets of this size.了
2)否则直接就是答案
复杂度 O(n)
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef long long LL;const int maxn = 1e5 + 8;char val[maxn];LL color[26], C[maxn][30];inline bool cmp(LL a, LL b){ return a > b;}int main(){ memset(C, 0, sizeof C); for(int i = 0; i <= 26; i++){ C[i][0] = 1; for(int j = 1; j <= i; j++){ C[i][j] = C[i-1][j-1] + C[i-1][j]; } } #ifdef LOCAL freopen("a.txt", "r", stdin); //freopen("b.txt", "w", stdout); int T = 3; while(T--){ #endif // LOCAL memset(color, 0, sizeof color); LL sz = 0, num = 1, k; scanf("%s", val); scanf("%I64d", &k); int len = strlen(val); for(int i = 0; i < len; i++) color[val[i] - 'a']++; sort(color, color + 26, cmp); int cnt1 = 0, cnt2 = 0; for(int i = 0; i < k; i++){ sz += color[i]; if(color[k-1] !=0 && color[i] == color[k-1]) cnt1++; } for(int i = 0; i < 26; i++){ if(color[k-1] != 0 && color[i] == color[k-1]) cnt2++; } if(color[k-1] != 0){ num = C[cnt2][cnt1]; } printf("%I64d %I64d", sz, num); #ifdef LOCAL printf("\n"); } #endif // LOCAL return 0;}
Thank you!
------from ProLights
0 0
- URAL 2024 Adventure Time 思维题、Interesting
- PROBLEM_A: URAL 2024 Adventure Time
- Adventure Time URAL
- ural 1820. Ural Steaks -思维题
- URAL 1385 Interesting Number
- URAL 1385 Interesting Number
- ural 2070 - Interesting Numbers
- URAL 2070 Interesting Numbers
- Ural 2070 Interesting Numbers
- Interesting Numbers URAL
- Scarily interesting! (URAL
- A - Adventure Time --阶乘/排列组合
- URAL 2070 Interesting Numbers(数学)
- UVALive 5963 Confusion in the Problem Set 思维题、Interesting
- Gym 100541 B. Sum 分块的技巧、思维题、Interesting
- HDU 6016 Count the Sheep(思维题)(Interesting)
- ural 2025. Line Fighting -思维题
- URAL 1385 Interesting Number 解题报告
- Redis笔记三之Java操作Redis
- hdu 1281 棋盘游戏 二分图匹配 匈牙利算法 解题报告
- django学习笔记一
- 使用Android Studio开发NDK
- InnoDB与MyISAM的六大区别(转)
- URAL 2024 Adventure Time 思维题、Interesting
- Java容器四:Hashtable
- 七牛的存储算法猜测
- myisam和innodb索引实现的不同
- JDBC的基本操作
- java PropertyChangeSupport类详解
- error C2143: 语法错误 : 缺少“;”(在“类型”的前面)
- 老大语录
- 全文索引快的原因以及contains与like查找区别比较