26_5_18 Educational Codeforces Round 190 (Rated for Div. 2)
Educational Codeforces Round 190 (Rated for Div. 2)
A. Optimal Purchase
(implementation)(math)
Ques
题意
You have a group of
- Individual key: costs
dollars and gives access to one student. - Group key: costs
dollars and gives access to a group of up to three students inclusive.
A group key can also be used for fewer students (one or two), and its price does not change.
Your task is to determine the minimum amount of money needed to provide access to the online course for all
Input
The first line contains one integer
Each test case consists of one line containing three integers
Output
For each test case, output one integer — the minimum amount of money needed to provide access to the online course for all
Example
1 | 9 |
1 | 45 |
Note
In the first example, you can buy
In the second example, you can buy
In the third example, you can buy
In the fourth example, you can buy
Ans
初始思路
刚开始想分类讨论,但是洗完澡脑子有点发懵,讨论不出来,于是想出了一种比较直接的方法,就是直接统计三个人、两个人、一个人的最小值,然后再累加
1 | void solve(){ |
顺利AC了
题解
思路很直接:比较购买3把个人钥匙的钱 3a 和 购买1把团体钥匙的钱 b
如果b >= 3a,团体购买不划算,全部购买个人钥匙即可
否则,钥匙覆盖3人时更便宜
剩下0/1/2人时再比较买个人钥匙和团体钥匙哪个更便宜
现在开始推导公式
q = n / 3
r = n % 3
如果 b >= 3a:res = n * a
否则:res = q b + min(ra,b)
于是有:
1 | void solve(){ |
B. Digit String
(greedy)(implementation)(math)
Ques
题意
You are given a string
Let’s say that the string is beautiful if it is impossible to select some of its elements and write them out (in the same order as they appear in the string) to form a number that is a multiple of
Your task is to calculate the minimum possible number of elements in the string
Input
The first line contains a single integer
The only line of each test case contains a string
Additional constraint on the input: the sum of the lengths of
Output
For each test case, print a single integer — the minimum possible number of elements in the string
Example
1 | 5 |
1 | 1 |
Note
In the first example, you have to delete the whole string.
In the second example, the string is already beautiful.
In the third example, you can delete the
Ans
初始思路
刚开始想到了一种思路,先删除4,这样剩下的就是12 3了。而对于接下来的“不可能从字符串中选取一些元素并将它们写出来以组成一个 4 的倍数”,我们可以以2为关注点,如果2的前面存在1或3,那肯定是不成立的。那一切都很明了了,如果存在2,那要保证2的前面不能有1或3。但是,WA2五次了也没调出来
题解
出发点完全正确。
核心结论:一个数是否是4的倍数,只看最后一位或最后两位。
字符串由1~4组成:
- 单个数字4本身就是4的倍数,所以美丽串内不能有4
- 两位数中,因为4必须删掉,所以只需考虑剩下的1 2 3,而也就是不能出现12、32
即,最终保留下来的字符串中,不能有1或3出现在某个2的前面
所以美丽串可以看成“若干个2+若干个1/3”,而我们的目标也变成:一个分界点,分界点左边保留所有2,右边保留所有1和3,使保留数量最大
最终res = n - 最多保留数
接下来记left2为左界能保留的2的数量,right3为右界能保留的1/3的数量,res为当前能保留多少字符
1 | void solve(){ |
C. Arrange the Numbers in a Circle
(implementation)(math)
Ques
题意
You have cards with numbers:
You must take at least three cards from the ones you have and arrange them in a circle so that the following condition holds:
- in every triple of consecutive cards, there are at least two equal cards.
Formally, if the numbers on the chosen cards are $a0, a_1, \dots, a{k-1}$ in the order they are arranged in a circle, then the following condition must hold:
- for every
from to , there are at least two equal numbers among $ai, a{(i+1) \bmod k}, a_{(i+2) \bmod k}$.
What is the maximum number of cards you can arrange?
Input
The first line contains one integer
Each test case consists of two lines:
- the first line contains one integer
( ); - the second line contains
integers ( ).
Additional constraint on the input: the sum of
Output
For each test case, output one integer — the maximum number of cards you can arrange. If it is impossible to choose three or more cards and arrange them in a circle so that the condition holds, output
Example
1 | 11 |
1 | 4 |
Note
In the first example, you can choose and arrange the cards as follows:
In the second example, you can arrange all the cards:
In the third example, you can choose and arrange the cards as follows:
Ans
初始思路
B题卡半天,做这道题只有二十来分钟了,最后遗憾WA2
这题思路还是比较好理解的,不断画图模拟模拟
我是记录了c为1的次数,出现c为2的次数,c为3的次数和c大于等于4的次数
题解
我们的目标是:摆成一个圆,避免连续三个位置全不同
出现次数 >= 2的数字都可以作为大块
比如 11 222 3333
因为每个块里至少有两个相同的数字,所以跨块检查时不会出现连续三个全不同
因此,所有
的牌可以全上 出现次数 = 1的数字需要被夹住
只出现一次的数字必须放在两个相同的数字之间
只有一种大块时
例如 1 1 1 1 1 1 1 1 7
则此时可以摆成 A x A A y A A z A,即7张A最多能夹
有至少两个大块时
例如 1 1 1 1 3 4
先拿所有大块:AAA BBBB
而若要往里面塞单牌,则要B B X B B,即这个块左右两侧都要留出至少两个相同数字来保护边界
所以此时,一个大小为c的大块,最多能夹
张单牌 因此,
总结:
1 | bigCnt == 0 -> res = 0 |
于是
1 | void solve(){ |

