Last active
August 26, 2021 00:03
-
-
Save koher/00bd76c909ff149d31d631676f1bcc48 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://algo-method.com/tasks/302 | |
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) { | |
let values = readLine()!.split(separator: " ").map { Int(String($0))! } | |
precondition(values.count == 3) | |
return (values[0], values[1], values[2]) | |
} | |
var (n, x, y) = readInt3() | |
for _ in 2 ..< n { | |
let t = (x + y) % 100 | |
x = y | |
y = t | |
} | |
print(y) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://algo-method.com/tasks/302 | |
func readInt3(line: Int = #line, file: String = #file) -> (Int, Int, Int) { | |
let values = readLine()!.split(separator: " ").map { Int(String($0))! } | |
precondition(values.count == 3) | |
return (values[0], values[1], values[2]) | |
} | |
let (n, x, y) = readInt3() | |
var memo: [Int?] = .init(repeating: nil, count: n) | |
func dp(_ i: Int) -> Int { | |
if let r = memo[i] { return r } | |
if i == 0 { return x } | |
if i == 1 { return y } | |
let r = (dp(i - 1) + dp(i - 2)) % 100 | |
memo[i] = r | |
return r | |
} | |
print(dp(n - 1)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
func readIntN(line: Int = #line, file: String = #file) -> [Int] { | |
readLine()!.split(separator: " ").map { Int(String($0))! } | |
} | |
let n = readInt1() | |
let aa = readIntN() | |
var dp: [Int] = .init(repeating: 0, count: n) | |
dp[0] = 0 | |
dp[1] = aa[1] | |
for i in 2 ..< n { | |
dp[i] = min(dp[i - 1] + aa[i], dp[i - 2] + aa[i] * 2) | |
} | |
print(dp[n - 1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
let n = readInt1() | |
var dp: [Int] = .init(repeating: 0, count: n + 1) | |
dp[n] = 1 | |
dp[n - 1] = 1 | |
for i in (0 ..< max(0, n - 1)).reversed() { | |
dp[i] = dp[i + 1] + dp[i + 2] | |
} | |
print(dp[0]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
let n = readInt1() | |
var dp: [Int] = [1, 2, 4] | |
for i in min(3, n) ..< n { | |
assert(i == dp.count) | |
dp.append(dp[i - 1] + dp[i - 2] + dp[i - 3]) | |
} | |
print(dp[n - 1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Array2D<Element>: Sequence, CustomStringConvertible { | |
let width: Int | |
let height: Int | |
private(set) var elements: [Element] | |
let outside: Element? | |
init(width: Int, height: Int, elements: [Element], outside: Element? = nil) { | |
precondition(elements.count == width * height) | |
self.width = width | |
self.height = height | |
self.elements = elements | |
self.outside = outside | |
} | |
init(width: Int, height: Int, element: Element, outside: Element? = nil) { | |
self.init(width: width, height: height, elements: [Element](repeating: element, count: width * height), outside: outside) | |
} | |
var count: Int { elements.count } | |
var xRange: Range<Int> { 0 ..< width } | |
var yRange: Range<Int> { 0 ..< height } | |
private func indexAt(x: Int, y: Int) -> Int? { | |
guard xRange.contains(x) else { return nil } | |
guard yRange.contains(y) else { return nil } | |
return y * width + x | |
} | |
subscript(x: Int, y: Int) -> Element { | |
get { | |
guard let i = indexAt(x: x, y: y) else { return outside! } | |
return elements[i] | |
} | |
set { | |
guard let i = indexAt(x: x, y: y) else { | |
precondition(outside != nil) | |
return | |
} | |
elements[i] = newValue | |
} | |
} | |
subscript(position: (Int, Int)) -> Element { | |
get { self[position.0, position.1] } | |
set { self[position.0, position.1] = newValue } | |
} | |
func makeIterator() -> IndexingIterator<[Element]> { | |
elements.makeIterator() | |
} | |
func map<T>(_ transform: (Element) throws -> T) rethrows -> Array2D<T> { | |
try Array2D<T>(width: width, height: height, elements: elements.map(transform)) | |
} | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append("\(self[x, y])") | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
extension Array2D where Element: CustomStringConvertible { | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append(self[x, y].description) | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
func readInt4(line: Int = #line, file: String = #file) -> (Int, Int, Int, Int) { | |
let values = readLine()!.split(separator: " ").map { Int(String($0))! } | |
precondition(values.count == 4) | |
return (values[0], values[1], values[2], values[3]) | |
} | |
let (a0, a1, a2, a3) = readInt4() | |
var aaa: Array2D<Int> = .init(width: 4, height: 4, element: 0, outside: 0) | |
aaa[0, 0] = a0 | |
aaa[1, 0] = a1 | |
aaa[2, 0] = a2 | |
aaa[3, 0] = a3 | |
for y in 1 ..< aaa.height { | |
for x in aaa.xRange { | |
aaa[x, y] = aaa[x - 1, y - 1] + aaa[x, y - 1] + aaa[x + 1, y - 1] | |
} | |
} | |
print(aaa[3, 3]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct Array2D<Element>: Sequence, CustomStringConvertible { | |
let width: Int | |
let height: Int | |
private(set) var elements: [Element] | |
let outside: Element? | |
init(width: Int, height: Int, elements: [Element], outside: Element? = nil) { | |
precondition(elements.count == width * height) | |
self.width = width | |
self.height = height | |
self.elements = elements | |
self.outside = outside | |
} | |
init(width: Int, height: Int, element: Element, outside: Element? = nil) { | |
self.init(width: width, height: height, elements: [Element](repeating: element, count: width * height), outside: outside) | |
} | |
var count: Int { elements.count } | |
var xRange: Range<Int> { 0 ..< width } | |
var yRange: Range<Int> { 0 ..< height } | |
private func indexAt(x: Int, y: Int) -> Int? { | |
guard xRange.contains(x) else { return nil } | |
guard yRange.contains(y) else { return nil } | |
return y * width + x | |
} | |
subscript(x: Int, y: Int) -> Element { | |
get { | |
guard let i = indexAt(x: x, y: y) else { return outside! } | |
return elements[i] | |
} | |
set { | |
guard let i = indexAt(x: x, y: y) else { | |
precondition(outside != nil) | |
return | |
} | |
elements[i] = newValue | |
} | |
} | |
subscript(position: (Int, Int)) -> Element { | |
get { self[position.0, position.1] } | |
set { self[position.0, position.1] = newValue } | |
} | |
func makeIterator() -> IndexingIterator<[Element]> { | |
elements.makeIterator() | |
} | |
func map<T>(_ transform: (Element) throws -> T) rethrows -> Array2D<T> { | |
try Array2D<T>(width: width, height: height, elements: elements.map(transform)) | |
} | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append("\(self[x, y])") | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
extension Array2D where Element: CustomStringConvertible { | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append(self[x, y].description) | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
func readIntN() -> [Int] { | |
readLine()!.split(separator: " ").map { Int(String($0))! } | |
} | |
let n = readInt1() | |
let aa = readIntN() | |
var aaa: Array2D<Int> = .init(width: n, height: n, element: 0, outside: 0) | |
for (i, a) in aa.enumerated() { | |
aaa[i, 0] = a | |
} | |
for y in 1 ..< aaa.height { | |
for x in aaa.xRange { | |
aaa[x, y] = (aaa[x - 1, y - 1] + aaa[x, y - 1] + aaa[x + 1, y - 1]) % 100 | |
} | |
} | |
print(aaa[n - 1, n - 1]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://algo-method.com/tasks/41 | |
struct Array2D<Element>: Sequence, CustomStringConvertible { | |
let width: Int | |
let height: Int | |
private(set) var elements: [Element] | |
let outside: Element? | |
init(width: Int, height: Int, elements: [Element], outside: Element? = nil) { | |
precondition(elements.count == width * height) | |
self.width = width | |
self.height = height | |
self.elements = elements | |
self.outside = outside | |
} | |
init(width: Int, height: Int, element: Element, outside: Element? = nil) { | |
self.init(width: width, height: height, elements: [Element](repeating: element, count: width * height), outside: outside) | |
} | |
var count: Int { elements.count } | |
var xRange: Range<Int> { 0 ..< width } | |
var yRange: Range<Int> { 0 ..< height } | |
private func indexAt(x: Int, y: Int) -> Int? { | |
guard xRange.contains(x) else { return nil } | |
guard yRange.contains(y) else { return nil } | |
return y * width + x | |
} | |
subscript(x: Int, y: Int) -> Element { | |
get { | |
guard let i = indexAt(x: x, y: y) else { return outside! } | |
return elements[i] | |
} | |
set { | |
guard let i = indexAt(x: x, y: y) else { | |
precondition(outside != nil) | |
return | |
} | |
elements[i] = newValue | |
} | |
} | |
subscript(position: (Int, Int)) -> Element { | |
get { self[position.0, position.1] } | |
set { self[position.0, position.1] = newValue } | |
} | |
func makeIterator() -> IndexingIterator<[Element]> { | |
elements.makeIterator() | |
} | |
func map<T>(_ transform: (Element) throws -> T) rethrows -> Array2D<T> { | |
try Array2D<T>(width: width, height: height, elements: elements.map(transform)) | |
} | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append("\(self[x, y])") | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
extension Array2D where Element: CustomStringConvertible { | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append(self[x, y].description) | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
func readIntN() -> [Int] { | |
readLine()!.split(separator: " ").map { Int(String($0))! } | |
} | |
let n = readInt1() | |
var aaa: [Int] = [] | |
for _ in 0 ..< n { | |
let aa = readIntN() | |
aaa.append(contentsOf: aa) | |
} | |
let aMap: Array2D<Int> = .init(width: 3, height: n, elements: aaa) | |
var memo: Array2D<Int?> = .init(width: 3, height: n, element: nil, outside: 0) | |
func dp(work: Int, day: Int) -> Int { | |
if let r = memo[work, day] { return r } | |
var p = Int.min | |
for prevWork in aMap.xRange { | |
if prevWork == work { continue } | |
p = max(p, aMap[prevWork, day - 1]) | |
} | |
let r: Int = aMap[work, day] + p | |
memo[work, day] = r | |
return r | |
} | |
let ans = (0 ..< 3).map { work in dp(work: work, day: n - 1) }.max()! | |
print(ans) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://algo-method.com/tasks/41 | |
struct Array2D<Element>: Sequence, CustomStringConvertible { | |
let width: Int | |
let height: Int | |
private(set) var elements: [Element] | |
let outside: Element? | |
init(width: Int, height: Int, elements: [Element], outside: Element? = nil) { | |
precondition(elements.count == width * height) | |
self.width = width | |
self.height = height | |
self.elements = elements | |
self.outside = outside | |
} | |
init(width: Int, height: Int, element: Element, outside: Element? = nil) { | |
self.init(width: width, height: height, elements: [Element](repeating: element, count: width * height), outside: outside) | |
} | |
var count: Int { elements.count } | |
var xRange: Range<Int> { 0 ..< width } | |
var yRange: Range<Int> { 0 ..< height } | |
private func indexAt(x: Int, y: Int) -> Int? { | |
guard xRange.contains(x) else { return nil } | |
guard yRange.contains(y) else { return nil } | |
return y * width + x | |
} | |
subscript(x: Int, y: Int) -> Element { | |
get { | |
guard let i = indexAt(x: x, y: y) else { return outside! } | |
return elements[i] | |
} | |
set { | |
guard let i = indexAt(x: x, y: y) else { | |
precondition(outside != nil) | |
return | |
} | |
elements[i] = newValue | |
} | |
} | |
subscript(position: (Int, Int)) -> Element { | |
get { self[position.0, position.1] } | |
set { self[position.0, position.1] = newValue } | |
} | |
func makeIterator() -> IndexingIterator<[Element]> { | |
elements.makeIterator() | |
} | |
func map<T>(_ transform: (Element) throws -> T) rethrows -> Array2D<T> { | |
try Array2D<T>(width: width, height: height, elements: elements.map(transform)) | |
} | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append("\(self[x, y])") | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
extension Array2D where Element: CustomStringConvertible { | |
var description: String { | |
var result: String = "" | |
for y in yRange { | |
for x in xRange { | |
if x > 0 { | |
result.append(" ") | |
} | |
result.append(self[x, y].description) | |
} | |
result.append("\n") | |
} | |
return result | |
} | |
} | |
func readInt1() -> Int { | |
Int(readLine()!)! | |
} | |
func readIntN() -> [Int] { | |
readLine()!.split(separator: " ").map { Int(String($0))! } | |
} | |
let n = readInt1() | |
var aaa: [Int] = [] | |
for _ in 0 ..< n { | |
let aa = readIntN() | |
aaa.append(contentsOf: aa) | |
} | |
let aMap: Array2D<Int> = .init(width: 3, height: n, elements: aaa) | |
var dp: Array2D<Int> = .init(width: 3, height: n, element: 0, outside: 0) | |
for day in dp.yRange { | |
for work in dp.xRange { | |
var p = Int.min | |
for prevWork in dp.xRange { | |
if prevWork == work { continue } | |
p = max(p, dp[prevWork, day - 1]) | |
} | |
dp[work, day] = p + aMap[work, day] | |
} | |
} | |
let ans = (0 ..< 3).map { work in dp[work, n - 1] }.max()! | |
print(ans) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment