Submission #3420216


Source Code Expand

import strutils
import sequtils
import algorithm
import math
import queues
import tables
import sets
import logging
import future

const INF* = int(1e18 + 373)

proc readSeq*(): seq[string] = stdin.readLine().strip().split()
proc readSeq*(n: Natural): seq[string] =
  result = newSeq[string](n)
  for i in 0..<n:
    result[i] = stdin.readLine().strip()

proc readInt1*(): int = readSeq().map(parseInt)[0]
proc readInt2*(): (int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1])
proc readInt3*(): (int, int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1], a[2])
proc readInt4*(): (int, int, int, int) =
  let a = readSeq().map(parseInt)
  return (a[0], a[1], a[2], a[3])

type seq2*[T] = seq[seq[T]]
proc newSeq2*[T](n1, n2: Natural): seq2[T] = newSeqWith(n1, newSeq[T](n2))

#------------------------------------------------------------------------------#
proc eval(s: string): int =
  var zeroInd = @[ -1 ]
  for i in 0..<s.len():
    if s[i] == '0':
      zeroInd.add(i)
  zeroInd.add(s.len())

  var ones = newSeq[int]()
  for i in 0..<zeroInd.len() - 1:
    ones.add(zeroInd[i + 1] - zeroInd[i] - 1)

  let k = ones.len()
  if k == 1:
    return 0

  var dp = newSeq2[int](k + 1, 3)
  dp[k][0] = 0
  dp[k][1] = 0
  dp[k][2] = 0
  dp[k - 1][0] = 0
  dp[k - 1][1] = 0
  dp[k - 1][2] = 0
  for i in countdown(k - 2, 0):
    let ll = ones[i]
    let rr = ones[i + 1]

    dp[i][0] = dp[i + 1][0]
    dp[i][0] = max(dp[i][0], ll + dp[i + 1][1])
    dp[i][0] = max(dp[i][0], rr + dp[i + 2][0])
    if rr >= 2:
      dp[i][0] = max(dp[i][0], (rr - 1) + dp[i + 1][2])

    dp[i][1] = dp[i + 1][0]
    if ll >= 2:
      dp[i][1] = max(dp[i][1], (ll - 1) + dp[i + 1][1])
      dp[i][1] = max(dp[i][1], rr + dp[i + 2][0])
      if rr >= 2:
        dp[i][1] = max(dp[i][1], (rr - 1) + dp[i + 1][2])

    dp[i][2] = dp[i + 1][0]
    dp[i][2] = max(dp[i][2], 1 + dp[i + 1][1])
    dp[i][2] = max(dp[i][2], rr + dp[i + 2][0])
    if rr >= 2:
      dp[i][2] = max(dp[i][2], (rr - 1) + dp[i + 1][2])

  # echo s
  # echo ones
  # echo dp[0][0]
  # echo ""
  return dp[0][0]

proc main() =
  let n = readInt1()
  let s = stdin.readLine()

  var sec = newSeq[string](0)

  var i = 0
  while i < n and s[i] == '0':
    i += 1

  while i < n:
    var j = i
    while j < n:
      if s[j] == '0' and (j + 1 >= n or s[j + 1] == '0'):
        break
      j += 1

    sec.add(s[i..<j])

    i = j
    while i < n and s[i] == '0':
      i += 1

  var ans = 0
  for s in sec:
    ans += s.eval()
  echo ans

main()

Submission Info

Submission Time
Task D - 101 to 010
User somq14
Language Nim (0.13.0)
Score 700
Code Size 2637 Byte
Status AC
Exec Time 42 ms
Memory 4860 KB

Compile Error

Hint: system [Processing]
Hint: Main [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: sequtils [Processing]
Hint: algorithm [Processing]
Hint: math [Processing]
Hint: times [Processing]
Hint: queues [Processing]
Hint: tables [Processing]
Hint: hashes [Processing]
Hint: etcpriv [Processing]
Hint: sets [Processing]
Hint: os [Processing]
Hint: posix [Processing]
Hint: logging [Processing]
lib/pure/logging.nim(128, 22) Hint: 'Exception' is declared but not used [XDeclaredButNotUsed]
Hint: future [Processing]
Hint: macros [Processing]
Hint:  [Link]
Hint: operation successful (24901 lines compiled; 2.837 sec total; 25.256MB; Release Build) [SuccessX]

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 700 / 700
Status
AC × 2
AC × 39
Set Name Test Cases
Sample example0.txt, example1.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, example0.txt, example1.txt
Case Name Status Exec Time Memory
000.txt AC 4 ms 1148 KB
001.txt AC 6 ms 1148 KB
002.txt AC 15 ms 2684 KB
003.txt AC 11 ms 1532 KB
004.txt AC 15 ms 2172 KB
005.txt AC 20 ms 2044 KB
006.txt AC 4 ms 636 KB
007.txt AC 7 ms 1148 KB
008.txt AC 1 ms 508 KB
009.txt AC 6 ms 2300 KB
010.txt AC 2 ms 1404 KB
011.txt AC 15 ms 3324 KB
012.txt AC 26 ms 3964 KB
013.txt AC 35 ms 4732 KB
014.txt AC 41 ms 4860 KB
015.txt AC 42 ms 3964 KB
016.txt AC 39 ms 3836 KB
017.txt AC 32 ms 2812 KB
018.txt AC 23 ms 2172 KB
019.txt AC 12 ms 2300 KB
020.txt AC 3 ms 2172 KB
021.txt AC 3 ms 2172 KB
022.txt AC 3 ms 2172 KB
023.txt AC 3 ms 2172 KB
024.txt AC 3 ms 2172 KB
025.txt AC 3 ms 2172 KB
026.txt AC 3 ms 2172 KB
027.txt AC 3 ms 2172 KB
028.txt AC 3 ms 2172 KB
029.txt AC 3 ms 2172 KB
030.txt AC 3 ms 2172 KB
031.txt AC 3 ms 2044 KB
032.txt AC 3 ms 2300 KB
033.txt AC 3 ms 2812 KB
034.txt AC 4 ms 3708 KB
035.txt AC 6 ms 3324 KB
036.txt AC 8 ms 2300 KB
example0.txt AC 1 ms 256 KB
example1.txt AC 1 ms 256 KB