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-alstjr7437 #57

Merged
merged 4 commits into from
Mar 23, 2024
Merged

16-alstjr7437 #57

merged 4 commits into from
Mar 23, 2024

Conversation

alstjr7437
Copy link
Member

@alstjr7437 alstjr7437 commented Mar 13, 2024

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

์„  ๊ธ‹๊ธฐ

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

40๋ถ„

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

  1. ์ค„์˜ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ(line)
  2. ํ˜„์žฌ๊นŒ์ง€์˜ ์ด ๊ธธ์ด๋ฅผ ์ €์žฅํ•  ๋ณ€์ˆ˜ ๋งŒ๋“ค๊ธฐ(answer)
  3. ๋„ํ™”์ง€ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณต๋ฌธ ๋Œ๋ฆฌ๊ธฐ(i,j)
    1. ์ตœ๋Œ€ ๊ธธ์ด(line)์ด ์•ž์ชฝ์ด ๋†’์œผ๋ฉด(i) ์ƒˆ๋กœ์šด ์ค„ ๋งŒ๋“ค๊ธฐ
    2. ๋” ์ž‘๊ณ  ๋’ค์— ์ค„(j)์ด ์ตœ๋Œ€ ๊ธธ์ด(line)๋ณด๋‹ค ํฌ๋ฉด ์ค„ ๊ธธ์ด ๋Š˜๋ฆฌ๊ธฐ
  4. ์ด ๊ธธ์ด ์ถœ๋ ฅํ•˜๊ธฐ(answer)




๐Ÿ’ก ์˜ˆ์‹œ

1, 5
2, 4
6, 7

1 2 3 4 5 6 7
ใ…‡ ใ…‡ ใ…‡ ใ…‡ ใ…‡
ใ…‡ ใ…‡ ใ…‡
ใ…‡ ใ…‡

์ด ๋“ค์–ด์˜ค๋ฉด

์œ„ ์ˆ˜๋„์ฝ”๋“œ์—์„œ

  1. line์— 5๊ฐ€ ๋“ค์–ด๊ฐ‘๋‹ˆ๋‹ค.(answer์— 5-1๋กœ 4์ถ”๊ฐ€)
  2. 2,4๊ฐ€ i,j๋กœ ๋“ค์–ด๊ฐ€์ง€๋งŒ line๋ณด๋‹ค ์ž‘์œผ๋ฏ€๋กœ ๋„˜์–ด๊ฐ‘๋‹ˆ๋‹ค.
  3. 6, 7์€ i์ธ 6์ด line์ธ 5๋ณด๋‹ค ํฌ๋ฏ€๋กœ answer์— 7-6 = 1์„ ์ถ”๊ฐ€ ํ•ฉ๋‹ˆ๋‹ค.
  4. ๊ทธ๋ฆฌ๊ณ  line์€ 7๋กœ ๊ฐฑ์‹ ํ•ฉ๋‹ˆ๋‹ค.




์ด๋ ‡๊ฒŒ ํ•ด์„œ ํ’€์—ˆ๋Š”๋ฐ ๊ฐ‘์ž๊ธฐ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ๋œจ๊ธธ๋ž˜ ๋ฐ˜๋ก€๋ฅผ ๋ณด๋‹ˆ๊นŒ

x, y (-1,000,000,000 โ‰ค x < y โ‰ค 1,000,000,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.
๋ผ๋Š” ๋ง์ด ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ -1,000,000,000 1,000,000,000 ๋ฅผ ๋„ฃ์œผ๋ฉด
2,000,000,000๋ฅผ ์ถœ๋ ฅํ•ด์•ผํ•˜๋Š”๋ฐ 1,000,000,000์ด ๋‚˜์˜ค๊ณ 

-1000000000 -999999999์„ ๋„ฃ์œผ๋ฉด
1์ด ์ถœ๋ ฅ๋˜์•ผ ํ•˜๋Š”๋ฐ 0์ด ๋‚˜์™”์Šต๋‹ˆ๋‹ค.
๋‹จ์ˆœ ๋น„๊ต๋กœ ํ•˜๋‹ˆ๊นŒ ๋” ์ž‘๊ฒŒ ํ•˜๋Š” ๋ถ€๋ถ„์ด ๋˜๋ฏ€๋กœ
-๊ฐ€ ๋“ค์–ด์˜ค๋Š” ๋ถ€๋ถ„์„ ๋น„๊ต๋ฅผ ๋ชปํ–ˆ์—ˆ์Šต๋‹ˆ๋‹ค.




๊ทธ๋ž˜์„œ ๋ฌด๋ ค!!! ์—„์ฒญ๋‚œ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ’ป ์ฝ”๋“œ

import sys
input = sys.stdin.readline

n = int(input())
dohwaji = [list(map(int,(input().split()))) for _ in range(n)]

for i in range(len(dohwaji)):
    dohwaji[i][0] += 1000000000
    dohwaji[i][1] += 1000000000

dohwaji.sort(key=lambda x : (x[0], x[1]))

line = 0
answer = 0

for left, right in dohwaji:
    if line < left:
        answer += right - left
        line = right
    elif line >= left and right > line: 
        answer += right - line
        line = right

print(answer)

โš ๏ธ๋ฌธ์ œ

๋ฌผ๋ก  ์œ„์— ๋ถ€๋ถ„ ํšจ์œจ์€ ๋ฐ•์‚ด ๋‚ฌ์Šต๋‹ˆ๋‹ค^^7
๋‹ค๋ฅธ๋ถ„๋“ค ์ฝ”๋“œ๋ฅผ ๋„ฃ์–ด๋ณด๊ณ 
์ œ ์ฝ”๋“œ๋ฅผ ๋ฐ”๊ฟ”์„œ 2000๋Œ€๋ฅผ ๋šซ์—ˆ์Šต๋‹ˆ๋‹ค..
image

import sys
input = sys.stdin.readline

n = int(input())
dohwaji = [tuple(map(int,(input().split()))) for _ in range(n)]

# for i in range(len(dohwaji)):
#     dohwaji[i][0] += 1000000000
#     dohwaji[i][1] += 1000000000

dohwaji.sort()

line = dohwaji[0][1]
answer = dohwaji[0][1] - dohwaji[0][0]

for left, right in dohwaji[1:]:
    if right <= line:
        continue
    elif line < left:
        answer += right - left
        line = right
    elif line >= left and right > line: 
        answer += right - line
        line = right

print(answer)

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

๋ฌธ์ œ๋Š” ๋งž์ถฐ๋„ ์‹œ๊ฐ„ ๋ณต์žก๋„ ์ƒ๊ฐ์„ ๊ณ„์†ํ•ด์„œ ํ•˜์ž..

answer = dohwaji[0][1] - dohwaji[0][0]

for left, right in dohwaji[1:]:
if right <= line:
Copy link
Member

@fnzksxl fnzksxl Mar 13, 2024

Choose a reason for hiding this comment

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

continue ๋ถ€๋ถ„์„ ๋”ฐ๋กœ ์•ˆ ๋นผ๋„ ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

import sys

input = sys.stdin.readline


N = int(input())

dots = []

for _ in range(N):
    dots.append(tuple(map(int, input().split())))

dots.sort()
length = dots[0][1] - dots[0][0]
target = dots[0][1]

for left, right in dots[1:]:
    if right > target:
        if left > target:
            length += right - left
        else:
            length += right - target
        
        target = right

print(length)

์ œ๊ฐ€ ๊ทธ๋ฆผํŒ์— ๊ทธ๋ ค๊ฐ€๋ฉด์„œ ํ•ด๋ณด๋‹ˆ๊นŒ ์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋ฌธ์ œ ํ†ต๊ณผ ๋˜๋„ค์š”. 2804ms ๋‚˜์™”์Šต๋‹ˆ๋‹ค!

p.s ์œ„์— answer ์„ ์–ธํ•˜๋Š”๊ฑด ์ž˜๋ชป๋ˆŒ๋Ÿฌ์„œ ํฌํ•จ๋์Šด๋‹ค ํ•˜ํ•˜!

Copy link
Collaborator

@SeongHoonC SeongHoonC left a comment

Choose a reason for hiding this comment

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

์ €๋Š” head tail ์„ ์˜ฎ๊ฒจ๊ฐ€๋ฉด์„œ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค.
์ •๋ ฌ ํ›„ ๊ฐ€์žฅ ์ž‘์€ ์ ์œผ๋กœ head ์™€ tail ์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ 
์ธ๋ฑ์Šค๋ฅผ ์˜ฌ๋ ค๊ฐ€๋ฉด์„œ

  1. tail ์ด x ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
  2. tail ์ด y ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ
  3. tail ์ด y ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
    ๋ฅผ ๋‚˜๋ˆ„์–ด ๋ถ„๊ธฐ์ฒ˜๋ฆฌ ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.
fun main() {
    val br = BufferedReader(InputStreamReader(System.`in`))
    val n = br.readLine().toInt()
    val lines = mutableListOf<Pair<Long, Long>>()
    for (i in 0 until n) {
        val (x, y) = br.readLine().split(" ").map { it.toLong() }
        lines.add(x to y)
    }
    lines.sortBy { it.first }

    var head: Long = lines[0].first
    var tail: Long = lines[0].second
    var sum = tail - head

    for (i in 1 until lines.size) {
        val now = lines[i]
        // tail ์ด x ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
        if (tail < now.first) {
            head = now.first
            tail = now.second
            sum += tail - head
            continue
        }
        // tail ์ด y ๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๊ฒฝ์šฐ
        if (now.second <= tail) {
            continue
        }
        // tail ์ด y ๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ
        sum += now.second - tail
        tail = now.second
    }
    println(sum)
}

@wkdghdwns199 wkdghdwns199 requested review from wkdghdwns199 and removed request for wkdghdwns199 March 20, 2024 02:26
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 = int(input())
lines = [list(map(int, input().rstrip().split())) for _ in range(N)]
lines.sort()

length =0 
current_start = lines[0][0]
current_end = lines[0][1]
for start, end in lines[1:]:
    if start > current_end :
        print(start, current_end)
        length += (current_end - current_start)
        current_start = start
    current_end = max(current_end, end)
length += (current_end - current_start)
print(length)

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