题意

给定一个长度为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. Linux—软连接与硬连接

    软链接的创建,删除,修改 创建软链接:ln -s[目标文件或目录][软链接地址] 解释:软链接地址相当于快捷方式,目标文件或目录才是真正的内容.[软链接地址]指“快捷键”文件名称,该文件是被指令创建的 ...

  2. Re-py交易

    python在线反编译 https://tool.lu/pyc/ 获得源码 import base64 def encode(message): s = '' for i in message: x ...

  3. python连接MySQL pymysql模块,游标,SQL注入问题,增删改查操作

    pymysql模块 pymysql是用python控制终端对MySQL数据库进行操作的第三方模块 import pymysql # 1.连接数据库 client = pymysql.connect( ...

  4. 关于scanf的一些知识

    10.22,对现阶段已知道的scanf的一些用法或注意事项的一些总结: 1.scanf中,赋值的那个数据前面一定加&! 2.若情景要求必须输入空格的,scanf("%d%c%d&qu ...

  5. Eclipse优化之设置不自动弹出控制台和Server

    有时候Eclipse启动,控制台console不会自动跳出来,需要手工点击该选项卡才行, 按下面的设置,可以让它自动跳出来(或不跳出来): windows  ->   preferences   ...

  6. vscode源码分析【八】加载第一个画面

    第一篇: vscode源码分析[一]从源码运行vscode 第二篇:vscode源码分析[二]程序的启动逻辑,第一个窗口是如何创建的 第三篇:vscode源码分析[三]程序的启动逻辑,性能问题的追踪 ...

  7. yii2关联表

    asArray()这个方法很好用,返回数组是1版本想要的形式,这种方式有种tp框架的感觉

  8. 惊!Python能够检测动态的物体颜色!

    本篇文章将通过图片对比的方法检查视频中的动态物体,并将其中会动的物体定位用cv2矩形框圈出来.本次项目可用于树莓派或者单片机追踪做一些思路参考.寻找动态物体也可以用来监控是否有人进入房间等等场所的监控 ...

  9. EventBus 使用/架构/源码分析

    EventBus是针对Android优化的发布-订阅事件总线,简化了Android组件间的通信.EventBus以其简单易懂.优雅.开销小等优点而备受欢迎. github 地址:https://git ...

  10. EJB组件开发实记(1)

    安装JBoss或者Wildfly jdk1.4以上. Eclipes安装插件 JBoss Tools: eclipes Jee photon 在eclipes 内部点击 >>Windows ...