题意

给定一个长度为n的二进制串(即由n个'0'和'1'构成的字符串),你最多可以进行k次交换相邻两个字符的操作,求字典序最小的串。

思路

大致就是找0的位置,然后贪心的放到最前面,这样字典序会最小:

代码

我的丑做法:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=200005;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
ll n,k;
cin>>n>>k;
string s;
cin>>s;
int l=s.length();
queue<int> q;
for(int i=0; i<l; i++)
{
if(s[i]=='0')
{
q.push(i);
}
}
ll sum=0,tmp=0,cnt=0,lst=0,flag=0;
while(!q.empty())
{
int f=q.front();
sum+=(f-tmp);
if(sum>k)
{
sum-=(f-tmp);
flag=1;
break;
}
lst=f;
tmp++;
cnt++;
q.pop();
}
// cout<<sum<<" "<<lst<<" "<<cnt<<endl;
if(!flag)
{
for(int i=0;i<cnt;i++)
cout<<0;
for(int i=0;i<n-cnt;i++)
cout<<1;
cout<<endl;
continue;
}
// cout<<cnt<<endl;
string ans;
for(int i=0; i<cnt; i++)
{
ans+="0";
}
for(int i=0; i<lst-cnt+1; i++)
ans+="1";
int num=lst-cnt+1+cnt;
// cout<<"h:"<<num<<endl;
for(int i=num; i<l; i++)
{
num++;
ans+=s[i];
if(s[i]=='0')
{
break;
}
}
// cout<<"g:"<<num<<endl;
for(int i=0; i<k-sum; i++)
{
swap(ans[num-i-1],ans[num-i-2]);
}
for(int i=num; i<l; i++)
ans+=s[i];
cout<<ans<<endl;
}
return 0;
}

  简单做法(太强了):

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int N=1e6+5;
const int mod=1e9+7;
const double eps=1e-8;
const double PI = acos(-1.0);
#define lowbit(x) (x&(-x))
char s[N];
int main()
{
std::ios::sync_with_stdio(false);
int t;
ll n,k;
cin>>t;
while(t--)
{
cin>>n>>k;
cin>>s+1;
ll l=strlen(s+1),cnt=0;
for(ll i=1;i<=l;i++)
{
if(s[i]=='0')
{
cnt++;
if(k>i-cnt)
{
swap(s[i],s[cnt]);
k-=(i-cnt);
}
else
{
swap(s[i],s[i-k]);
break;
}
}
}
cout<<s+1<<endl;
}
return 0;
}

CodeForces - 1256D (贪心+思维)的更多相关文章

  1. Codeforces Round #546 (Div. 2) D 贪心 + 思维

    https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...

  2. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  3. Mike and distribution CodeForces - 798D (贪心+思维)

    题目链接 TAG: 这是我近期做过最棒的一道贪心思维题,不容易想到,想到就出乎意料. 题意:给定两个含有N个正整数的数组a和b,让你输出一个数字k ,要求k不大于n/2+1,并且输出k个整数,范围为1 ...

  4. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  5. T - Posterized(贪心思维)

    Description Professor Ibrahim has prepared the final homework for his algorithm’s class. He asked hi ...

  6. codeforces 233 C. Cycles(贪心+思维)

    题目链接:http://codeforces.com/contest/233/problem/C 题意:在一个无相图中有N个长度为3 的回路,输出符合条件的图.注意此图的节点数不得超过100 题解:贪 ...

  7. codeforces 798 D. Mike and distribution(贪心+思维)

    题目链接:http://codeforces.com/contest/798/problem/D 题意:给出两串长度为n的数组a,b,然后要求长度小于等于n/2+1的p数组是的以p为下表a1-ap的和 ...

  8. codeforces 798 C. Mike and gcd problem(贪心+思维+数论)

    题目链接:http://codeforces.com/contest/798/problem/C 题意:给出一串数字,问如果这串数字的gcd大于1,如果不是那么有这样的操作,删除ai, ai + 1 ...

  9. codeforces 893D Credit Card 贪心 思维

    codeforces 893D Credit Card 题目大意: 有一张信用卡可以使用,每天白天都可以去给卡充钱.到了晚上,进入银行对卡的操作时间,操作有三种: 1.\(a_i>0\) 银行会 ...

随机推荐

  1. KumuluzEE - Java EE的微服务框架

    KumuluzEE - Java EE的微服务架构 https://www.jdon.com/soa/kumuluzEE.html

  2. CodeForces 1238C(思维+贪心)

    题意 https://vjudge.net/problem/CodeForces-1238C 您现在正在玩一个游戏,您初始在一个高度 h 的悬崖 悬崖沿壁高度为 1-h 的这些位置均有平台,平台有两种 ...

  3. 201871010116-祁英红《面向对象程序设计(java)》第十三周学习总结

    博文正文开头格式:(2分) 项目 内容 <面向对象程序设计(java)> https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://ww ...

  4. linux 库文件配置

    linux 库文件配置 /etc/ld.so.conf 或 /etc/ld.so.conf.d/*.conf

  5. LeetCode 5129. 下降路径最小和 II Minimum Falling Path Sum II

    地址 https://leetcode-cn.com/contest/biweekly-contest-15/problems/minimum-falling-path-sum-ii/ 题目描述给你一 ...

  6. QT debug执行exe文件 应用程序无法正常启动0xc000007b

    遇到这种错,发现并不是因为缺失dll文件,因为我把需要的DLL都放到Debug文件下了,但还是有这问题: 解决方法: 右键点击-- >我的电脑--属性-->高级系统设置-->环境变量 ...

  7. Windows10 下利用Hyper-V安装CentOS系统

    开启Windows10的Hyper-v功能(需要重启电脑) 控制面板→程序→启用或关闭Windows功能→打开Hyper-v→确定 创建虚拟机 在Windows管理工具中找到Hyper-v管理器并双击 ...

  8. 关闭Chrome浏览器的广告

    生活没有绝对的对与错:代码就不一样了,错了就编译不过,也正是因为这样,编程的人思维有时也会陷入一种狭隘中,这就是把工作和生活没有分开.Win10 右下角的广告就像程序调试中的"警告" ...

  9. Codeforces Round #594 (Div. 1)

    Preface 这场CF真是细节多的爆炸,B,C,F都是大细节题,每道题都写了好久的说 CSP前的打的最后一场比赛了吧,瞬间凉意满满 希望CSP可以狗住冬令营啊(再狗不住真没了) A. Ivan th ...

  10. 《细说PHP》第四版 样章 第23章 自定义PHP接口规范 11

    23.6  使用第三方接口服务实例 接供服务的第三方接口平台有很多,现在的项目中也经常用到一些第三方接口,如支付宝.微信.短信.邮件接口等,我们需要借助第三方的能力来实现产品的某些功能.如果自己已经掌 ...