Problem:


  MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Areas on the Cross-Section Diagram

  Your task is to simulate a flood damage.

  For a given cross-section diagram, reports areas of flooded sections.

  Assume that rain is falling endlessly in the region and the water overflowing from the region is falling in the sea at the both sides.
For example, for the above cross-section diagram, the rain will create floods which have areas of 4, 2, 1, 19 and 9 respectively.

Input

  A string, which represents slopes and flatlands by '/', '\' and '_' respectively, is given in a line. For example, the region of the above example is given by a string "\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\".

output

  Report the areas of floods in the following format:

$A$

$k$ $L_1$ $L_2$ ... $L_k$

  In the first line, print the total area $A$ of created floods.

  In the second line, print the number of floods $k$ and areas $L_i (i = 1, 2, ..., k)$ for each flood from the left side of the cross-section diagram. Print a space character before $L_i$.

Constraints

$1 \leq$ length of the string $\leq 20,000$

Sample Input 1

\\//

Sample Output 1

4
1 4

Sample Input 2

\\///\_/\/\\\\/_/\\///__\\\_\\/_\/_/\

Sample Output 2

35
5 4 2 1 19 9



Sample input:
\\//


Sample output:
4
1 4

Submission status: Accepted

Solution code:
data = input()
#data = '\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\'
diff = {'\\':-1, '_':0, '/':1}
height = [0]
[height.append(height[-1]+diff[i]) for i in data]
bottom = min(height)
height = [h-bottom for h in height]

m = max(height)
water = [m for h in height]

height00 = [0] + height + [0]
water00 = [0] + water + [0]

idx = list(range(1,len(water00)-1))
for i in idx + idx[::-1]:
	water00[i] = max(height00[i],min(water00[i-1:i+2]))

water = water00[1:-1]
depth = [w-h for w,h in zip(water,height)]

paddles = [0]
for d1,d2 in zip(depth[:-1],depth[1:]):
	if d1==0 and d2>0:
		paddles.append(0)
	paddles[-1] += min(d1,d2) + 0.5*abs(d1-d2)

paddles = [int(p) for p in paddles[1:]]
print(sum(paddles))
print(len(paddles), *paddles)

Submission status: Accepted

Solution code:
S = input()
st = [(0, 0, -1)]
cur = 0; ma = 0
for i, c in enumerate(S):
    if c == '/':
        cur += 1
        s = 0
        while st and st[-1][0] == cur-1 and cur <= ma:
            lev, su, j = st.pop()
            s += su
        if st and st[-1][0] == cur:
            s += i - st[-1][2] - 1
        ma = max(ma, cur)
        st.append((cur, s, i))
    elif c == '\\':
        cur -= 1
        st.append((cur, 0, i))
    elif c == '_':
        st.append((cur, 0, i))
ans = []
for lev, su, j in st:
    if su > 0:
        ans.append(su)
print(sum(ans))
print(len(ans), *ans)

Submission status: Accepted

Solution code:
ss = input()

S1 = []
S2 = []

a = 0
for ind, s in enumerate(ss):
    if '\\' == s:
        S1.append(ind)
    elif s == '/':
        if len(S1) == 0:
            continue
        i = S1.pop()
        a += ind - i
        t = 0
        while len(S2) > 0 and S2[-1][0] > i-1:
            v = S2.pop()
            t += v[1]
        S2.append((i, ind-i+t))

print(a)
if len(S2) == 0:
    print(0)
else:
    print(len(S2), ' '.join(map(lambda x: str(x[1]), S2)))

Submission status: Accepted

Solution code:
s=j=0
a=[];b=[];d=[]
for i,x in enumerate(input()):
    if x=='\\':
        a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;s+=c
        while b and b[-1]>j:
            c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(s)
print(len(b),*(d))

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
modelmap = list(input())
S1 = []
S2 = []
A = 0

for i, l in enumerate(modelmap):
    if l == "\\":
        S1.append(i)
    elif l == "/" and len(S1) > 0:
        ip = S1.pop()
        A += i - ip
        L = i - ip
        while len(S2) > 0 and S2[-1][0] > ip:
            L += S2.pop()[1]
        S2.append([ip, L])

print(A)
text2 = "{}".format(len(S2))
for s in S2:
    text2 += " " + str(s[1])
print(text2)

Submission status: Accepted

Solution code:
def main():
    """ ????????? """
    S1 = [] # ????????????1(??????????????????)
    S2 = [] # ????????????2(??????????°´????????????????????¨??¢???)
    A = 0 # ?????¢???

    inp = input()
    #inp = "\\\\///\\_/\\/\\\\\\\\/_/\\\\///__\\\\\\_\\\\/_\\/_/\\"
    for i in range(len(inp)):
        if inp[i] == chr(0x5c): # \ 
            S1.append(i)
            S2.append([i,0])
        elif inp[i] == chr(0x2f): # /
            if len(S1):
                j = S1.pop()
                d = i - j
                A += d
                k = d
                while len(S2):
                    p = S2.pop()
                    if p[1] > 0:
                        k += p[1]
                    else:
                        S2.append([p[0],k])
                        break
        #print("[{}] {} A={} S1:{} S2:{}".format(i,inp[i],A,S1,S2))
    L = []
    for p in S2:
        if p[1] > 0:
            L.append(p[1])
    print(A)
    if A > 0:
        print("{} {}".format(len(L)," ".join(map(str,L))))
    else:
        print("0")


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
square = input()
 
down = []
edge = []
pool = []
 
for i, strings in enumerate(square):
    if strings == "\\":
        down.append(i)
    elif down and strings == "/":
        left = down.pop()
        area = i - left
 
        while edge:
            if edge[-1] < left:
                break
            edge.pop()
            area += pool.pop()
 
        edge.append(left)
        pool.append(area)
 
print(sum(pool))
print(len(pool), *pool)

Submission status: Accepted

Solution code:
data = input()
stack = []
lakes = []
for i, X in enumerate(data):
    if X == "\\":
        stack.append(i)
    elif X == "/":
        try:
            j = stack.pop()
        except IndexError:
            continue
        s = 0
        while True:
            if (not lakes) or lakes[-1][1][0] < j:
                break
            s += lakes[-1][0]
            lakes.pop()
        lakes.append([i - j + s, (j, i)])

text = [len(lakes)] + [L[0] for L in lakes]
print(sum(text[1:]))
print(" ".join(list(map(str, text))))

Submission status: Accepted

Solution code:
s = raw_input()

St1, St2 = [], []
sumv = 0

for i in range(len(s)):
    if s[i] == '\\':
        St1.append(i)
    elif s[i] == '/' and len(St1) > 0:
        j = St1.pop()
        a = i-j
        sumv += a
        while len(St2) > 0 and St2[-1][0] > j:
            a += St2.pop()[1]

        St2.append([j, a])

St2.insert(0, [0, len(St2)])

print sumv
print ' '.join(map(lambda x: str(x[1]), St2))

Submission status: Accepted

Solution code:

strSlopeFlat = list(input())

stack_total = []
starck_areas = []

for i,x in enumerate(strSlopeFlat):
    if x == '\\':
        stack_total.append(i)
    elif x == '/' :
        onearea = 0
        if len(stack_total) > 0:
            pos = stack_total.pop()
            onearea = (i-pos)
            while len(starck_areas)>0 and starck_areas[-1][0] > pos:
                tmp_pop = starck_areas.pop()
                onearea += tmp_pop[1]
            starck_areas.append([pos,onearea])

print(sum([y for x,y in starck_areas]))
results = [x[1]  for x in starck_areas if x[1] != 0]
print(len(results),*results)

Submission status: Accepted

Solution code:
s1 = []   #/????????????????????§??????????????????
s2 = []   #??????????°´??????????????¢??????[????????????\???????????????????°´??????????????¢???]?????¢??§????????§??????????????????
su = 0   #?????¢???

st = list(input())   #??\?????????????????????????´????????????????

for i in range(len(st)):   #???????????????????????????????????£????????¢????¨????
    if st[i] == '\\':
        s1.append(i)
    elif st[i] == '/' and len(s1) != 0:
        left = s1.pop()
        su += i - left
        area = 0
        while len(s2) != 0 and left < s2[-1][0]:
            area += s2.pop()[1]
        s2.append([left,area + i -left])

print(su)
print(' '.join([str(len(s2))] + [str(i[1]) for i in s2]))

Submission status: Accepted

Solution code:
s = input()
a = []
down = []
a_sum = 0
for i,line in enumerate(s):
    if line == "\\":
        down.append(i)
    elif line == "/" and down:
        j = down.pop()
        a_tmp = i-j
        a_sum += a_tmp
        while a and a[-1][0] > j:
            a_tmp += a.pop()[1]
        a.append([j,a_tmp])

print(a_sum)
print(len(a),*[s for i,s in a])

Submission status: Accepted

Solution code:
def ALDS1_3D():
    s0, s1 = [], []
    for i, e in enumerate(input()):
        if e == '\\':
                s0.append(i)
        elif e == '/':
            if len(s0) > 0:
                j = s0.pop()
                s = i - j
                while len(s1) > 0 and j < s1[-1][0]:
                    s += s1[-1][1]
                    s1.pop()
                s1.append((j, s))

    ss = [s[1] for s in s1]
    print(sum(ss))
    print(len(ss), *ss)





if __name__ == '__main__':
    ALDS1_3D()

Submission status: Accepted

Solution code:

from collections import deque

def main():
    s1 = deque()
    s2 = deque()
    total = 0

    string = input()

    for i in range(len(string)):
        if string[i] == '\\':
            s1.append(i)
        elif string[i] == '/' and len(s1) > 0:
            j = s1.pop()
            a = i - j
            total += a

            while (len(s2) > 0 and s2[-1][0] > j):
                a += s2[-1][1]
                s2.pop()

            s2.append((j, a))

    ans = []
    while (len(s2) > 0):
        ans.append(s2[-1][1])
        s2.pop()

    print(total)

    print(len(ans), *ans[::-1])

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import sys


def cross_section_diagram(diagram):
    stack = []
    area = []
    for i, ch in enumerate(diagram):
        if ch == "\\":
            stack.append(i)
        elif ch == "/":
            if stack:
                point = stack.pop()
                area.insert(0, (point, i - point))
        else:
            pass
    result = []
    if area:
        p1, cnt1 = area.pop(0)
        for p2, cnt2 in area:
            if p1 < p2:
                cnt1 += cnt2
            else:
                result.insert(0, cnt1)
                p1 = p2
                cnt1 = cnt2
        result.insert(0, cnt1)

    print(sum(result))
    result.insert(0, len(result))
    print(" ".join([str(n)for n in result]))
    return result


if __name__ == "__main__":
    diagram = [val for val in sys.stdin.read().splitlines()]
    cross_section_diagram(diagram[0])

Submission status: Accepted

Solution code:
def main():
    line = input()
    s1 = []
    s2 = []
    sum = 0
    for i, s in enumerate(line):
        if s == "\\":
            # 谷の位置をスタックに積む
            s1.append(i)
        elif s == "/" and len(s1) > 0:
            # 山で対応する谷があるなら、面積に追加
            j = s1.pop()
            sum += i - j  # 谷から山の距離を水たまりの面積に追加
            # jより前に位置する水たまりの面積を追加
            a = i - j
            while len(s2) > 0 and s2[-1][0] > j:
                a += s2.pop()[1]
            # (水たまりの左端の位置, 水たまりの面積)のタプルをスタックに積む
            s2.append((j, a))

    print(sum)
    if len(s2) > 0:
        print(len(s2), end="")
        for x in s2:
            print(f" {x[1]}", end="")
        print()
    else:
        print(len(s2))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    line = input()
    s1 = []
    s2 = []
    sum = 0
    for i, s in enumerate(line):
        if s == "\\":
            # 谷の位置をスタックに積む
            s1.append(i)
        elif s == "/" and len(s1) > 0:
            # 山で対応する谷があるなら、面積に追加
            j = s1.pop()
            sum += i - j  # 谷から山の距離を水たまりの面積に追加
            # jより前に位置する水たまりの面積を追加
            a = i - j
            while len(s2) > 0 and s2[-1][0] > j:
                a += s2.pop()[1]
            # (水たまりの左端の位置, 水たまりの面積)のタプルをスタックに積む
            s2.append((j, a))

    print(sum)
    print(len(s2), end="")
    for x in s2:
        print(f" {x[1]}", end="")
    print()


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

t=input()
s1=[]
s2=[]
pond=[]
totalsum=0

for i in range(len(t)):
    if t[i]=="\\" :
        s1.append(i)
    elif t[i]=="/" and len(s1)!=0:
        p1=s1.pop()
        sum=i-p1
        totalsum+=sum
        while len(s2)>0:
            if s2[-1]>p1:
                s2.pop()
                sum+=pond.pop()
            else:
                break
        pond.append(sum)
        s2.append(i)


print(totalsum)
print(len(pond),*pond)

Submission status: Accepted

Solution code:
li = []
for i, s in enumerate(input()):
    if s == "\\":
        li.append([i, 0])
    elif s == "/":
        if li:
            if li[-1][1] == 0:
                li[-1][1] = i - li[-1][0]
            else:
                for j in range(len(li) - 1, -1, -1):
                    if li[j][1] == 0:
                        li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
                        break
ans = []
for a in li:
    if a[1] != 0:
        ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)

Submission status: Accepted

Solution code:
import sys
readline = sys.stdin.readline
li = []
for i, s in enumerate(readline().strip()):
    if s == "\\":
        li.append([i, 0])
    elif s == "/":
        if li:
            if li[-1][1] == 0:
                li[-1][1] = i - li[-1][0]
            else:
                for j in range(len(li) - 1, -1, -1):
                    if li[j][1] == 0:
                        li = li[:j] + [[li[j][0], sum(tuple(zip(*li[j + 1:]))[1]) + i - li[j][0]]]
                        break
ans = []
for a in li:
    if a[1] != 0:
        ans.append(a[1])
print(sum(ans))
print(len(ans), *ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-


if __name__ == '__main__':

    s1 = []
    s2 = []
    all_area = 0
    
    for i, c in enumerate(input()):
        if c == "\\":
            s1.append([c, i])
        elif c == "/":
            if len(s1) > 0:
                _, j = s1.pop()
                area = i - j
                all_area += area

                while len(s2)>0 and s2[-1][0]>j:
                    area += s2.pop()[1]
                s2.append([j, area])
                
    print(all_area)
    ret = [len(s2)]
    for w in s2:
        ret.append(w[1])
    print(" ".join(map(str, ret)))

Submission status: Accepted

Solution code:
li1 = []
li2 = []
ans = 0
for i, s in enumerate(input()):
    if s == "\\":
        li1.append(i)
    elif s == "/" and li1:
        j = li1.pop()
        c = i - j
        ans += c
        while li2 and li2[-1][0] > j:
            c += li2[-1][1]
            li2.pop()
        li2.append((j, c))
print(ans)
if li2:
    print(len(li2), *list(zip(*li2))[1])
else:
    print(0)

Submission status: Accepted

Solution code:
li1 = []
li2 = []
for i, s in enumerate(input()):
    if s == "\\":
        li1.append(i)
    elif s == "/" and li1:
        j = li1.pop()
        c = i - j
        while li2 and li2[-1][0] > j:
            c += li2[-1][1]
            li2.pop()
        li2.append((j, c))
if li2:
    li3 = list(zip(*li2))[1]
    print(sum(li3))
    print(len(li3), *li3)
else:
    print(0, 0, sep="\n")

Submission status: Accepted

Solution code:
def cross_section_area(data):
    area = 0;
    sections = [];
    stack = [];
    for i in range(len(data)):
        if data[i] == '\\':
            stack.append(i);
        elif data[i] == '/' and len(stack) > 0:
            j = stack.pop();
            section_area = i - j;
            area += section_area;
            while len(sections) > 0 and sections[-1][0] > j:
                section_area += sections[-1][1];
                sections.pop();
            sections.append([j, section_area]);
    print(area);
    print(len(sections), *[n[1] for n in sections]);

cross_section_area(input());

Submission status: Accepted

Solution code:
def check(aline):
    pools = []
    leftedge = []

    for i, c in enumerate(aline):
        if c == "\\":
            leftedge.append(i)
        elif c == "_":
            pass
        else:
            if leftedge:
                left = leftedge.pop()
                w = i - left

                while pools and left < pools[-1][0]:
                    w += pools.pop()[1]

                pools.append((left, w))

    return pools


if __name__ == '__main__':

    al = input().strip()
    ps = check(al)

    print(sum([x[1] for x in ps]))
    print(len(ps), *[x[1] for x in ps])

Submission status: Accepted

Solution code:
s=input()
slash=[]
area=0

slash_2=[]
area_2=[]

for i in range(len(s)):
    if s[i]=="\\":
        slash.append(i)
    elif s[i]=="/":
        try: 
            j=slash.pop()
            area=area+i-j
            a=i-j
            while len(area_2) > 0 and slash_2[len(slash_2)-1] >j:
                a=a+area_2[len(slash_2)-1]
                area_2.pop()
                slash_2.pop() 
            slash_2.append(j)     
            area_2.append(a)
        except: pass


if area==0:
    print(0)
    print(0)
else:
    print(area)
    print(str(len(area_2))+" "+" ".join(map(str, area_2)))

Submission status: Accepted

Solution code:
hoge1 = list()
hoge2 = list()
total = 0
for num, c in enumerate(input()):
    if c == '\\':
        hoge1.append(num)
    elif c == '/':
        if not hoge1:
            continue
        last_index = hoge1.pop()
        S = num - last_index
        if not hoge2:
            hoge2.append((last_index, S))
        else:
            if last_index >= hoge2[-1][0]:
                hoge2.append((last_index, S))
            else:
                new_S = S
                for i, j in hoge2[::-1]:
                    if last_index >= i:
                        break
                    else:
                        new_S += hoge2.pop()[1] 
                hoge2.append((last_index, new_S))

totals = [x[1] for x in hoge2]
print (sum(totals))
totals.insert(0, len(hoge2))
print (' '.join([str(x) for x in totals]))

Submission status: Accepted

Solution code:
s=[]
p=[]
a=i=0
for c in input():
 if"\\"==c:s+=[i]
 elif"/"==c and s:
  j=s.pop()
  t=i-j
  a+=t
  while p and p[-1][0]>j:t+=p[-1][1];p.pop()
  p+=[(j,t)]
 i+=1
print(a)
if p:print(len(p),*list(zip(*p))[1])
else:print(0)

Submission status: Accepted

Solution code:
s=[]
p=[]
a=i=0
for c in input():
 if"\\"==c:s+=[i]
 elif"/"==c and s:
  j=s.pop()
  t=i-j
  a+=t
  while p and p[-1][0]>j:
   t+=p[-1][1]
   p.pop()
  p+=[(j,t)]
 i+=1
print(a)
if p:print(len(p),*list(zip(*p))[1])
else:print(0)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

import sys

class peak():
    def __init__(self, loc, elevation):
        self.loc = loc
        self.elevation = elevation
        self.next_high = None


def peak_append(peak_list, loc, elevation):
    p = peak(loc, elevation)
    peak_list.append(p)
    return

def peak_set_next_high(peak_list):
    next_high = peak_list[-1]   # last peak
    for i in range(len(peak_list)-2, -1, -1):
        peak_list[i].next_high = next_high
        if peak_list[i].elevation >= next_high.elevation:
            next_high = peak_list[i]


def find_flood(elev_list, peak_list):
    peak_p = peak_list.pop(0)
    next_high = peak_p.next_high
    if not next_high:
        return None, None
    q_elev = next_high.elevation
    p_elev = peak_p.elevation

    if p_elev > q_elev:
        q_offset = next_high.loc
        p_offset = peak_p.loc
        while p_offset < len(elev_list):
            if elev_list[p_offset] == elev_list[q_offset]:
                return p_offset, q_offset
            p_offset += 1
        else:
            return None, None
    else:
        p_offset = peak_p.loc
        q_offset = elev_list.index(p_elev, p_offset+1)
        return p_offset, q_offset
    raise "Logic Error"


def calc_area(p, q, elev_list):
    elev_0 = elev_list[p]
    area = 0

    prev_elev = 0
    for x in range(p, q+1):
        elev = elev_list[x] - elev_0
        area += elev

    return (abs(area))


def adjust_peak_list(q, peak_list):
    while peak_list:
        if peak_list[0].loc >= q:
            return
        peak_list.pop(0)


s = list(sys.stdin.readline().strip())

flood_area = list()
peak_list = list()

ascending = True
elevation = 0
elev_list = list()

for i in range(len(s)):
    elev_list.append(elevation)

    if s[i] == '_':
        pass
    elif s[i] == '/':
        ascending = True
        elevation += 1
    elif s[i] == '\\':
        if ascending:
            peak_append(peak_list, i, elevation)
        ascending = False
        elevation -= 1

elev_list.append(elevation)

if ascending:
    peak_append(peak_list, len(s), elevation)

peak_set_next_high(peak_list)

while peak_list:
    p, q = find_flood(elev_list, peak_list)
    if p == None:
        break
    flood_area.append(calc_area(p, q, elev_list))
    adjust_peak_list(q, peak_list)
    
print(sum(flood_area))
print(len(flood_area), end='')
for i in range(len(flood_area)):
    print(' ', flood_area[i], sep='', end='')
print()

Submission status: Accepted

Solution code:
s = input()
i = 0
S1 = []
S2 = []
water = []
wtot = 0
while i < len(s):
    if s[i] == "\\":
        S1.append(i)
    elif s[i] == '/' and len(S1) != 0:
        w0 = (i - S1[-1])
        wtot += w0
        if len(S2) == 0:
            S2.append((S1[-1], w0))
        else:
            while len(S2) != 0 and S2[-1][0] > S1[-1]:
                w0 += S2[-1][1]
                S2.pop()
            S2.append((S1[-1], w0))
        S1.pop()
    i += 1

ans = str(wtot) + '\n' + str(len(S2))
for p in S2:
    ans += ' ' + str(p[1])
print(ans)

Submission status: Accepted

Solution code:
def find_lakes(diagram):
    """Find lakes in diagram.

    >>> pat = str.maketrans('ufd', '/_\\\\')
    >>> find_lakes('ddfuu'.translate(pat))
    [6]
    >>> find_lakes('dduuudfududdddufudduuuffdddfddufdufud'.translate(pat))
    [4, 2, 1, 19, 9]
    >>> find_lakes('ff'.translate(pat))
    []
    """
    def calc_areas(ls):
        areas = []
        e1, e2 = 0, 0
        for n, m in reversed(ls):
            if e1 < n and e2 > m:
                area = areas.pop()
                areas.append(area + (m - n))
            else:
                areas.append(m - n)
                e1, e2 = n, m
        return areas[::-1]

    lakes = []
    slopes = []

    for i, c in enumerate(diagram):
        if c == '\\':
            slopes.append(i)
        elif c == '/':
            if len(slopes) > 0:
                lakes.append((slopes.pop(), i))

    return calc_areas(lakes)


def run():
    ls = find_lakes(input())

    summary = [str(len(ls))] + [str(i) for i in ls]
    print(sum(ls))
    print(" ".join(summary))


if __name__ == '__main__':
    run()

Submission status: Accepted

Solution code:
string = input()
height = 0
height_list = [[0, False]]
for s in string:
    if s == '\\':
        height += -1
    elif s == '/':
        height += 1
    else:
        pass
    height_list.append([height, False])

highest = 0
for i in range(1, len(height_list)):
    if height_list[i-1][0] < height_list[i][0] <= highest:
        height_list[i][1] = True
    highest = max(highest, height_list[i][0])

puddles = []
area = 0
surface_level = None

for i in range(len(height_list)-1, -1, -1):
    if surface_level != None:
        area += surface_level - height_list[i][0]
        if surface_level == height_list[i][0]:
            puddles += [area]
            surface_level = None
            area = 0
    if surface_level == None and height_list[i][1]:
        surface_level = height_list[i][0]

puddles = puddles[::-1]
print(sum(puddles))
print(len(puddles), *puddles)

Submission status: Accepted

Solution code:
s=j=0
a=[];
b=[];
d=[];
for i,x in enumerate(input()):
    if x=='\\':a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;s+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(s)
print(len(b),*(d))

Submission status: Accepted

Solution code:
from collections import deque
s = input()
g = deque()
p = deque()
for i in range(len(s)):
    if s[i] == "\\":
        g.append(i)
    elif s[i] == "/":
        a = 0
        if len(g) > 0:
            while True:
                if len(p) > 0 and p[-1][0] > g[-1]:
                    a += p[-1][1]
                    p.pop()
                else:
                    break
            a += i - g[-1]
            p.append([g[-1], a])
            g.pop()
for i in range(len(p)):
    p[i] = p[i][1]
if len(p) > 0:
    print(sum(p))
    print(len(p), end = " ")
    print(" ".join(map(str, p)))
else:
    print(0)
    print(0)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 29 23:10:10 2018

@author: maezawa
"""

s = input()
n = len(s)

st = []
st2 = []
total_sum = 0

for i, c in enumerate(s):
    if c == '\\': # Back Slashをそのまま使うため二重にした。
        st.insert(0,i)
    elif c == '/' and len(st)>0: 
        j = st.pop(0)
        total_sum += i - j
        a = i - j
        while len(st2) > 0 and st2[0][0] > j:
            a += st2.pop(0)[1]
        st2.insert(0, [j, a])
        
print(total_sum)
print(len(st2), end='')
for i in reversed(range(len(st2))):
    print(" {}".format(st2[i][1]), end='')
print()

Submission status: Accepted

Solution code:
stack = []
res = []

area = input()

for i, terrain in enumerate(area):
    if terrain == '\\':
        stack.append(i)
    elif terrain == '/' and len(stack)>0:
        left = stack.pop()
        pond = i - left
        if len(res) == 0 or left >= res[-1][0]:
            res.append([left,pond])
        else:
            while len(res) > 0 and left < res[-1][0] :
                pond += res[-1][1]
                res.pop()
            res.append([left,pond])

total = 0
ponds = []
for i in res:
    ponds.append(i[1])
    total += i[1]

ans = [len(res)]
ans += ponds
ans_new = " ".join(map(str,ans))

print(total)
print(ans_new)

Submission status: Accepted

Solution code:
cross_section = input()

stack = []
areas_stack = []
area = 0
for i in range(len(cross_section)):
    c = cross_section[i]
    if c == '\\':
        stack.append(i)
    elif c == '/' and len(stack) != 0:
        j = stack.pop()
        a = i - j
        area += a
        while len(areas_stack) != 0 and areas_stack[-1][0] > j:
            a += areas_stack.pop()[1]
        areas_stack.append([j, a])

print(area)
if area == 0:
    print(0)
else:
    print(len(areas_stack), ' '.join([str(x[1]) for x in areas_stack]))

Submission status: Accepted

Solution code:
class Stack:
    def __init__(self, full_idx=10):
        self.stack = [None] * full_idx
        self.top = 0
        self.MAX = full_idx

    def is_empty(self):
        return self.top == 0

    def is_full(self):
        return self.top >= self.MAX - 1

    def push(self, x):
        if self.is_full():
            raise IndexError

        self.top += 1
        self.stack[self.top] = x

    def pop(self):
        if self.is_empty():
            raise IndexError

        self.top -= 1
        return self.stack[self.top+1]


def main():
    s = input()
    s1, s2 = Stack(20000), Stack(20000)
    pond = [0]
    area = 0

    for i in range(len(s)):
        if s[i] == "\\":
            s1.push(i)
            s2.push([i, 0])

        elif s[i] == "/":
            if not s1.is_empty():
                area += i - s1.pop()
            if not s2.is_empty():
                tup = s2.pop()
                area2 = 0

                while tup[1] != 0:
                    tup2 = s2.pop()
                    tup, tup2 = tup2, tup
                    area2 += tup2[1]

                tup[1] = i - tup[0] + area2

                if s2.is_empty():
                    pond.append(tup[1])
                else:
                    s2.push(tup)

    s3 = Stack(20000)
    while not s2.is_empty():
        ss = s2.pop()
        if ss[1] != 0:
            s3.push(ss)

    while not s3.is_empty():
        pond.append(s3.pop()[1])

    pond[0] = len(pond) - 1
    print(area)
    print(" ".join([str(i) for i in pond]))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
A=[]
p=[]
a=i=0
for c in input():
 if"\\"==c:A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while p and p[-1][0]>j:t+=p[-1][1];p.pop()
  p+=[(j,t)]
 i+=1
print(a)
if p:print(len(p),*list(zip(*p))[1])
else:print(0)

Submission status: Accepted

Solution code:
A=[]
B=[]
a=i=0
for c in input():
 if"\\"==c:A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while B and B[-1][0]>j:t+=B[-1][1];B.pop()
  B+=[(j,t)]
 i+=1
print(a)
if B:print(len(B),*list(zip(*B))[1])
else:print(0)

Submission status: Accepted

Solution code:
A=[]
B=[]
a=0
i=0
for c in input():
 if"\\"==c:
  A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while B and B[-1][0]>j:t+=B[-1][1];B.pop()
  B+=[(j,t)]
 i+=1
print(a)
if B:print(len(B),*list(zip(*B))[1])
else:print(0)

Submission status: Accepted

Solution code:
A=[]
B=[]
a=0
i=0
for c in input():
 if"\\"==c:
  A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while B and B[-1][0]>j:
   t+=B[-1][1];B.pop()
  B+=[(j,t)]
 i+=1
print(a)
if B:print(len(B),*list(zip(*B))[1])
else:print(0)

Submission status: Accepted

Solution code:
A=[]
B=[]
a=0
i=0
for c in input():
 if"\\"==c:
  A+=[i]
 elif"/"==c and A:
  j=A.pop()
  t=i-j
  a+=t
  while B and B[-1][0]>j:
   t+=B[-1][1];B.pop()
  B+=[(j,t)]
 i+=1
print(a)
if B:
 print(len(B),*list(zip(*B))[1])
else:
 print(0)

Submission status: Accepted

Solution code:
k=j=0
a=[];b=[];d=[]
for i,x in enumerate(input()):
    if x=='\\':a+=[i]
    elif x=='/' and a:
        j=a.pop()
        c=i-j;k+=c
        while b and b[-1]>j:c+=d.pop();b.pop()
        b+=[j];d+=[c]
print(k)
print(len(b),*(d))

Submission status: Accepted

Solution code:
inputs = list(input())
stack = []
L = []
tmp_total = 0
counter = 0
upper = 0
for i,x in enumerate(inputs):
    if(x == '\\'):
        stack.append(i)
    elif(x == '/') and stack:
        tmp = stack.pop()
        total_layer = i-tmp
        if L:
            while L and L[-1][0] > tmp:
                cal = L.pop()
                total_layer += cal[1]
        L.append((i, total_layer))

print(sum([j[1] for j in L]))
if(len(L) != 0):
    print(str(len(L))+ ' ' + ' '.join([str(i[1]) for i in L]))
else:
    print(str(len(L)))

Submission status: Accepted

Solution code:
ary = list(input())
s1 = []
s = 0

s2 = []

cnt = 0
for _ in ary:

    if _ == '\\':
        s1.append(cnt)
        cnt += 1

    elif _ == '/':
        if s1:
            old_cnt = s1.pop()
            area = cnt - old_cnt
            s += area

            while s2:
                if old_cnt < s2[-1][0]:
                    area += s2.pop()[1]
                else:
                    break

            s2.append((old_cnt, area))

        cnt += 1

    else:
        cnt += 1

print(s)
print(len(s2), *[_[1] for _ in s2])

Submission status: Accepted

Solution code:
# ALDS1_3_D.

def show(a):
    # 配列の中身を出力する。
    _str = ""
    for i in range(len(a) - 1):
        _str += str(a[i]) + " "
    _str += str(a[len(a) - 1])
    print(_str)

def main():
    data = list(input())
    a = []  # '\\'の位置を格納していく。
    b = []  # 同じだが、池の集約に使う。
    ponds = []  # 池の面積のリスト(必要に応じて集約).
    Area = 0  # 総面積
    N = len(data)
    for i in range(N):
        if data[i] == '\\':
            a.append(i); b.append(i)
        elif data[i] == '/':
            if len(a) == 0: continue
            else:
                left = a.pop()  # '/'と対になる'\\'の位置
                Area += i - left
                ponds.append(i - left)
                # leftより右にあるbの元を先頭から見てあればカット、
                # その分だけpondsを集約する。
                while left < b[len(b) - 1]:
                    b.pop()
                    x = ponds.pop(); y = ponds.pop(); ponds.append(x + y)
    print(Area)
    ponds.insert(0, len(ponds))
    show(ponds)
    
if __name__ == "__main__":
    main()