Advertisement
Guest User

ядра

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1.  
  2. /*
  3.  * main.cpp
  4.  *
  5.  *  Created on: Mar 18, 2018
  6.  *      Author: vad
  7.  */
  8.  
  9. #pragma comment(linker,"/STACK:64000000")
  10.  
  11. #include <iostream>
  12. #include <fstream>
  13. #include <ctime>
  14.  
  15. std::ifstream inp;
  16. std::ofstream outp;
  17.  
  18. #define cin inp
  19. #ifndef LC_GLOBAL_LOCALE
  20. #define cout outp
  21. #endif
  22.  
  23. void end();
  24. void start(const char *ifname,const char *ofname);
  25.  
  26. using namespace std;
  27.  
  28. ////////////////////////////////////////////
  29. //                START CODE              //
  30. ////////////////////////////////////////////
  31.  
  32. #include <map>
  33. #include <list>
  34.  
  35. typedef long int li;
  36. typedef long long ll;
  37.  
  38. list<li> pirs;
  39. li anses[300001];
  40.  
  41. void init_pirs(){
  42.     li sum = 0;
  43.     li c = 0;
  44.     li x = 0;
  45.     while(sum <= 300000){
  46.         c++;
  47.         x+=c;
  48.         sum+=x;
  49.         pirs.push_back(sum);
  50.     }
  51. }
  52.  
  53. void init_anses(){
  54.     anses[0] = 0;
  55.     for(li x = 1; x <= 300000; x++){
  56.         auto pir = pirs.begin();
  57.         anses[x] = anses[x-*pir]+1;
  58.         while(*pir <= x)
  59.             anses[x]=min(anses[x-*pir]+1, anses[x]),
  60.             pir++;
  61.     }
  62. }
  63.  
  64. int main()
  65. {
  66.     start("king2.in", "king2.out");
  67.  
  68.     init_pirs();
  69.     init_anses();
  70.  
  71.     li m;
  72.     cin >> m;
  73.  
  74.     while(m--)
  75.     {
  76.         li a;
  77.         cin >> a;
  78.         cout << anses[a] << endl;
  79.     }
  80.  
  81.     end();
  82.     return 0;
  83. }
  84.  
  85.  
  86.  
  87.  
  88.  
  89. void start(const char *ifname, const char *ofname){
  90. #ifdef LC_GLOBAL_LOCALE
  91.     inp.open("input.txt");
  92. #else
  93.     inp.open(ifname);
  94.     outp.open(ofname);
  95. #endif
  96. }
  97. void end(){
  98. #ifdef LC_GLOBAL_LOCALE
  99.     cout << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  100. #else
  101.     outp.close();
  102. #endif
  103.     inp.close();
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement