Guest User

Untitled

a guest
Nov 16th, 2019
3,331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct Antenna
  7. {
  8.     int iniLeft, iniRight;
  9. };
  10.  
  11. int main()
  12. {
  13.     ios::sync_with_stdio(false);
  14.     cin.tie(0);
  15.  
  16.     int nbAntennas, totLen;
  17.     cin >> nbAntennas >> totLen;
  18.     vector<Antenna> ants(nbAntennas);
  19.  
  20.     for (int iAnt = 0; iAnt < nbAntennas; ++iAnt) {
  21.         int center, iniScope;
  22.         cin >> center >> iniScope;
  23.         ants[iAnt].iniLeft = max(0, center - iniScope);
  24.         ants[iAnt].iniRight = min(totLen, center + iniScope);
  25.     }
  26.  
  27.     vector<int> minCost(totLen+1);
  28.     minCost[totLen] = 0;
  29.  
  30.     for (int pos = totLen-1; pos >= 0; --pos) {
  31.         minCost[pos] = (totLen - pos);
  32.  
  33.         for (int iAnt = 0; iAnt < nbAntennas; ++iAnt) {
  34.             int left = ants[iAnt].iniLeft, right = ants[iAnt].iniRight;
  35.  
  36.             if (left <= pos+1 && pos+1 <= right) {
  37.                 minCost[pos] = minCost[pos+1];
  38.                 break;
  39.             }
  40.            
  41.             if (pos < left) {
  42.                 int accessCost = (left - pos - 1);
  43.                 int nextPos = min(totLen, right + accessCost);
  44.                 minCost[pos] = min(minCost[pos], accessCost + minCost[nextPos]);
  45.             }
  46.         }
  47.     }
  48.  
  49.     cout << minCost[0] << "\n";
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment