000 — CSEC CPD KICKOFF Contest — (DIV 1) — Editorial

Revision en43, by porcif, 2024-10-17 16:15:47

A. Garland

Understand the Problem
Tutorial
Code

B. Detective Book

Understand the Problem
Tutorial
Code

C. Points on Plane

Understand the problem
Tutorial

Code1

def solve(): n = int(input())

def check(k):
    return (k + 1) ** 2 >= n



l = 0  
r = 10**9
ans = float("inf")

while l <= r:
    mid = (l + r) // 2
    if check(mid):
        ans = min(mid,ans)
        r = mid - 1
    else:
        l = mid + 1 

print(ans)

for _ in range(int(input())): solve()


<spoiler summary="Code2">

import math

def solve(): n = int(input())

ans = math.isqrt(n-1)
print(ans)

for _ in range(int(input())): solve()

</spoiler>




### [D. Good String](https://codeforces.net/gym/558367/problem/D)

<spoiler summary="Tutorial">
Let's analyze when the string is good. Suppose it is t1t2…tk
.

The cyclic shifts of this string are tkt1t2…tk−1
 and t2t3…tkt1
. We get the following constraints for a good string: tk=t2
, t1=t3
, t2=t4
, ..., tk−2=tk
, tk−1=t1
. If the string has odd length, then all characters should be equal to each other; otherwise, all characters on odd positions should be equal, and all characters on even positions should be equal.

Now, since there are only 10
 different types of characters, we can brute force all possible combinations of the first and the second character of the string we want to obtain (there are only 100
 of them) and, for each combination, greedily construct the longest possible subsequence of s
 beginning with those characters in O(n)
.</spoiler>

<spoiler summary="Code">

def solve(s, x, y): res = 0 for c in s: if int(c) == x: res += 1 x, y = y, x if x != y and res % 2 == 1: res -= 1 return res

for _ in range(int(input())): s = input() ans = 0 for x in range(10): for y in range(10): ans = max(ans, solve(s, x, y)) print(len(s) — ans) ```

E. Playlist

Problem

The problem is about choosing up to k songs from a playlist, where each song has a length and a beauty. The goal is to maximize the pleasure of listening, which is calculated as the sum of the selected song lengths multiplied by the smallest beauty among those songs.

Tutorial

The intuition behind this problem is to maximize the total pleasure by carefully balancing two things: the total length of the songs you choose and the beauty of those songs. Since pleasure depends on both, the trick is to first focus on beauty because it acts as a multiplier. Higher beauty means we get a bigger boost in pleasure, so it makes sense to start by considering the most beautiful songs first.

However, we also need to manage the total length of the songs we pick, since adding longer songs can increase the total pleasure. But we can only select up to k songs, so if we reach the limit, we should drop the shortest song to keep the sum of lengths as large as possible. This way, we get the best combination of beauty and length to maximize the pleasure.

We can solve this problem by first sorting the songs in descending order of beauty, so we prioritize songs with higher beauty values. As we process each song, we maintain a running total of the song lengths using a min-heap. If the number of selected songs exceeds k, we remove the smallest length to keep the total as large as possible. At each step, we compute the pleasure by multiplying the current total length by the beauty of the song we're considering, and we update the maximum pleasure accordingly. This approach ensures we maximize the pleasure by balancing the sum of lengths and the minimum beauty.

Code

n, k = map(int,input().split())

nums = []

for _ in range(n): t, b = map(int,input().split()) nums.append([t,b])

nums.sort(reverse = True, key= lambda x: x[1])

heap = [] ans = 0 total = 0

for i in range(n): time, beauty = nums[i] total += time

if len(heap) < k:
    heappush(heap,(time,beauty))

else: 
    min_time, min_beauty = heappop(heap)
    heappush(heap,(time,beauty))
    total -= min_time

ans = max(ans,total*beauty)

print(ans) ```

Tags csec_astu, codeforces

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en48 English porcif 2024-10-17 16:46:48 2 Tiny change: '\n[here](htt' -> '[here](htt'
en47 English porcif 2024-10-17 16:45:19 138
en46 English porcif 2024-10-17 16:38:51 0 (published)
en45 English porcif 2024-10-17 16:30:51 2376
en44 English porcif 2024-10-17 16:30:37 4960 Reverted to en42
en43 English porcif 2024-10-17 16:15:47 4960
en42 English porcif 2024-10-17 14:39:39 5
en41 English porcif 2024-10-17 14:38:12 430
en40 English porcif 2024-10-17 14:23:52 38
en39 English porcif 2024-10-17 14:22:59 846
en38 English porcif 2024-10-17 14:02:29 3 Tiny change: 'oiler>\n\nCode by [u' -> 'oiler>\n\n- Code by [u'
en37 English porcif 2024-10-17 13:59:50 4
en36 English porcif 2024-10-17 13:59:19 9
en35 English porcif 2024-10-17 13:58:28 631
en34 English porcif 2024-10-17 13:35:07 1130
en33 English porcif 2024-10-17 13:04:38 56
en32 English porcif 2024-10-17 13:03:12 30
en31 English porcif 2024-10-17 13:01:34 678
en30 English porcif 2024-10-16 23:40:21 12 Tiny change: ' summary="Code">\n\nOnce' -> ' summary="Tutorial">\n\nOnce'
en29 English porcif 2024-10-16 23:38:06 820
en28 English porcif 2024-10-16 23:11:11 350
en27 English porcif 2024-10-16 23:01:10 22
en26 English porcif 2024-10-16 23:00:37 418
en25 English porcif 2024-10-16 01:31:34 5
en24 English porcif 2024-10-16 01:30:57 1 Tiny change: 'that:\n1. The Eucli' -> 'that:\n1. The Eucli'
en23 English porcif 2024-10-16 01:30:09 37 Tiny change: 'em">\n\n\n\n### **Problem Understanding**\n\nThe ta' -> 'em">\n\n\nThe ta'
en22 English porcif 2024-10-16 01:29:32 930
en21 English porcif 2024-10-16 00:43:22 3 Tiny change: 'r \n```\n9\n1 3 3 6 ' -> 'r \n```\n1 3 3 6 '
en20 English porcif 2024-10-16 00:42:51 29
en19 English porcif 2024-10-16 00:40:05 93
en18 English porcif 2024-10-16 00:37:08 866
en17 English porcif 2024-10-16 00:33:15 2654
en16 English porcif 2024-10-16 00:21:37 15
en15 English porcif 2024-10-16 00:19:18 1459
en14 English porcif 2024-10-16 00:07:47 45
en13 English porcif 2024-10-16 00:05:25 958
en12 English porcif 2024-10-16 00:01:27 4 Tiny change: '[A. Garlan' -> '### [A. Garlan'
en11 English porcif 2024-10-16 00:01:09 11
en10 English porcif 2024-10-16 00:00:52 5 Tiny change: '#### [A &mdash;' -> '[A &mdash;'
en9 English porcif 2024-10-16 00:00:38 2 Tiny change: '#### **[A &mdash; Garland](' -> '#### [A - Garland]('
en8 English porcif 2024-10-16 00:00:17 1 Tiny change: '#### **[A &mdash; Garland](' -> '#### **[A - Garland]('
en7 English porcif 2024-10-16 00:00:03 9 Tiny change: '#### **[Problem A &mdash; G' -> '#### **[A &mdash; G'
en6 English porcif 2024-10-15 23:59:36 16
en5 English porcif 2024-10-15 23:57:17 4
en4 English porcif 2024-10-15 23:57:04 67
en3 English porcif 2024-10-15 23:56:12 83
en2 English porcif 2024-10-15 23:55:53 63
en1 English porcif 2024-10-15 23:54:42 82 Initial revision (saved to drafts)