sultan

1772

Mar 5th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.11 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cstring>
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. #include <algorithm>
  9. #include <queue>
  10. #include <bitset>
  11. #include <stack>
  12. #include <iostream>
  13. #include <fstream>
  14. #include <cmath>
  15.  
  16. #define sqr(a) ((a)*(a))
  17. #define odd(a) ((a)&1)
  18. #define foru(i,n) for (int i=0;i<(n);i++)
  19. #define ford(i,n) for (int i=(n)-1;i>=0;i--)
  20. #define forab(i,l,r) for (int i=(l);i<=(r);i++)
  21. #define forabd(i,r,l) for (int i=(r);i>=(l);i--)
  22. #define pb push_back
  23. #define F first
  24. #define S second
  25. #define all(x) x.begin(),x.end()
  26. #define sz(__X) (int)__X.size()
  27. #define pii pair<long long,long long>
  28. #define pb push_back
  29. #define mp make_pair
  30.  
  31. typedef long long ll;
  32. const double eps=1e-19;
  33. const double PI=acos(-1.0);
  34. const int INF=1000*1000*1000+7;
  35. const int MAXN = 100005;
  36. const ll LINF = 1000000000000000LL;
  37.  
  38. using namespace std;
  39.  
  40. int n,s,k;
  41. pii rob[MAXN];
  42. pii dp[MAXN][2];
  43.  
  44. void best(ll cost, ll korx, int x)
  45. {
  46.     if ( korx<1 || korx>n)
  47.         return;
  48.  
  49.     if ( korx>=rob[x].F && korx<=rob[x].S)
  50.     {
  51.         if (rob[x].F>1 && cost+korx-rob[x].F+1 < dp[x][0].F )
  52.         {
  53.             dp[x][0].S = rob[x].F-1;
  54.             dp[x][0].F = cost +korx-rob[x].F+1;
  55.         }
  56.         if ( rob[x].S<n && cost+rob[x].S-korx+1 < dp[x][1].F )
  57.         {
  58.             dp[x][1].S = rob[x].S+1;
  59.             dp[x][1].F = cost+rob[x].S-korx+1;
  60.         }
  61.     }
  62.     else
  63.     {
  64.         if ( cost < dp[x][0].F)
  65.             dp[x][0] = mp(cost, korx);
  66.         if ( cost < dp[x][1].F)
  67.             dp[x][1] = mp(cost, korx);
  68.     }
  69. }
  70.  
  71. int main()
  72. {
  73.     //freopen("input.txt", "r", stdin);
  74.     //freopen("output.txt", "w", stdout);
  75.     scanf("%d %d %d", &n, &s, &k);
  76.  
  77.     for (int i=1; i<=k; i++)
  78.     {
  79.         cin>>rob[i].F>>rob[i].S;
  80.         dp[i][0] = mp(LINF, INF);
  81.         dp[i][1] = mp(LINF, INF);
  82.     }
  83.  
  84.     dp[0][0].F = 0; dp[0][0].S=s;
  85.     dp[0][1].F = 0; dp[0][1].S=s;
  86.  
  87.     for (int i=1; i<=k; i++)
  88.     {
  89.         best(dp[i-1][0].F, dp[i-1][0].S, i);
  90.         best(dp[i-1][1].F, dp[i-1][1].S, i);
  91.     }
  92.  
  93.     cout<<min(dp[k][0].F, dp[k][1].F);
  94.     return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment