思路:

枚举0-9之间的数,然后判断。

然后一鼓作气打成了大模拟。。。。我日啊。。。

心疼自己。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL; const int N=1e4+10;
int n,k;
char s[N],ss[N];
int num[19];
int cnt[19],len;
int all[N][15]; void solve(int x)
{
int sum;
for(int i=0;i<len;i++)
{
int e=s[i]-'0';
if(cnt[e])
{
if(e>x)//5->3
{
cnt[e]--;
ss[i]=x+'0';
}
else//3->5
{
if((i-1>=0&&(all[len-1][e]-all[i-1][e])<=cnt[e])||(i==0&&all[len-1][e]<=cnt[e]))
{
ss[i]=x+'0';
cnt[e]--;
}
else
ss[i]=s[i];
}
}
else
ss[i]=s[i];
}
ss[len]='\0';
} char sss[N];
void solve2(int x)
{
int sum;
for(int i=0;i<len;i++)
{
int e=s[i]-'0';
if(cnt[e])
{
if(e>x)//5->3
{
cnt[e]--;
sss[i]=x+'0';
}
else//3->5
{
if((i-1>=0&&(all[len-1][e]-all[i-1][e])<=cnt[e])||(i==0&&all[len-1][e]<=cnt[e]))
{
sss[i]=x+'0';
cnt[e]--;
}
else
sss[i]=s[i];
}
}
else
sss[i]=s[i];
}
sss[len]='\0';
} int main()
{
scanf("%d%d",&n,&k);
scanf("%s",s);
len=strlen(s);
memset(all,0,sizeof(all));
memset(num,0,sizeof(num));
for(int i=0;i<len;i++)
{
int x=s[i]-'0';
if(i==0)
all[i][x]=1;
else
{
for(int kk=0;kk<=9;kk++){
if(kk==x)
all[i][x]=all[i-1][x]+1;
else
all[i][kk]=all[i-1][kk];
}
}
num[x]++;
} int ans=0x3f3f3f3f;
for(int i=0;i<=9;i++)
{
// printf("%d\n",num[i]);
int res=0;
int sum=num[i];
int t=1;
memset(cnt,0,sizeof(cnt));
if(sum>=k)
{
ans=0;
strcpy(ss,s);
break;
}
while(sum<k&&(i+t<=9||i-t>=0))
{
if(i+t<=9)
{
if(num[i+t]+sum>=k)
{
cnt[i+t]=k-sum;
res+=(k-sum)*t;
if(ans>res)
{
solve(i);
ans=res;
//printf("%d %d\n",i,ans);
}
else if(ans==res)
{
solve2(i);
if(strcmp(ss,sss)>0)
strcpy(ss,sss);
}
sum=k;
}
else
{
res+=num[i+t]*t;
sum+=num[i+t];
cnt[i+t]=num[i+t];
}
}
if(i-t>=0)
{
if(num[i-t]+sum>=k)
{
cnt[i-t]=k-sum;
res+=(k-sum)*t;
if(ans>res)
{
solve(i);
ans=res;
//printf("%d %d\n",i,ans);
}
// else if(ans==res)
// {
// solve2(i);
// if(strcmp(ss,sss)>0)
// strcpy(ss,sss);
// }
sum=k;
}
else
{
res+=num[i-t]*t;
sum+=num[i-t];
cnt[i-t]=num[i-t];
}
}
t++;
}
}
printf("%d\n",ans);
printf("%s\n",ss);
return 0;
}
/*
16 14
6124258626539246 22
4124248424439244
*/

CodeForces 118C 【模拟】的更多相关文章

  1. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  2. CodeForces - 404B(模拟题)

    Marathon Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Sta ...

  3. Codeforces 709B 模拟

    B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...

  4. CodeForces - 404A(模拟题)

    Valera and X Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  5. Codeforces 390A( 模拟题)

    Inna and Alarm Clock Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64 ...

  6. Codeforces 452D [模拟][贪心]

    题意: 给你k件衣服处理,告诉你洗衣机烘干机折叠机的数量,和它们处理一件衣服的时间,要求一件衣服在洗完之后必须立刻烘干,烘干之后必须立刻折叠,问所需的最小时间. 思路: 1.按照时间模拟 2.若洗完的 ...

  7. CodeForces - 796B 模拟

    思路:模拟移动即可,如果球落入洞中停止移动.注意:有可能第一个位置就是洞!! AC代码 #include <cstdio> #include <cmath> #include ...

  8. CodeForces - 864C-Bus-(模拟加油站问题)

    https://vjudge.net/problem/CodeForces-864C 题意:两地之间有个加油站,往返走k个单程,最少加油多少次. 大佬几十行代码就解决,我却要用一百多行的if语句模拟解 ...

  9. Codeforces 709C 模拟

    C. Letters Cyclic Shift time limit per test:1 second memory limit per test:256 megabytes input:stand ...

随机推荐

  1. 解决xhost: unable to open display ""

    首先安装vncserver,如图: 切换账户:sudo su到root下 执行:export DISPLAY=:0.0 执行:xhost +,如图:

  2. java.sql.SQLException: Column count doesn't match value count at row 1 Query: insert into category values(null,?,?,?) Parameters: [1111111, 1111, 软件]

    java.sql.SQLException 问题: java.sql.SQLException: Column count doesn't match value count at row 1 Que ...

  3. Hibernate学习---第十节:Hibernate之QBC、样例查询&离线查询

    一.QBC (Query By Criteria) 主要有Criteria,Criterion,Oder,Restrictions类组成 1.java 代码如下: /** * 查询所有 */ @Tes ...

  4. Selenium-一组元素的定位

    一组元素的定位: 有时候我们可能需要定位一组元素,比如一组checkbox,这时候要实现的思路大概为: 先把一组元素识别出来,然后定位你需要的元素 下面是查找多个元素(这些方法将返回一个列表): 方法 ...

  5. json-lib的一些过滤操作

    package demo4; import java.io.Serializable; import net.sf.json.JSONString; public class User impleme ...

  6. 数据库连接池(connection pool)

    1.JDBC数据库连接池的必要性  在使用开发基于数据库的web程序时,传统的模式基本是按以下步骤: –      在主程序(如servlet.beans)中建立数据库连接. –      进行sql ...

  7. [JSOI 2015] 最大公约数

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4488 [算法] 不妨首先枚举左端点 注意到对于任意一个正整数n , 其质因子个数是l ...

  8. VC++6.0注释快捷键的添加使用

    在eclipse等编辑工具中都有注释代码的快捷键,但是vc++6.0没有. vc++是以VB为脚本来控制的,在网上找到了一个VB的脚本供大家使用.   工具/原料 VC++6.0 方法/步骤 打开VC ...

  9. 洛谷【P1104】生日(插入排序版)

    题目传送门:https://www.luogu.org/problemnew/show/P1104 题目很简单,我主要是来讲插入排序的. 所谓插入排序,就是从待排序数组不断将数据插入答案数组里. 假设 ...

  10. bzoj 1927 星际竞速 —— 最小费用最大流

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1927 首先注意到这是个DAG: 考虑每个点从哪里来,可以是瞬移来的,也可以是从某个点走过来的 ...