IMohammedNasr

G. Playing With Rocks

Sep 24th, 2022
687
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define cin(v) for (auto &i : v) cin >> i;
  4. #define cout(v) for (auto &i : v) cout << i << " "; cout<<'\n';
  5. #define ll long long
  6. #define all(v) v.begin(), v.end()
  7. #define rall(v) v.rbegin(), v.rend()
  8. #define MOD 1000000007
  9. #define Time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs"<< "\n";
  10.  
  11. void Warding()
  12. {
  13.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  14. #ifndef ONLINE_JUDGE
  15.     freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  16. #endif
  17. }
  18.  
  19. /*
  20. STOP USING int  
  21. */
  22.  
  23. ll dmg;
  24.  
  25. ll rec(ll target,ll pos, ll curr_idx, ll sec, vector<pair<int, pair<int,int>>> &v){
  26.     ll hit = 0;
  27.     if(pos == target) return 0;
  28.     if(sec > 10) return target - pos;
  29.     if(curr_idx < v.size() && v[curr_idx].first == sec){
  30.         while(curr_idx < v.size() && v[curr_idx].first == sec){
  31.             hit += (pos >= v[curr_idx].second.first && pos <= v[curr_idx].second.second);
  32.             curr_idx++;
  33.         }
  34.     }
  35.     hit *= dmg;
  36.     ll step_back = 1 + hit + rec(target, pos - 1, curr_idx, sec + 1, v);
  37.     ll step_front = 1 + hit + rec(target, pos + 1, curr_idx, sec + 1, v);
  38.     ll same_pos = 1 + hit + rec(target, pos, curr_idx, sec + 1, v);
  39.     return min({step_back, step_front, same_pos});
  40. }
  41.  
  42.  
  43. void solve(){
  44.     ll n, x, target; cin>>n>>x>>target>>dmg;
  45.     vector<pair<int, pair<int,int>>> v(n);
  46.     for(int i=0; i<n; i++){
  47.         cin>>v[i].second.first>>v[i].second.second>>v[i].first;
  48.     }
  49.     sort(all(v));
  50.     // for(auto&i:v) cout<<i.second.first<<" "<<i.second.second<<" "<<i.first<<endl;
  51.     cout<<rec(target, x, 0ll,0ll, v)<<endl;
  52. }
  53.  
  54. int main()
  55. {
  56.     Warding();
  57.     int TC = 1;
  58.     // cin >> TC;
  59.     while (TC--)
  60.     {
  61.         solve();
  62.     }
  63.     Time
  64. }
Advertisement
Add Comment
Please, Sign In to add comment