Advertisement
Guest User

Untitled

a guest
Nov 1st, 2023
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <fstream>
  4. #include <vector>
  5. #include <numeric>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <cmath>
  10. #include <stack>
  11. #include <deque>
  12. #include <string>
  13. #include <ctime>
  14. #include <bitset>
  15. #include <queue>
  16. #include <cassert>
  17. #include<unordered_set>
  18. #include<unordered_map>
  19. #include<string.h>
  20. #include <random>
  21. #include <chrono>
  22. #pragma GCC optimize("O3,unroll-loops")
  23. #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
  24. #include <math.h>
  25. //#include <bit>
  26. #include <climits>
  27. using namespace std;
  28. #define int long long
  29. #define ll long long
  30. #define ld double
  31. #define pi pair<int, int>
  32. #define pll pair<ll, ll>
  33. #define vi vector<int>
  34. #define ull unsigned long long
  35. #define pb push_back
  36. #define newl '\n'
  37. #define all(a) (a).begin(), (a).end()
  38. #define rall(a) (a).rbegin(), (a).rend()
  39. #define repeat(a) for (int i31 = 0; i31 < a; ++i31)
  40. template<typename T>
  41. istream& operator>>(istream& is, vector<T>& v) {
  42. for (T& o : v) {
  43. is >> o;
  44. }
  45. return is;
  46. }
  47. class Query {
  48. public:
  49. int l = 0;
  50. int r = 0;
  51. int idx = 0;
  52. Query(int L, int R, int Num) {
  53. l = L;
  54. r = R;
  55. idx = Num;
  56. }
  57. };
  58. int NOD(int a, int b) {
  59. if (a < 0 || b < 0) return NOD(abs(a), abs(b));
  60. return b ? NOD(b, a % b) : a;
  61. }
  62. //const ld EPS = 1e-9;
  63. const ll INF = 1e16 + 3;
  64. const ll MOD1 = 1000000007;
  65. const ll POWER = 5;
  66. const ll HASHMOD1 = 939999971;
  67. const ll HASHMOD2 = 23;
  68. const ll MOD2 = 998244353;
  69. //const ld PHI = atan(1) * 4;
  70. const ll MOD3 = 1e9 + 7;
  71. const int root = 31;
  72. const int root_1 = 128805723;
  73. const int root_pw = 1 << 23;
  74. vector<int> dX = { 1, 0, -1, 0, };
  75. vector<int> dY = { 0, 1, 0, -1 };
  76. mt19937 MT(chrono::steady_clock::now().time_since_epoch().count());
  77. ifstream fin("aha.txt");
  78. const int LOG = 19;
  79. const int PR = 67;
  80. const int mM = 305;
  81. const int block_size = 100;
  82. const int SSIZE = 1001;
  83. const int mod = 998244353;
  84. const ld eps = 1e-8;
  85. const int iter = 150;
  86. const int maxN = 10000;
  87.  
  88.  
  89. vector<vector<int>> dp;
  90. void advance(deque<pi>& dd, int pos, int val) {
  91. while (!dd.empty() && dd.back().first > val) dd.pop_back();
  92. dd.push_back({ val, pos });
  93. }
  94. void redo(deque<pi>& dd, int l) { // < l -> nahuy
  95. while (!dd.empty() && dd.front().second < l) dd.pop_front();
  96. }
  97.  
  98. void solve() {
  99. int N = 0, K = 0, T = 0; cin >> N >> K >> T;
  100. vector<int> a; a.assign(N, 0);
  101. for (auto& x : a) cin >> x;
  102. vector<int> huy;
  103. vector<int> pizda;
  104. for (int i = 0; i < N; ++i) {
  105. if (a[i] >= 0) huy.push_back(a[i]);
  106. else pizda.push_back(a[i]);
  107. }
  108. int N1 = huy.size(); int N2 = pizda.size();
  109. sort(pizda.begin(), pizda.end());
  110. reverse(pizda.begin(), pizda.end());
  111. sort(huy.begin(), huy.end());
  112. dp.assign(N1 + 1, vector<int>(N2 + 1, INF));
  113. dp[0][0] = 0;
  114. vi suf1; vi suf2;
  115. suf1.assign(N1 + 2, INF); suf2.assign(N2 + 1, INF);
  116. suf1[0] = suf2[0] = 0;
  117. vector<int> chosen_mn;
  118. vector<deque<pi>> hahahahhaa;
  119. hahahahhaa.assign(N + 1, deque<pi>());
  120. chosen_mn.assign(2 * N + 1, INF);
  121. vector<deque<pi>> have_to_do;
  122. have_to_do.assign(2 * N + 1, deque<pi>());
  123. have_to_do[0].push_back({ 0, 0 });
  124. for (int i = 0; i < N1; ++i) {
  125. int pos_take = T - 2 * abs(huy[i]); pos_take /= K;
  126. int pr = max(0ll, i + 1 - pos_take); suf1[i + 1] = suf1[pr] + 1;
  127. dp[i + 1][0] = suf1[i + 1];
  128. }
  129. for (int i = 0; i < N2; ++i) {
  130. int pos_take = T - 2 * abs(pizda[i]); pos_take /= K;
  131. int pr = max(0ll, i + 1 - pos_take); suf2[i + 1] = suf2[pr] + 1;
  132. dp[0][i + 1] = suf2[i + 1];
  133. have_to_do[i + 1].push_back({ dp[0][i + 1], 0 });
  134. }
  135. for (int ps1 = 1; ps1 <= N1; ++ps1) {
  136. have_to_do[ps1].push_back({ dp[ps1][0], ps1});
  137. for (int ps2 = 1; ps2 <= N2; ++ps2) {
  138. int fir_poss = (T - 2 * huy[ps1 - 1]); fir_poss /= K;
  139. dp[ps1][ps2] = min(dp[ps1][ps2], dp[max(0ll, ps1 - fir_poss)][ps2] + 1);
  140. int sec_poss = (T - 2 * abs(pizda[ps2 - 1])); sec_poss /= K;
  141. dp[ps1][ps2] = min(dp[ps1][ps2], dp[ps1][max(0ll, ps2 - sec_poss)] + 1);
  142. int lft = T - 2 * huy[ps1 - 1] + 2 * pizda[ps2 - 1];
  143. if (lft >= 2 * K) {
  144. int prs = lft / K;
  145. if (ps1 + ps2 <= prs) {
  146. dp[ps1][ps2] = 1;
  147. }
  148. else {
  149. int dval = ps1 + ps2 - prs;
  150. while (!have_to_do[dval].empty() && have_to_do[dval].front().second <= ps1 - 1) {
  151. advance(hahahahhaa[dval], have_to_do[dval].front().second, have_to_do[dval].front().first);
  152. have_to_do[dval].pop_front();
  153. }
  154. redo(hahahahhaa[dval], ps1 - prs + 1);
  155. if (!hahahahhaa[dval].empty()) dp[ps1][ps2] = min(dp[ps1][ps2], hahahahhaa[dval].front().first + 1);
  156. }
  157. //dp[ps1][ps2] = min(dp[ps1][ps2], chosen_mn[max(0ll, ps1 + ps2 - prs)] + 1);
  158. // m : diag_val + prs - m < ps2 -> ps1 + ps2 - prs + prs - m < ps2
  159. //на диагонали нужен максимум на отрезке [ps1 + 1; ps1 + ps2 - prs - (ps2)] -> [ps1 - prs + 1; ps1 - 1] // [l; r]
  160. }
  161. have_to_do[ps1 + ps2].push_back({ dp[ps1][ps2], ps1 });
  162. chosen_mn[ps1 + ps2] = min(chosen_mn[ps1 + ps2], dp[ps1][ps2]);
  163. }
  164. }
  165. cout << dp[N1][N2] << "\n";
  166. }
  167.  
  168. signed main() {
  169. ios_base::sync_with_stdio(false);
  170. cin.tie(0);
  171. cout.tie(0);
  172. cout << setprecision(20);
  173. int T = 1;
  174. while (T--)
  175. solve();
  176. return 0;
  177. }
  178. /*
  179. 6
  180. 1 2
  181. 2 3
  182. 3 4
  183. 4 5
  184. 5 6
  185. 3
  186. 1 2
  187. 3 4
  188. 1 4
  189. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement