日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Binary String Minimizing CodeForces - 1256D(贪心)

發布時間:2023/12/15 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Binary String Minimizing CodeForces - 1256D(贪心) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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.
思路:思路挺好想的一道題,但是一不留心就出錯。。
在規定的移動次數內,我們先在前面找出一個1,然后再其后面找出一個0.然后(盡可能的)交換位置。具體看代碼
代碼如下:

#include<bits/stdc++.h> #define ll long long using namespace std;string s; int n; ll k;int main() {int t;scanf("%d",&t);while(t--){scanf("%d%lld",&n,&k);cin>>s;int l=0,r;while(l<n&&s[l]=='0') l++;r=l+1;while(r<n&&s[r]=='1') r++;//起始位置一定要提前找出來,否則會出錯或者超時。while(k){while(l<n&&s[l]=='0') l++;while(r<n&&s[r]=='1') r++;if(l>=n||r>=n) break;if(k>=(r-l)){swap(s[l],s[r]);k-=(ll)(r-l);l++,r++;}else{swap(s[r-k],s[r]);//這就是盡可能的交換。k=0;}}cout<<s<<endl;}return 0; }

努力加油a啊,(o)/~

總結

以上是生活随笔為你收集整理的Binary String Minimizing CodeForces - 1256D(贪心)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。