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

16-SeongHoonC #60

Merged
merged 2 commits into from
Mar 28, 2024
Merged

16-SeongHoonC #60

merged 2 commits into from
Mar 28, 2024

Conversation

SeongHoonC
Copy link
Collaborator

πŸ”— 문제 링크

ꡬ간 ν•© κ΅¬ν•˜κΈ° 4

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

40λΆ„

✨ μˆ˜λ„ μ½”λ“œ

첫번째 μ‹œλ„

리슀트λ₯Ό μž˜λΌμ„œ 총합 κ΅¬ν–ˆλ”λ‹ˆ λ‹Ήμ—°νžˆ μ‹œκ°„μ΄ˆκ³Όκ°€ λ°œμƒν–ˆμŠ΅λ‹ˆλ‹€.
μ΅œλŒ€ 100000(N) * 100000(M) 번 연산이 싀행될 수 있기 λ•Œλ¬Έμ΄μ—ˆμ£ 

import java.io.BufferedReader
import java.io.InputStreamReader

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val (n, m) = br.readLine().split(" ").map { it.toInt() }
    val nums = br.readLine().split(" ").map { it.toInt() }
    for (i in 0 until m) {
        val (start, end) = br.readLine().split(" ").map { it.toInt() }
        println(nums.subList(start - 1, end).sum())
    }
}

λͺ»ν’€κ² λ‹€ μ‹Άμ–΄μ„œ μœ ν˜•μ„ λ΄€λŠ”λ° λˆ„μ ν•©μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

μ•„.. 5 λ²ˆμ§ΈκΉŒμ§€ λˆ„μ ν•©μ— 2 λ²ˆμ§ΈκΉŒμ§€ λˆ„μ ν•©μ„ λΉΌλ©΄ 3~5 ꡬ간합이 λ‚˜μ˜€λŠ” κ²ƒμ΄μ—ˆμŠ΅λ‹ˆλ‹€.

  1. λˆ„μ ν•© 배열을 λ§Œλ“€κ³  각 μžλ¦¬κΉŒμ§€μ˜ λˆ„μ ν•©μ„ κ΅¬ν•œ ν›„ (μ—°μ‚° N번)
  2. M 번 만큼 ꡬ간합을 ꡬ해 좜λ ₯ν•©λ‹ˆλ‹€.

이러면 N + M 번 연산을 μˆ˜ν–‰ν•  κ²ƒμœΌλ‘œ μ˜ˆμƒλ©λ‹ˆλ‹€.

print 도 μ‹œκ°„μ΄ μ˜€λž˜κ±Έλ¦°λ‹€κΈΈλž˜ BufferedWriter 에 write ν•˜κ³  ν•œλ²ˆμ— flush ν–ˆμŠ΅λ‹ˆλ‹€.

import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.InputStreamReader
import java.io.OutputStreamWriter

fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val bw = BufferedWriter(OutputStreamWriter(System.out))
    val (n, m) = br.readLine().split(" ").map { it.toInt() }
    val nums = br.readLine().split(" ").map { it.toInt() }
    val addedSum = Array(n + 1) { 0 }
    addedSum[1] = nums[0]

    // i κΉŒμ§€ λˆ„μ ν•© κ΅¬ν•˜κΈ°
    for (i in 2..n) {
        addedSum[i] = addedSum[i - 1] + nums[i - 1]
    }

    // λˆ„μ ν•© end μ—μ„œ λˆ„μ ν•© start - 1 λ₯Ό λΉΌμ„œ start ~ end ꡬ간 ν•© κ΅¬ν•˜κΈ°
    for (i in 0 until m) {
        val (start, end) = br.readLine().split(" ").map { it.toInt() }
        bw.write((addedSum[end] - addedSum[start - 1]).toString())
        bw.newLine()
    }
    // BufferedWriter 둜 ν•œλ²ˆμ— 좜λ ₯
    bw.flush()
    bw.close()
}

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

λˆ„μ ν•©μ„ μ΄ν•΄ν•˜κ²Œ λ˜μ—ˆλ„€μš”.
μ•Œλ©΄ μ‰¬μš΄λ° λͺ¨λ₯΄λ©΄ λ– μ˜¬λ¦¬κΈ° μ–΄λ €μš΄ λ¬Έμ œκ°™μ•„μ„œ κ³΅μœ ν–ˆμŠ΅λ‹ˆλ‹€.

Copy link
Member

@fnzksxl fnzksxl left a comment

Choose a reason for hiding this comment

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

μ•„ 이 문제 μ˜€λžœλ§Œμ΄λ„€μš” γ…‹γ…‹γ…‹γ…‹γ…‹

λ˜‘κ°™μ€ 둜직으둜 ν’€μ—ˆλ˜ λ¬Έμ œμž…λ‹ˆλ‹€!

import sys

input = sys.stdin.readline

N, M = map(int, input().split())

lst = list((map(int, input().split())))
sums = [0] * (N+1)

for i in range(N):
    sums[i+1] = sums[i] + lst[i]

for _ in range(M):
    i, j = map(int, input().split())
    print(sums[j] - sums[i-1])

@alstjr7437
Copy link
Member

ν—‰ 저도 νƒœμ›λ‹˜μ΄λž‘ λΉ„μŠ·ν•œ μ½”λ“œλ‘œ μž‘μ„±λ˜λŠ”λ° temp만 μΆ”κ°€λ˜μ„œ ν–ˆλ„€μš”!!

import sys
input = sys.stdin.readline
 
m, n = map(int, input().split())
arr = list(map(int, input().split()))
answer = [0]    
 
temp = 0    
for i in arr:  
    temp += i
    answer.append(temp)
 
# print(answer)

for i in range(n):
    a, b = map(int, input().split())
    print(answer[b] - answer[a-1])

Copy link
Collaborator

@wkdghdwns199 wkdghdwns199 left a comment

Choose a reason for hiding this comment

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

μ²˜μŒμ— 문제 μ™„μ „ 쉽ꡰ μ΄λž¬λ‹€κ°€ μ‹œκ°„μ΄ˆκ³Ό 뜨고 λ‹Ήν™©... 후에 λ‹€λ₯Έ λΆ„λ“€ μ½”λ“œλ³΄κ³  머리가 λ΅ν–ˆλ„€μš” γ…‹γ…‹γ…‹
λˆ„μ ν•© κ°œλ…μœΌλ‘œ ν•œ 수 λ°°μ›λ‹ˆλ‹€!

import sys
input = sys.stdin.readline
N,M = map(int, input().split())
num_list = list(map(int, input().split()))
accumulate_list = [0] * (N+1)
for i in range(N):
    accumulate_list[i+1] = accumulate_list[i] + num_list[i]

for _ in range(M):
    start, end = map(int, input().split())
    print(accumulate_list[end] - accumulate_list[start-1])

@SeongHoonC SeongHoonC merged commit f8bc67d into main Mar 28, 2024
6 checks passed
@SeongHoonC SeongHoonC deleted the 16-SeongHoonC branch March 28, 2024 04:17
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