思路:

枚举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. 程序以html形式发送邮件注意问题

    1.样式要写在内部,不要单独定义样式 2.div 使用背景图片在有些浏览器中出不来这时候需要用: <table style="width: 640px" cellpaddin ...

  2. webpack 教程资源目录

    初级教程 webpack-howto 作者:Pete HuntWebpack 入门指迷 作者:题叶 webpack-demos 作者:ruanyf一小时包教会 —— webpack 入门指南 作者:V ...

  3. Unity3D之Mesh(三)绘制四边形

    前言: 由於之前的基本介紹,所以有關的知識點不做贅述,只上案例,知識作爲自己做試驗的記錄,便於日後查看. 步驟: 1.創建一個empty 的gameobject: 2.添加一個脚本給這個game ob ...

  4. JavaUtil_02_二维码的生成与解析

    1.引入jar包 zxing-core-1.7.jar  :   http://viralpatel.net/blogs/download/jar/zxing-core-1.7.jar zxing-j ...

  5. 【leetcode刷题笔记】Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  6. php断点续传

    http://www.cnblogs.com/xproer/archive/2012/10/26/2741264.html

  7. bzoj1002轮状病毒

    高精度练习题 根据什么什么基尔霍夫矩阵 反正就是高精度练习 #include<iostream> #include<cstdio> using namespace std; s ...

  8. 【JVM】java棧

    java棧和函数调用的关系图 [名词解释]--->java棧是一块线程的私有空间--->java的棧是先进后出的数据结构.函数返回,则该函数的棧帧被弹出.--->一个函数对应一个棧帧 ...

  9. hdu 5269 ZYB loves Xor I 分治 || Trie

    题目大意: 长度为\(n\)的数组A.求对于所有数对\((i,j)(i \in [1,n],j \in [1,n])\),\(lowbit(A_i xor A_j)\)之和.答案对998244353取 ...

  10. 【LeetCode】014. Longest Common Prefix

    Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 ...