fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <fstream>
  5. #include <sstream>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <map>
  9. #define LOCAL
  10. #ifdef LOCAL
  11. #define cin in
  12. #define cout out
  13. #endif
  14.  
  15. #define FOREACH(i, n) for (typeof(n.begin()) i = n.begin(); i != n.end(); ++i)
  16. #define MEMSET(p, c) memset(p, c, sizeof(p))
  17.  
  18. using namespace std;
  19.  
  20. template < class T,class T2 >
  21. T2 fac(T n)
  22. {
  23. return n<2?1:fac<T,T2>(n-1)*n;
  24. }
  25.  
  26. template <class T>
  27. T nb_combinaisons(T k,T n)
  28. {
  29. if(n<k)
  30. return nb_combinaisons<T>(n,k);
  31. return fac<T,long long int>(n)/(fac<T,long long int>(n-k)*fac<T,long long int>(k));
  32. }
  33.  
  34. string itoa_2(long long int n)
  35. {
  36. string s;
  37. stringstream ss;
  38. ss<<n;
  39. ss>>s;
  40. while(s.size()!=9)
  41. s='0'+s;
  42. return s;
  43. }
  44.  
  45.  
  46. int main()
  47. {
  48. #ifdef LOCAL
  49. ifstream in("input.txt");
  50. ofstream out("output.txt");
  51. #endif
  52. int n,k;
  53. cin>>n>>k;
  54. string s;
  55. cin>>s;
  56. sort(s.begin(),s.end());
  57. int mini_global=10000000;
  58. for(int c=0;c<10;c++)
  59. {
  60. int c2;
  61. for(c2=0;c2<s.size()&&s[c2]<c+'0';c2++);
  62. int total=abs(s[c2]-'0'-c);
  63. int bas=c2-1,haut=c2+1;
  64. for(c2=0;c2<k-1;c2++)
  65. {
  66. if(haut>=s.size()||(bas>=0&&abs(s[bas]-'0'-c)<=abs(s[bas]-'0'-c)))
  67. {
  68. total+=abs(s[bas]-'0'-c);
  69. bas--;
  70. }
  71. else
  72. {
  73. total+=abs(s[haut]-'0'-c);
  74. haut++;
  75. }
  76. }
  77. mini_global=min(mini_global,total);
  78. }
  79. cout<<mini_global;
  80. }
  81.  
  82.  
  83.  
  84.  
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int main()’:
prog.cpp:61: warning: comparison between signed and unsigned integer expressions
prog.cpp:66: warning: comparison between signed and unsigned integer expressions
stdout
Standard output is empty