-
Notifications
You must be signed in to change notification settings - Fork 1
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
16-alstjr7437 #57
Conversation
answer = dohwaji[0][1] - dohwaji[0][0] | ||
|
||
for left, right in dohwaji[1:]: | ||
if right <= line: |
There was a problem hiding this comment.
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 ์ ์ธํ๋๊ฑด ์๋ชป๋๋ฌ์ ํฌํจ๋์ด๋ค ํํ!
There was a problem hiding this 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 ์ ์ด๊ธฐํํ๊ณ
์ธ๋ฑ์ค๋ฅผ ์ฌ๋ ค๊ฐ๋ฉด์
- tail ์ด x ๋ณด๋ค ํฐ ๊ฒฝ์ฐ
- tail ์ด y ๋ณด๋ค ์๊ฑฐ๋ ๊ฐ์ ๊ฒฝ์ฐ
- 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)
}
There was a problem hiding this 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)
๐ ๋ฌธ์ ๋งํฌ
์ ๊ธ๊ธฐ
โ๏ธ ์์๋ ์๊ฐ
40๋ถ
โจ ์๋ ์ฝ๋
๐ก ์์
1, 5
2, 4
6, 7
์ด ๋ค์ด์ค๋ฉด
์ ์๋์ฝ๋์์
์ด๋ ๊ฒ ํด์ ํ์๋๋ฐ ๊ฐ์๊ธฐ ํ๋ ธ์ต๋๋ค๊ฐ ๋จ๊ธธ๋ ๋ฐ๋ก๋ฅผ ๋ณด๋๊น
๊ทธ๋์ -1,000,000,000 1,000,000,000 ๋ฅผ ๋ฃ์ผ๋ฉด
2,000,000,000๋ฅผ ์ถ๋ ฅํด์ผํ๋๋ฐ 1,000,000,000์ด ๋์ค๊ณ
-1000000000 -999999999์ ๋ฃ์ผ๋ฉด
1์ด ์ถ๋ ฅ๋์ผ ํ๋๋ฐ 0์ด ๋์์ต๋๋ค.
๋จ์ ๋น๊ต๋ก ํ๋๊น ๋ ์๊ฒ ํ๋ ๋ถ๋ถ์ด ๋๋ฏ๋ก
-๊ฐ ๋ค์ด์ค๋ ๋ถ๋ถ์ ๋น๊ต๋ฅผ ๋ชปํ์์ต๋๋ค.
๊ทธ๋์ ๋ฌด๋ ค!!! ์์ฒญ๋ ๋ฐฉ๋ฒ์ผ๋ก ํ์์ต๋๋ค.
๐ป ์ฝ๋
๋ฌผ๋ก ์์ ๋ถ๋ถ ํจ์จ์ ๋ฐ์ด ๋ฌ์ต๋๋ค^^7
๋ค๋ฅธ๋ถ๋ค ์ฝ๋๋ฅผ ๋ฃ์ด๋ณด๊ณ
์ ์ฝ๋๋ฅผ ๋ฐ๊ฟ์ 2000๋๋ฅผ ๋ซ์์ต๋๋ค..
๐ ์๋กญ๊ฒ ์๊ฒ๋ ๋ด์ฉ
๋ฌธ์ ๋ ๋ง์ถฐ๋ ์๊ฐ ๋ณต์ก๋ ์๊ฐ์ ๊ณ์ํด์ ํ์..