Submission #4956877


Source Code Expand

Copy
when NimMajor == 0 and NimMinor <= 18: import future
else: import sugar

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import map, mapIt, toSeq
from strutils import split, parseInt, parseFloat, parseBool, parseEnum
when NimMajor == 0 and NimMinor >= 14: from strutils import parseUint

import macros
# headaux macro -- https://stackoverflow.com/questions/31948131/unpack-multiple-variables-from-sequence
# unpack macro  -- https://chy72.hatenablog.com/entry/2017/12/16/214708
macro unpack*(input: array|seq|tuple; count: static[int]): untyped =
  var t = genSym()
  result = quote do: (let `t` = `input`; ())
  when NimMajor == 0 and NimMinor <= 17:
    for i in 0..<count: result[0][1].add quote do: `t`[`i`]
  else:
    for i in 0..<count: result[1].add quote do: `t`[`i`]

template input*(typ: static[typedesc]): seq =
  when typ is char:
    toSeq(stdin.readLine.items)
  elif typ is string:
    stdin.readLine.split
  else:
    stdin.readLine.split.map(`parse typ`)

# 2引数の input() は通常こちらが呼ばれる
template input*(typ: static[typedesc]; count: static[int]): untyped =
  input(typ).unpack(count)

# input(typ, [some integer].Positive) と指定しないとこちらは呼ばれない
template input*(typ: static[typedesc]; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ))

template input*(typ: static[typedesc]; count: static[int] = 1; lineCount: Positive): untyped =
  (1..lineCount).mapIt(input(typ, count))

macro inputs*(body: untyped): untyped =
  result = newNimNode(nnkStmtList)

  var letSection = newNimNode(nnkLetSection)

  var vars = newSeq[NimNode](body.len)
  for i, _ in vars:
    if body[i][0].len > 1: # (var1, var2, ...): int | float | string
      vars[i] = newNimNode(nnkVarTuple)
      for ident in body[i][0]:
        vars[i].add(ident)
      vars[i].add(newEmptyNode())
      vars[i].add(newNimNode(nnkCall)
                    .add(ident("input"))
                    .add(body[i][1][0])
                    .add(newLit(body[i][0].len))
                 )
    elif body[i][1][0].len == 0: # variable: int | float | string
      vars[i] = newIdentDefs(
        body[i][0],
        newEmptyNode(),
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0])
          .add(newLit(1))
      )
    elif body[i][1][0][0].len > 0: # variable: tuple[field1, field2, ...: T]
      var tupleIdents = newNimNode(nnkIdentDefs)
      for ident in body[i][1][0][0]:
        tupleIdents.add(ident)
      let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

      vars[i] = newIdentDefs(
        body[i][0],
        tuplety,
        newNimNode(nnkCall)
          .add(ident("input"))
          .add(body[i][1][0][0][^2])
          .add(newLit(body[i][1][0][0].len - 2))
      )
    elif body[i][1][0][0].len == 0: # seq
      if body[i][1][0].len == 2: # variable: seq[int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0].len == 3 and body[i][1][0][2].len == 0: # variable: seq[N, int | float | string | char]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2])
            .add(newLit(1))
            .add(body[i][1][0][1])
        )
      elif body[i][1][0][2].len == 2: # variable: seq[lineCount, seq[int | float | string | char]]
        vars[i] = newIdentDefs(
          body[i][0],
          newEmptyNode(),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][1])
            .add(newNimNode(nnkDotExpr)
              .add(body[i][1][0][1])
              .add(ident("Positive"))
            )
        )
      else: # variable: seq[lineCount, tuple[field1, field2, ...: T]]
        var tupleIdents = newNimNode(nnkIdentDefs)
        for ident in body[i][1][0][2][0]:
          tupleIdents.add(ident)
        let tuplety = newNimNode(nnkTupleTy).add(tupleIdents)

        vars[i] = newIdentDefs(
          body[i][0],
          newNimNode(nnkBracketExpr).add(body[i][1][0][0]).add(tuplety),
          newNimNode(nnkCall)
            .add(ident("input"))
            .add(body[i][1][0][2][0][^2])
            .add(newLit(body[i][1][0][2][0].len - 2))
            .add(body[i][1][0][1])
        )
    else:
      discard

  for `var` in vars:
    letSection.add(`var`)

  result.add(letSection)

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
proc `//=`*(x: var SomeInteger; y: SomeInteger): SomeInteger {.discardable.} = x = x div y; return x
proc `%=`*(x: var SomeInteger; y: SomeInteger): SomeInteger {.discardable.} = x = x mod y; return x
proc `<?=`*[T](x: var T; y: T): T {.discardable.} = x = min(x, y); return x
proc `>?=`*[T](x: var T; y: T): T {.discardable.} = x = max(x, y); return x

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from sequtils import concat

template flatten*[T](s: seq[T]): untyped =
  when T is seq: flatten(s.concat)
  else: s
proc flatten*[T](s: seq[T]; recLevel: static[Natural]): untyped =
  when recLevel == 0: s
  elif T is seq: flatten(s.concat, recLevel - 1)
  else: s

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
template stderrEcho*(x: varargs[string, `$`]) =
  for v in x:
    stderr.write(v)
  stderr.writeLine ""

template stderrEcho*[T](x: seq[seq[T]]) =
  for v in x: stderrEcho v

template stderrEcho*(x: seq[string]) =
  for v in x: stderrEcho v

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
inputs:
  (N, K): int
  S: string

if K == 0:
  var counts = newSeq[int](N + 1)
  for i, c in S:
    if c == '0':
      counts[i + 1] = 0
    else:
      counts[i + 1] = counts[i] + 1
  echo counts.max
  quit()

var zeroCount = newSeq[int](N + 1)
block:
  var prev = (if S[0] == '1': '0'  else: '1')
  for i, c in S:
    zeroCount[i + 1] = zeroCount[i]
    if c != prev:
      zeroCount[i + 1] += (c == '0').ord
      prev = c
  zeroCount[1] = 0

var
  result = 0
  (l, r) = (1, 1)

while r <= N:
  # stderrEcho l, ", ", r
  if zeroCount[r] - zeroCount[l] > K:
    var flag = true
    while S[l - 1] == '0' or flag:
      if S[l - 1] == '0':
        flag = false
      l.inc
  result >?= r - l + 1
  if zeroCount[r] - zeroCount[l] <= K:
    r.inc

echo result
# stderrEcho zeroCount

Submission Info

Submission Time
Task D - Handstand
User nimon
Language Nim (0.13.0)
Score 400
Code Size 6722 Byte
Status
Exec Time 3 ms
Memory 1536 KB

Compile Error

Hint: system [Processing]
Hint: Main [Processing]
Hint: future [Processing]
Hint: macros [Processing]
Hint: sequtils [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Main.nim(189, 4) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(193, 31) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(195, 13) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(196, 12) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(198, 7) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(199, 18) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Main.nim(200, 31) Warning: 'l' should not be used as an identifier; may look like '1' (one) [SmallLshouldNotBeUsed]
Hint:  [Link]
Hint: operat...

Test Cases

Set Name Score / Max Score Test Cases
All 400 / 400 sample_01, sample_02, sample_03, testcase_01, testcase_02, testcase_03, testcase_04, testcase_05, testcase_06, testcase_07, testcase_08, testcase_09, testcase_10, testcase_11, testcase_12, testcase_13, testcase_14, testcase_15
Sample 0 / 0 sample_01, sample_02, sample_03
Case Name Status Exec Time Memory
sample_01 1 ms 256 KB
sample_02 1 ms 256 KB
sample_03 1 ms 256 KB
testcase_01 1 ms 640 KB
testcase_02 1 ms 640 KB
testcase_03 3 ms 1536 KB
testcase_04 1 ms 256 KB
testcase_05 2 ms 1536 KB
testcase_06 2 ms 1536 KB
testcase_07 2 ms 1536 KB
testcase_08 2 ms 1536 KB
testcase_09 2 ms 1536 KB
testcase_10 2 ms 1536 KB
testcase_11 1 ms 640 KB
testcase_12 1 ms 640 KB
testcase_13 2 ms 1536 KB
testcase_14 2 ms 1536 KB
testcase_15 1 ms 256 KB