zoukankan      html  css  js  c++  java
  • Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心

    D. Binary String Minimizing

    You are given a binary string of length n (i. e. a string consisting of n characters '0' and '1').

    In one move you can swap two adjacent characters of the string. What is the lexicographically minimum possible string you can obtain from the given one if you can perform no more than k moves? It is possible that you do not perform any moves at all.

    Note that you can swap the same pair of adjacent characters with indices i and i+1 arbitrary (possibly, zero) number of times. Each such swap is considered a separate move.

    You have to answer q independent test cases.

    Input

    The first line of the input contains one integer q (1≤q≤104) — the number of test cases.

    The first line of the test case contains two integers n and k (1≤n≤106,1≤k≤n2) — the length of the string and the number of moves you can perform.

    The second line of the test case contains one string consisting of n characters '0' and '1'.

    It is guaranteed that the sum of n over all test cases does not exceed 106 (∑n≤106).

    Output

    For each test case, print the answer on it: the lexicographically minimum possible string of length n you can obtain from the given one if you can perform no more than k moves.

    Example

    input
    3
    8 5
    11011010
    7 9
    1111100
    7 11
    1111100
    output
    01011110
    0101111
    0011111

    Note

    In the first example, you can change the string as follows: 110–––11010→10–––111010→011110–––10→01110–––110→0110–––1110→01011110.

    In the third example, there are enough operations to make the string sorted.

    题意

    现在有t组数据,每组数据给你n和k;表示二进制的长度和操作次数。
    每次操作你可以选择一个数和他相邻的位置进行交换,在不超过k次的操作次数情况下,使得这个字符串变得最小。

    题解

    贪心,每次操作最小的数,使得他尽可能的放在前面;由于里面只有0和1,其实就是操作0,把0尽可能的往前面放。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    int n;
    long long k;
    string s;
    vector<int>p;
    void solve(){
    	scanf("%d%lld",&n,&k);
    	cin>>s;
    	p.clear();
    	for(int i=0;i<s.size();i++){
    		if(s[i]=='0'){
    			p.push_back(i);
    		}
    	}
    	int la=-1;
    	for(int i=0;i<p.size();i++){
    	   // cout<<"before "<<p[i]<<" "<<k<<endl;
    		if(k>p[i]-la-1){
    			int cost=p[i]-la-1;
    			k-=cost;
    			p[i]=la+1;
    			la=p[i];
    		}else{
    			p[i]-=k;
    			break;
    		}
    		//cout<<"aft "<<p[i]<<" "<<k<<endl;
    	}
    	vector<int>o(s.size(),1);
    	for(int i=0;i<p.size();i++){
    		o[p[i]]=0;
    	}
    	for(int i=0;i<o.size();i++){
    		cout<<o[i];
    	}
    	cout<<endl;
    }
    int main(){
    	int t;
    	scanf("%d",&t);
    	while(t--)solve();
    }
  • 相关阅读:
    Web用户控件
    ASP.Net状态管理读书笔记--思维导图
    网站教学 提纲总结到ajax结束后面还有
    ajax文本空输入显示用户信息
    Ajax 下拉列表联动显示
    用Ajax删除的做法
    Findora:引入保密性和可审计的区块链
    角逐云计算的“新黄金十年”,谁将胜出?
    区块链世界的中心应该是什么?
    边缘计算2.0时代存在哪些挑战?
  • 原文地址:https://www.cnblogs.com/qscqesze/p/11798952.html
Copyright © 2011-2022 走看看