Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

29-yuna83 #136

Merged
merged 1 commit into from
Mar 4, 2024
Merged

29-yuna83 #136

merged 1 commit into from
Mar 4, 2024

Conversation

yuna83
Copy link
Member

@yuna83 yuna83 commented Feb 13, 2024

๐Ÿ”— ๋ฌธ์ œ ๋งํฌ

๋ธ”๋กœ๊ทธ


โœ”๏ธ ์†Œ์š”๋œ ์‹œ๊ฐ„

30๋ถ„


โœจ ์ˆ˜๋„ ์ฝ”๋“œ

๋ธ”๋กœ๊ทธ๋ฅผ ์‹œ์ž‘ํ•˜๊ณ  ์ง€๋‚œ ์ผ์ˆ˜ n
x์ผ ๋™์•ˆ ๊ฐ€์žฅ ๋งŽ์ด ๋“ค์–ด์˜จ ๋ฐฉ๋ฌธ์ž ์ˆ˜์™€ ๊ทธ ๊ธฐ๊ฐ„์ด ๋ช‡ ๊ฐœ๊ฐ€ ์žˆ๋Š” ์ง€ ์ถœ๋ ฅ!

ex)
n=5, x=2
1 4 2 5 1
์ด๋ฉด

2์ผ๋™์•ˆ ๊ฐ€์žฅ ๋งŽ์ด ๋“ค์–ด์˜จ ์‚ฌ๋žŒ์˜ ์ˆ˜๋Š” 2+5= 7๋ช…,
7๋ช…์ด ๋“ค์–ด์˜จ ๊ธฐ๊ฐ„์€ 3~4์ผ ๋ฐ–์— ์—†์œผ๋ฏ€๋กœ 1๋ฒˆ์œผ๋กœ
7
1
์„ ์ถœ๋ ฅํ•œ๋‹ค!


์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๐ŸชŸ

: ๊ณ ์ • ์‚ฌ์ด์ฆˆ์˜ ์œˆ๋„์šฐ๊ฐ€ ์ด๋™ํ•˜๋ฉด์„œ ์œˆ๋„์šฐ ๋‚ด์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์ดํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
๋ฐฐ์—ด์ด๋‚˜ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ์˜ ์ผ์ • ๋ฒ”์œ„์˜ ๊ฐ’์„ ๋น„๊ตํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ์œ ์šฉํ•˜๋‹ค!

์ด ๋ฌธ์ œ๋Š” ์ฃผ์–ด์ง„ n์ผ์˜ ๋ฐฉ๋ฌธ์ž์ˆ˜๋ฅผ
์ˆœ์„œ๋Œ€๋กœ x์ผ ๋งŒํผ์œผ๋กœ ์œˆ๋„์šฐ๋ฅผ ์„ค์ •ํ•ด
๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ์œˆ๋„์šฐ ์ค‘ ์ตœ๋Œ“๊ฐ’๊ณผ, ๊ทธ ์ตœ๋Œ“๊ฐ’์ด ๋ช‡ ๋ฒˆ ๋‚˜์˜ค๋Š”์ง€ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค!


1. ์ฒ˜์Œ ์œˆ๋„์šฐ๋ฅผ ์ตœ๋Œ“๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.

value = sum(data[:x])  # x๊ฐœ์”ฉ ์œˆ๋„์šฐ ์ƒ์„ฑ
    max_value=value
    max_cnt=1

data: ์‹œ์ž‘ํ•œ 1์ผ๋ถ€ํ„ฐ n์ผ๊นŒ์ง€ ํ•˜๋ฃจ ๋ฐฉ๋ฌธ์ž ์ˆ˜ ๋ฆฌ์ŠคํŠธ
ex)
n=5, x=2์ด๋ฉด (1 4 2 5 1)
value = 1+4 = 5


2. ์œˆ๋„์šฐ๋ฅผ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฉด์„œ ์œˆ๋„์šฐ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

for i in range(x,n):
        value+=data[i]
        value-=data[i-x]

์œˆ๋„์šฐ๋Š” ํ•œ์นธ์”ฉ ์ด๋™ํ•˜๋ฏ€๋กœ ๋งค๋ฒˆ ์ƒˆ๋กญ๊ฒŒ ๊ณ„์‚ฐํ•˜๋Š”๊ฒƒ๋ณด๋‹ค
์ด์ „ ์œˆ๋„์šฐ์˜ ๊ฐ’vaiue์—์„œ
์ƒˆ๋กœ์šด ๊ฐ’์„ ๋”ํ•˜๊ณ , ์ด์ „ ์š”์†Œ๋Š” ๋นผ๋Š” ๊ฒŒ ํšจ์œจ์ ์ด๋‹ค!
ex)
n=5, x=2์ด๋ฉด (1 4 2 5 1)
value = 5+2-1= 6


3. ์ƒˆ๋กœ์šด ์œˆ๋„์šฐ๊ฐ€ ์›๋ž˜ ์œˆ๋„์šฐ๋ณด๋‹ค ํฌ๋ฉด ์ตœ๋Œ“๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

if value > max_value:
            max_value=value
            max_cnt=1

ex)
ํ˜„์žฌ value=6, max_value=5์ด๋ฏ€๋กœ
max_value๋ฅผ 6์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค!


4. ์ƒˆ๋กœ์šด ์œˆ๋„์šฐ๊ฐ€ ์›๋ž˜ ์œˆ๋„์šฐ์™€ ๋™์ผํ•˜๋ฉด ๊ฐœ์ˆ˜๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.

 elif value == max_value:
            max_cnt+=1

๋ชจ๋“  ์œˆ๋„์šฐ๋ฅผ ๋น„๊ตํ•œ ํ›„ ๊ฐฑ์‹ ๋œ max_value์™€ max_cnt๋ฅผ ์ถœ๋ ฅํ•ด์ฃผ๋ฉด ๋œ๋‹ค.
์ตœ๋Œ€ ๋ฐฉ๋ฌธ์ž์ˆ˜๊ฐ€ 0๋ช…์ธ ๊ฒฝ์šฐ SAD๊ฐ€ ์ถœ๋ ฅ๋˜๋„๋ก ์กฐ๊ฑด๋ฌธ์„ ์„ค์ •ํ•ด์ฃผ๋Š” ๊ฒƒ๋„ ์žŠ์–ด์„  ์•ˆ๋œ๋‹ค!


์ „์ฒด์ฝ”๋“œ

import sys
input=sys.stdin.readline

n,x=map(int,input().split())
data=list(map(int,input().split()))

if max(data) == 0:
    print("SAD")
else:
    value = sum(data[:x])  # x๊ฐœ์”ฉ ์œˆ๋„์šฐ ์ƒ์„ฑ
    max_value=value
    max_cnt=1

    for i in range(x,n):
        value+=data[i]
        value-=data[i-x]

        if value > max_value:
            max_value=value
            max_cnt =1

        elif value == max_value:
            max_cnt+=1

    print(max_value)
    print(max_cnt)

๐Ÿ“š ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋ž€ ์ด๋ฆ„์ด ๋‚ฏ์„ค์–ด์„œ ์–ด๋ ต๊ฒŒ ๋Š๊ปด์กŒ๋Š”๋ฐ
๋ฒ”์œ„๊ฐ€ ์ •ํ•ด์ง„ ํ•ฉ๋“ค์˜ ๋น„๊ต๋ฅผ ํ•  ๋•Œ ์—„์ฒญ ํŽธ๋ฆฌํ•˜๋„ค์š”!!
๋‹ค์Œ ๋ฌธ์ œ๋Š” ์‹ฌํ™”๋ฒ„์ „์œผ๋กœ ํ’€์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค!!

<๋„์›€๋ฐ›์€ ์ถœ์ฒ˜>

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๊ฐœ๋… & ์ฝ”๋“œ
https://ji-musclecode.tistory.com/37


Copy link
Member

@tgyuuAn tgyuuAn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์•„ ์ €๋Š” ์ด๊ฑฐ ๋‹จ์ˆœ ํ ๋ฌธ์ œ ์ธ ์ค„ ์•Œ์•—๋Š”๋ฐ

์ƒ๊ฐํ•ด๋ณด๋‹ˆ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๊ฐ€ ๋” ์ ํ•ฉํ•˜๊ฒ ๋„ค์š”...

์•„์ฐจ์ฐจ.... ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ํ•˜๋‹ˆ๊นŒ ๋„คํŠธ์›Œํฌ ์‹œ๊ฐ„์— ๋ฒ„ํผ ๊ด€๋ฆฌํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋˜๊ฑฐ ์ƒ๊ฐ๋‚˜๋„ค์š”...

from collections import deque

total_blog_day, duration = map(int,input().split())

_max = 0
max_duration = 0
deq = deque()
now = 0
for today in map(int,input().split()):
    if len(deq) >= duration:
        top = deq.popleft()
        now -= top
        deq.append(today)
        now += today

    elif len(deq) < duration:
        now += today
        deq.append(today)

    if _max < now:
        _max = now
        max_duration = 1

    elif _max == now:
        max_duration += 1

if _max == 0: print("SAD")
else:
    print(_max)
    print(max_duration)

@pknujsp
Copy link
Collaborator

pknujsp commented Feb 21, 2024

๋ธ”๋กœ๊ทธ ๋ฐฉ๋ฌธ ์ˆ˜ ๋ˆ„์ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ ๋’ค ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ๋กœ X์ผ ์น˜์˜ ์ตœ๋Œ“๊ฐ’์„ ๊ตฌํ•˜๋Š” ์‹์œผ๋กœ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ๋กœ ๋ฐฉ๋ฌธ์ž์ˆ˜๋ฅผ ๊ธฐ๋กํ•ด๋†“๊ณ  ๋‚˜์ค‘์— ์ •๋ ฌํ•ด์„œ ํ‘ธ๋Š” ์‹์˜ ์ฝ”๋“œ์ธ๋ฐ ๋‹ค ํ’€๊ณ  ๋ณด๋‹ˆ

์ •๋ ฌ์€ ์—†์–ด๋„ ๋˜๊ฒ ๋„ค์š”

N, X = map(int, stdin.readline().split())
visits = list(map(int, stdin.readline().split()))

for i in range(1, N):
    visits[i] += visits[i - 1]

max_visits = {}
for right in range(X - 1, N):
    if right == X - 1:
        if visits[right] > 0:
            max_visits[visits[right]] = 1
    else:
        count = visits[right] - visits[right - X]
        if count > 0:
            max_visits[count] = max_visits.get(count, 0) + 1

if not max_visits:
    print('SAD')
else:
    visit_items = list(max_visits.items())
    visit_items.sort(key=lambda x: x[0])
    print(visit_items[-1][0])
    print(visit_items[-1][1])

@Munbin-Lee
Copy link
Member

Code
#include <iostream>
#include <vector>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N, X;
    cin >> N >> X;

    vector<int> visitors(N);

    for (int &visitor: visitors) {
        cin >> visitor;
    }

    int cur = 0;

    for (int i = 0; i < X; i++) {
        cur += visitors[i];
    }

    int lo = 0;
    int hi = X;
    int maxVisitors = 0;
    int period = -1;

    while (true) {
        if (cur == maxVisitors) {
            period++;
        } else if (cur > maxVisitors) {
            maxVisitors = cur;
            period = 1;
        }

        if (hi >= N) break;

        cur -= visitors[lo];
        cur += visitors[hi];
        lo++;
        hi++;
    }

    if (maxVisitors == 0) {
        cout << "SAD";
        return 0;
    }

    cout << maxVisitors << '\n' << period;

    return 0;
}

๋ฌธ์ œ ์ž์ฒด๋Š” ๊ฐ„๋‹จํ•˜์ง€๋งŒ, off by one ์˜ค๋ฅ˜๊ฐ€ ์ƒ๊ธธ๊นŒ๋ด ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๊ณ ๋ฏผ์„ ํ•˜๋ฉฐ ๋ฌธ์ œ๋ฅผ ํ’€์—ˆ์–ด์š”.

๊ฒฐ๊ตญ [lo, hi) ์ฒ˜๋Ÿผ ๋ฐ˜ํ๊ตฌ๊ฐ„์„ ์„ธ์›Œ์„œ ํ’€์—ˆ๋Š”๋ฐ, ํ™•์‹คํžˆ ๊ธฐ์ค€์ด ๋ช…ํ™•ํ•ด์ง€๋‹ˆ๊นŒ ๊น”๋”ํ•˜๋„ค์š”.

์ฐธ๊ณ ์ž๋ฃŒ : https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants