Codeforces Round #632 (Div. 2) D. Challenges in school №41 (模拟&思维)
题意:给定序列求使得转身k轮刚好序列中不再存在转身的序列的解决方案。
思路:
AC代码:
#include<bits/stdc++.h>
using namespace std;
vector<vector<int> >ans;
int a[3050];
char c;
int main(){
int n,k,sum=0;
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>c;
a[i]=(c=='R');//转换为01串,左为0,右为1.
}
while(1){
vector<int>now;
for(int i=1;i<n;i++)
if(a[i]&&!a[i+1]) now.push_back(i),swap(a[i],a[i+1]),i++;
if(now.empty()) break;
sum+=now.size();
ans.push_back(now);
}
if(ans.size()>k||sum<k) puts("-1");
else {
int re=k-ans.size();//re(remain)需要填充的轮次
for(auto now:ans){
while(now.size()>1&&re>0){
printf("1 %d\n",now.back());
now.pop_back();
re--;
}
printf("%d ",now.size());
for(auto i:now) printf("%d ",i);
puts("");
}
}
return 0;
}