Advertisement
ivnikkk

Untitled

Dec 19th, 2022
721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #define debug(l) cerr<<#l<<' '<<l<<'\n';
  3. #include "bits/stdc++.h"
  4. using namespace std;
  5. #define all(mask) mask.begin(), mask.end()
  6. typedef long long ll;
  7. typedef pair<ll, ll> pll;
  8. typedef pair<int, int> pii;
  9. typedef long double ld;
  10. ld gen() {
  11.     ll x = rand() + 1;
  12.     ll y = rand() + 1;
  13.     x %= y;
  14.     return (ld)x / (ld)y;
  15. }
  16. signed main() {
  17. #ifdef _DEBUG
  18.     freopen("input.txt", "r", stdin);
  19.     freopen("output.txt", "w", stdout);
  20. #endif
  21.     ios_base::sync_with_stdio(false);
  22.     cin.tie(nullptr);
  23.     cout.tie(nullptr);
  24.     srand(time(NULL));
  25.     int n;
  26.     cin >> n;
  27.     int k;
  28.     cin >> k;
  29.     struct event {
  30.         int a, b, c;
  31.     };
  32.     vector<event> a(n);
  33.     for (int i = 0; i < n; i++) {
  34.         cin >> a[i].a >> a[i].b >> a[i].c;
  35.     }
  36.     vector<int> p(n);
  37.     for (int i = 0; i < n; i++) {
  38.         p[i] = i;
  39.     }
  40.     int now_res = 0;
  41.     int best_res = 0;
  42.     vector<int> best_p;
  43.     best_p = p;
  44.     auto count = [&](vector<int>& d) {
  45.         int sum = k;
  46.         int res = 0;
  47.         for (int i = 0; i < n; i++) {
  48.             if (sum >= a[d[i]].a && sum <= a[d[i]].b) {
  49.                 sum += a[d[i]].c;
  50.                 res++;
  51.             }
  52.         }
  53.         return res;
  54.     };
  55.     now_res = best_res = count(p);
  56.     while(1){
  57.  
  58.         ld t0 = 1.0000, q1 = 0.995;
  59.         while(t0>1e-6) {
  60.             if ((double)clock() / (double)CLOCKS_PER_SEC >= 1.8999) {
  61.                 cout << best_res << '\n';
  62.                 int sum = k;
  63.                 int res = 0;
  64.                 vector<int> anse;
  65.                 for (int i = 0; i < n; i++) {
  66.                     if (sum >= a[best_p[i]].a && sum <= a[best_p[i]].b) {
  67.                         sum += a[best_p[i]].c;
  68.                         anse.push_back(best_p[i]);
  69.                     }
  70.                 }
  71.                 for (int& i : anse) {
  72.                     cout << i + 1 << ' ';
  73.                 }
  74.                 return 0;
  75.             }
  76.             vector<int> q = p;
  77.             for (int j = 0; j < n * t0; j++) {
  78.                 int l = rand() % n, r = rand() % n;
  79.                 swap(q[l], q[r]);
  80.             }
  81.             int cnt = count(q);
  82.             if (cnt > best_res) {
  83.                 best_res = cnt;
  84.                 best_p = q;
  85.                 p = q;
  86.             }
  87.             if (gen() < exp((cnt - now_res) / t0)) {
  88.                 now_res = cnt;
  89.                 p = q;
  90.             }
  91.             t0 *= q1;
  92.         }
  93.     }
  94.     cout << best_res << '\n';
  95.     int sum = k;
  96.     int res = 0;
  97.     vector<int> anse;
  98.     for (int i = 0; i < n; i++) {
  99.         if (sum >= a[best_p[i]].a && sum <= a[best_p[i]].b ) {
  100.             sum += a[best_p[i]].c;
  101.             anse.push_back(best_p[i]);
  102.         }
  103.     }
  104.     for (int& i : anse) {
  105.         cout << i + 1<< ' ';
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement