CodeForces-1333D Challenges in school №41


传送门:CodeForces-1333D

思路分析

先暴力统计出最大和最小的操作天数。如果K在这个区间内就有解,然后将答案拆成K天就行了

样例输入

4 2
RLRL

样例输出

2 1 3 
1 2

AC代码

#include <functional>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdio>
#include <chrono>
#include <random>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#define ls x<<1
#define rs x<<1|1
#define fi first
#define se second
#define ll long long
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) x.size()
#define lowbit(x) x&(-x)
#define all(x) x.begin(),x.end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0);

using namespace std;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int INF = 0x3f3f3f3f;
const int N=3000010;

vector<int>ans[N];
vector<int>a[N];

char s[3030];

int main() {
#ifdef xiaofan
    freopen("1.in","r",stdin);
    freopen("1.out","w",stdout);
#endif

    int n,k;
    cin>>n>>k;
    cin>>s+1;
    int cnt=0,tot=0;
    while(1) {
        int flag=0;
        cnt++;
        for(int i=1; i<=n; i++)
            if(s[i]=='R'&&s[i+1]=='L') {
                flag=1;
                tot++;
                swap(s[i],s[i+1]);
                a[cnt].push_back(i);
                i++;
            }
        if(!flag) {
            cnt--;
            break;
        }
        
    }
    if(k<cnt||k>tot){
        puts("-1");
        return 0;
    }
    int now=1;
    for(int i=1;i<=cnt;i++){
        while(!a[i].empty() && k>cnt-i+1){
            printf("1 %d\n",a[i].back());
            a[i].pop_back();
            k--;
        }
        if(!a[i].empty()){
            printf("%d ",a[i].size());
            for(auto x:a[i]) printf("%d ",x);
            puts("");
            k--;
        }
    }




    return 0;
}
&nbsp;


文章作者: 小凡
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 小凡 !
评论
  目录
隐藏
{% if theme.sakura.enable %}{% endif %}