Challenges in school №41

There are n children, who study at the school №41. It is well-known that they are good mathematicians. Once at a break, they arranged a challenge for themselves. All children arranged in a row and turned heads either to the left or to the right.

Children can do the following: in one second several pairs of neighboring children who are looking at each other can simultaneously turn the head in the opposite direction. For instance, the one who was looking at the right neighbor turns left and vice versa for the second child. Moreover, every second at least one pair of neighboring children performs such action. They are going to finish when there is no pair of neighboring children who are looking at each other.

You are given the number n, the initial arrangement of children and the number k. You have to find a way for the children to act if they want to finish the process in exactly k seconds. More formally, for each of the k moves, you need to output the numbers of the children who turn left during this move.

For instance, for the configuration shown below and k=2 children can do the following steps:
在这里插入图片描述

At the beginning, two pairs make move: (1,2) and (3,4). After that, we receive the following configuration:
在这里插入图片描述
At the second move pair (2,3) makes the move. The final configuration is reached. Good job.
在这里插入图片描述

It is guaranteed that if the solution exists, it takes not more than n2 “headturns”.

Input
The first line of input contains two integers n and k (2≤n≤3000, 1≤k≤3000000) — the number of children and required number of moves.

The next line contains a string of length n and consists only of characters L and R, where L means that the child looks to the left and R means that the child looks to the right.

Output
If there is no solution, print a single line with number −1.

Otherwise, output k lines. Each line has to start with a number ni (1≤ni≤n2) — the number of pairs of children, who turn at this move. After that print ni distinct integers — the numbers of the children who will turn left during this move.

After performing all “headturns”, there can’t be a pair of two neighboring children looking at each other.

If there are many solutions, print any of them.

Examples
inputCopy
2 1
RL
outputCopy
1 1
inputCopy
2 1
LR
outputCopy
-1
inputCopy
4 2
RLRL
outputCopy
2 1 3
1 2
Note
The first sample contains a pair of children who look at each other. After one move, they can finish the process.

In the second sample, children can’t make any move. As a result, they can’t end in k>0 moves.

The third configuration is described in the statement.

造了一组样例,如下
8 10
RRRLLRLL
 
字母数组脚标    12345678
原始数据        RRRLLRLL
第1次整体移动    RRLRLLRL记录移动的个体步骤(3,4),(6,7)        个体移动次数2次
第2次整体移动    RLRLRLLR记录移动的个体步骤(2,3),(4,5),(7,8)  个体移动次数3次
第3次整体移动    LRLRLRLR记录移动的个体步骤(1,2),(3,4),(5,6)  个体移动次数3次
第4次整体移动    LLRLRLRR记录移动的个体步骤(2,3),(4,5),(6,7)  个体移动次数3次
第5次整体移动    LLLRLRRR记录移动的个体步骤(3,4),(5,6)        个体移动次数2次
第6次整体移动    LLLLRRRR记录移动的个体步骤(4,5)              个体移动次数1次
 
可以看到最少移动次数6次
最多移动次数2+3+3+3+3+2+1=14次
 
k=10可这样操作
1 3
1 6
1 2
1 4
1 7
1 1
2 3 5
3 2 4 6
2 3 5
1 4

上述操作明白之后,结合代码便能ac这道题。

#include"bits/stdc++.h"
using namespace std;
int n,k;
char s[3010];
int a[3010][3010];
int x[3010];
int mn,mx;
int judge()
{
	for(int i=1;i<n;i++)
	{
		if(s[i]=='R'&&s[i+1]=='L')
		{
			return 1;
		}
	}
	return 0;
}
void move()
{
	int r=0;
	mn++;
	for(int i=1;i<n;i++)
	{
		if(s[i]=='R'&&s[i+1]=='L')
		{
			mx++;
			r++;
			a[mn][r]=i;
		}
	}
	for(int i=1;i<=r;i++)
	{
		swap(s[a[mn][i]],s[a[mn][i]+1]);
	}
	x[mn]=r;
}
int main()
{	
	scanf("%d %d %s",&n,&k,s+1);
	int l,o;
	//scanf("%s",s+1);
	while(judge())
	{
		move();
	}
	if(k<mn||k>mx)
	{
		printf("-1\n");
		return 0;
	}
	k-=mn;
	for(int i=1;i<=mn;i++)
	{
		l=1,o=x[i];
		while(k&&l<o)
		{
			printf("1 %d\n",a[i][l]);
			k--,l++;
		}
		printf("%d",o-l+1);
		for(int j=l;j<=o;j++)
		{
			printf(" %d",a[i][j]);
		}
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值