brute force constructive algorithms games graphs greedy implementation sortings *2100

Please click on ads to support us..

Python Code:

def main():
    
    n, k = readIntArr()
    a = list(input())
    
    batch = []
    for i in range(n - 1):
        if a[i] == 'R' and a[i + 1] == 'L':
            batch.append(i)
    allmoves = []
    while len(batch) > 0:
        allmoves.append(batch)
        for i in batch:
            a[i], a[i + 1] = a[i + 1], a[i]
        
        batch2 = set()
        for i in batch:
            i -= 1
            if i >= 0 and a[i] == 'R' and a[i + 1] == 'L':
                batch2.add(i)
            i += 2
            if i + 1 < n and a[i] == 'R' and a[i + 1] == 'L':
                batch2.add(i)
        batch = list(batch2)
    min_moves = len(allmoves)
    max_moves = sum([len(moves) for moves in allmoves])
    if not min_moves <= k <= max_moves:
        print(-1)
        return
    
        allans = []
    before = 0
    after = len(allmoves)
    for moves in allmoves:
        after -= 1
        l = len(moves)
        nmoves = min(l, k - before - after)
        for i in range(nmoves - 1):
            allans.append((1, moves[i] + 1))
            before += 1
        arr = [len(moves) - (nmoves - 1)]
        for i in range(nmoves - 1, len(moves)):
            arr.append(moves[i] + 1)
        before += 1
        allans.append(tuple(arr))
        if len(allans) > 1000000:
            multiLineArrayOfArraysPrint(allans)
            allans = []
    if allans:
        multiLineArrayOfArraysPrint(allans)
    
    return

import sys
input=lambda: sys.stdin.readline().rstrip("\r\n")  
def oneLineArrayPrint(arr):
    print(' '.join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
    print('\n'.join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
    print('\n'.join([' '.join([str(x) for x in y]) for y in arr]))
 
def readIntArr():
    return [int(x) for x in input().split()]
 
def makeArr(defaultValFactory,dimensionArr):     dv=defaultValFactory;da=dimensionArr
    if len(da)==1:return [dv() for _ in range(da[0])]
    else:return [makeArr(dv,da[1:]) for _ in range(da[0])]
 
def queryInteractive(a, b):
    print('? {} {}'.format(a, b))
    sys.stdout.flush()
    return int(input())
 
def answerInteractive(ans):
    print('! {}'.format(ans))
    sys.stdout.flush()
 
inf=float('inf')
 
from math import gcd,floor,ceil
import math
 
for _abc in range(1):
    main()

C++ Code:

#include <bits/stdc++.h>

using namespace std;

int n, k;

vector<int> find_steps(const vector<int>& a) {
    vector<int> steps;
    for (int i = 0; i < n - 1; ++i) {
        if (a[i] == 1 && a[i + 1] == 0) steps.push_back(i);
    }
    return steps;
}


int main() {
    cin >> n >> k;
    string s; cin >> s;
    vector<int> a(n);
    for (int i = 0; i < n; ++i) a[i] = (s[i] == 'L') ? 0 : 1;
    int maxi = 0, mini = 0;
    int cnt = 0;
    int last = -1;
    for (int i = n - 1; i >= 0; --i) {
        if (a[i] == 0) {
            ++cnt;
        } else {
            if (cnt == 0) continue;
            maxi += cnt;
            mini = max(cnt, last + 1);
            last = mini;
        }
    }
    if (k < mini || k > maxi) {
        cout << -1;
        return 0;
    }
    bool is_min = false;
    vector<int> have_step;
    for (int i = 0; i < k; ++i) {
        if (!is_min) {
            auto steps = find_steps(a);
            cout << min(int(steps.size()), maxi - k + i + 1) << ' ';
            int cur = 0;
            while (k - i - 1 < maxi && cur < steps.size()) {
                cout << steps[cur] + 1 << ' ';
                a[steps[cur]] = 0;
                a[steps[cur] + 1] = 1;
                ++cur;
                --maxi;
            }
            if (maxi == k - i - 1) {
                is_min = true;
                have_step = find_steps(a);
            }
        } else {
            int v = have_step.back();
            have_step.pop_back();
            cout << "1 " << v + 1;
            a[v] = 0;
            a[v + 1] = 1;
            if (v > 0 && a[v - 1] == 1) {
                have_step.push_back(v - 1);
            }
            if (v + 2 < n && a[v + 2] == 0) {
                have_step.push_back(v + 1);
            }
        }
        cout << '\n';
    }
}


Comments

Submit
0 Comments
More Questions

174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes