yukicoder contest 243 Participation record

yukicoder contest 243 Participation record

A 1020 Reverse

If you actually try it by hand, you can see that the Kth to Nth are arranged in order, then the 1st to K-1th are arranged in order, and the reverse is arranged. The 1st to K-1th arrangement Of course, it depends on whether the number of operations is odd or even, so it can be solved with the following code.

N, K = map(int, input().split())
S = input()

if (N - K) % 2 == 0:
    print(S[K-1:] + S[:K-1][::-1])
else:
    print(S[K-1:] + S[:K-1])

B 1021 Children in Classrooms

I thought about rewriting the array while moving the pointer and outputting the result based on the pointer information, but it was complicated and my head got confused, so I decided to execute the operation straight with deque. There was nothing difficult with this, and I could solve it with the following code.

from collections import deque

N, M = map(int, input().split())
a = list(map(int, input().split()))
S = input()

q = deque(a)
for c in S:
    if c == 'L':
        t = q.popleft()
        q[0] += t
        q.append(0)
    elif c == 'R':
        t = q.pop()
        q[-1] += t
        q.appendleft(0)
print(*q)

Postscript: I also tried to solve it by moving the pointer. It was complicated.

N, M = map(int, input().split())
a = list(map(int, input().split()))
S = input()

p = 0
for c in S:
    if c == 'L':
        p = max(p - 1, -N + 1)
        if p < 0:
            a[-p] += a[-p - 1]
            a[-p - 1] = 0
    elif c == 'R':
        p = min(p + 1, N - 1)
        if p > 0:
            a[N - 1 - p] += a[N - p]
            a[N - p] = 0

if p > 0:
    print(*([0] * p + a)[:N])
elif p < 0:
    print(*(a + [0] * -p)[-p:N - p])
else:
    print(*a)

Recommended Posts

yukicoder contest 265 Participation record
yukicoder contest 266 Participation record
yukicoder contest 263 Participation record
yukicoder contest 243 Participation record
yukicoder contest 273 Participation record
yukicoder contest 252 Participation record
yukicoder contest 259 Participation record
yukicoder contest 249 Participation record
yukicoder contest 271 Participation record
yukicoder contest 251 Participation record
yukicoder contest 242 Participation record
yukicoder contest 241 Participation record
yukicoder contest 277 Participation record
yukicoder contest 257 Participation record
yukicoder contest 254 Participation record
yukicoder contest 246 Participation record
yukicoder contest 275 Participation record
yukicoder contest 274 Participation record
yukicoder contest 247 Participation record
yukicoder contest 261 Participation record
yukicoder contest 278 Participation record
yukicoder contest 248 Participation record
yukicoder contest 270 (mathematics contest) Participation record
yukicoder contest 272 (Weird math contest) Participation record
yukicoder contest 256 entry record
yukicoder contest 267 entry record
yukicoder contest 264 entry record
yukicoder contest 245 entry record
yukicoder contest 250 entry record
yukicoder contest 262 entry record
yukicoder contest 259 Review
yukicoder contest 264 Review
yukicoder contest 261 Review
yukicoder contest 267 Review
yukicoder contest 266 Review
yukicoder contest 263 Review
yukicoder contest 268 Review
AtCoder Beginner Contest 161 Participation Report
AtCoder Beginner Contest 151 Participation Report
AtCoder Beginner Contest 176 Participation Report
AtCoder Beginner Contest 154 Participation Report
AtCoder Beginner Contest # 003 Participation Note
AtCoder Grand Contest 041 Participation Report
AtCoder Beginner Contest 166 Participation Report
AtCoder Grand Contest 040 Participation Report
AtCoder Beginner Contest 153 Participation Report
AtCoder Beginner Contest 145 Participation Report
AtCoder Beginner Contest 184 Participation Report
AtCoder Beginner Contest 165 Participation Report
AtCoder Beginner Contest 160 Participation Report
AtCoder Beginner Contest 169 Participation Report
AtCoder Beginner Contest 178 Participation Report
AtCoder Beginner Contest 163 Participation Report
AtCoder Beginner Contest 159 Participation Report
AtCoder Beginner Contest 164 Participation Report
AtCoder Regular Contest 105 Participation Report
AtCoder Beginner Contest 168 Participation Report
AtCoder Beginner Contest 150 Participation Report
AtCoder Beginner Contest 158 Participation Report
AtCoder Beginner Contest 180 Participation Report
AtCoder Regular Contest 104 Participation Report