Advertisement
smatskevich

Seminar7

Oct 31st, 2022 (edited)
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4.  
  5. int W = 100;
  6. std::vector<std::vector<int>> E{{4, 3}, {}};
  7.  
  8. int main1() {
  9.   const char* s = "UGATU.ПГНИУ.";
  10.   std::cout << strlen(s) << std::endl;
  11.   const char* t = "PSU";
  12.   char* s2 = const_cast<char *>(s);
  13.   //s2[2] = 'U';
  14.  
  15.   for (int i = 0; i < 50; ++i) {
  16.     std::cout << s[i] << ' ';
  17.   }
  18.  
  19.   int a;
  20.   int* v = new int[100000000];
  21.   std::cout << s << std::endl;
  22.   return 0;
  23. }
  24.  
  25. int main2() {
  26.   const char* s = "Hi\r\nHello\nPrivetPrivetPrivetPrivet\rHo\n";
  27.   std::cout << s << "UGATU and PSU\n";
  28.   std::cout << "Tab\tTa\tT\tTab" << std::endl;
  29.   std::cout << "Backspace\bBacksp\bBack\bBa\b\b\b\b" << std::endl;
  30.   std::cout << "Unicode: \u1245 \u2665" << std::endl;
  31.   std::cout << "Кавычки: \"\'" << std::endl;
  32.   std::cout << std::endl;
  33.  
  34.   const char* source = "Hello, PSU and UGATU!";
  35.   char* s2 = new char[100];
  36.   int i = 0;
  37.   do {
  38.     s2[i] = source[i];
  39.   } while(source[i++]);
  40.  
  41.   //s2[8] = 0;
  42.   std::cout << s2;
  43.  
  44.   return 0;
  45. }
  46.  
  47. // Disk [        [Seminar7.exe код стат.данные WE]   ..
  48.  
  49. // ВАП [---------[SeMInar7.exe КОд стАт.данные WE]   [Stack----]  [Куча [v100mb]---] [Явное выделение через АПИ ---]
  50.  
  51. // RAM [[Stack----]] [S-MI----.--- КО- --А-.------ WE]  [v100mb]
  52.  
  53. int main3() {
  54.   std::string x = "abra";
  55.   const char* s = x.c_str();
  56.   std::cout << s << std::endl;
  57.  
  58.   // UB, но работает
  59.   std::cout << x[0] << x[1] << x[2] << x[3] << x[4] << std::endl;
  60.   std::cout << sizeof(x) << std::endl;
  61.   return 0;
  62. }
  63.  
  64. bool comparator(const std::string &a, const std::string &b) {
  65.   auto cur_a = a.begin();
  66.   auto cur_b = b.begin();
  67.  
  68.   while (cur_a != a.end() && cur_b != b.end())
  69.     if (*(cur_a++) != *(cur_b++))
  70.       return *(--cur_a) < *(--cur_b);
  71.  
  72.   return cur_b != b.end();
  73. }
  74.  
  75. // ab\0
  76. // abdfsl
  77.  
  78. int main4() {
  79.   std::string a, b;
  80.   std::cin >> a >> b;
  81.   std::cout << (comparator(a, b) ? "YES" : "NO") << std::endl;
  82.   return 0;
  83. }
  84.  
  85. // Возвращает индекс, где разместится опорный элемент. [l, r)
  86. int Partition(std::vector<int>& v, int l, int r) {
  87.   if (r - l <= 1) return 0;
  88.   const int& pivot = v[r - 1];
  89.   int i = l, j = r - 2;
  90.   while (i <= j) {
  91.     // Не проверяем, что i < n - 1, т.к. a[n - 1] == pivot.
  92.     for (; v[i] < pivot; ++i ) {}
  93.     for (; j >= l && !(v[j] < pivot); --j) {}
  94.     if (i < j) std::swap(v[i++], v[j--]);
  95.   }
  96.   std::swap(v[i], v[r - 1]);
  97.   return i;
  98. }
  99.  
  100. // xxxxxPxxxxKxPxx
  101. int KStat(std::vector<int>& v, int k) {
  102.   int left = 0, right = v.size();
  103.   while (true) {
  104.     int pivot = Partition(v, left, right);
  105.     if (pivot == k) return v[pivot];
  106.     if (pivot < k) {
  107.       left = pivot + 1;
  108.     } else {
  109.       right = pivot;
  110.     }
  111.   }
  112. }
  113.  
  114. int main() {
  115.   std::vector<int> v{4, 6, 12, 5, 33, 4, 7, 15, 3};
  116.   std::cout << KStat(v, v.size() / 2) << std::endl;
  117.   return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement