D-Challenges in school №41 (thinking)

Title Portal

Topic: Given a string of length n, given the number of operations k, you can transpose at least one pair of "RL" at a time, and ask if you can convert this string into an inoperable state exactly k times .

Idea: If we convert every time we can convert, then this is the minimum number of times. If we convert one pair at a time, it is the maximum number of times. If the minimum number of times is greater than K or the maximum number of times is less than k, it is illegal, otherwise we Convert each time it can be converted, and record the left side, and finally look at the few and take out a few to fill.

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
const int inf=0x7fffffff;
const int mod=1e9+7;
const double eps=1e-6;
#define ls p<<1
#define rs p<<1|1
#define endl '\n'
#define null NULL
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define ll long long
#define int long long
#define vi vector<int>
#define mii map<int,int>
#define pii pair<int,int>
#define ull unsigned long long
#define pqi priority_queue<int>
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ct cerr<<"Time elapsed:"<<1.0*clock()/CLOCKS_PER_SEC<<"s.\n";
int a[N];
signed main()
{
    IOS;
    int n,k;
    cin>>n>>k;
    string s;
    cin>>s;
    for(int i=0;i<n;i++)
    {
        a[i+1]=(s[i]=='R');
    }
    vector<vi>ans;
    bool fg=1;int sum=0;
    while(fg)
    {
        vi x;fg=0;
        for(int i=1;i<n;i++)
        {
            if(a[i]&&!a[i+1])
            {
                fg=1;
                swap(a[i],a[i+1]);
                sum++;
                x.pb(i);i++;
            }
        }
        if(fg)
            ans.pb(x);
    }
    if(ans.size()>k||sum<k)
    {
        puts("-1");
        return 0;
    }
    int d=k-ans.size();
    for(auto now:ans)
    {
        while(now.size()>1&&d>0)
        {
            printf("1 %d\n",now.back());
            now.pop_back();
            d--;
        }
        printf("%d ",now.size());
        for(auto i:now)
        {
            printf("%d ",i);
        }
        printf("\n");
    }
}

Published 93 original articles · won praise 9 · views 4203

Guess you like

Origin blog.csdn.net/Joker_He/article/details/105431024
41