Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdio>
- #include <cstdlib>
- #include <cstring>
- #include <string>
- #include <map>
- #include <set>
- #include <vector>
- #include <algorithm>
- #include <queue>
- #include <bitset>
- #include <stack>
- #include <iostream>
- #include <fstream>
- #include <cmath>
- #define sqr(a) ((a)*(a))
- #define odd(a) ((a)&1)
- #define foru(i,n) for (int i=0;i<(n);i++)
- #define ford(i,n) for (int i=(n)-1;i>=0;i--)
- #define forab(i,l,r) for (int i=(l);i<=(r);i++)
- #define forabd(i,r,l) for (int i=(r);i>=(l);i--)
- #define pb push_back
- #define F first
- #define S second
- #define all(x) x.begin(),x.end()
- #define sz(__X) (int)__X.size()
- #define pii pair<long long,long long>
- #define pb push_back
- #define mp make_pair
- typedef long long ll;
- const double eps=1e-19;
- const double PI=acos(-1.0);
- const int INF=1000*1000*1000+7;
- const int MAXN = 100005;
- const ll LINF = 1000000000000000LL;
- using namespace std;
- int n,s,k;
- pii rob[MAXN];
- pii dp[MAXN][2];
- void best(ll cost, ll korx, int x)
- {
- if ( korx<1 || korx>n)
- return;
- if ( korx>=rob[x].F && korx<=rob[x].S)
- {
- if (rob[x].F>1 && cost+korx-rob[x].F+1 < dp[x][0].F )
- {
- dp[x][0].S = rob[x].F-1;
- dp[x][0].F = cost +korx-rob[x].F+1;
- }
- if ( rob[x].S<n && cost+rob[x].S-korx+1 < dp[x][1].F )
- {
- dp[x][1].S = rob[x].S+1;
- dp[x][1].F = cost+rob[x].S-korx+1;
- }
- }
- else
- {
- if ( cost < dp[x][0].F)
- dp[x][0] = mp(cost, korx);
- if ( cost < dp[x][1].F)
- dp[x][1] = mp(cost, korx);
- }
- }
- int main()
- {
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- scanf("%d %d %d", &n, &s, &k);
- for (int i=1; i<=k; i++)
- {
- cin>>rob[i].F>>rob[i].S;
- dp[i][0] = mp(LINF, INF);
- dp[i][1] = mp(LINF, INF);
- }
- dp[0][0].F = 0; dp[0][0].S=s;
- dp[0][1].F = 0; dp[0][1].S=s;
- for (int i=1; i<=k; i++)
- {
- best(dp[i-1][0].F, dp[i-1][0].S, i);
- best(dp[i-1][1].F, dp[i-1][1].S, i);
- }
- cout<<min(dp[k][0].F, dp[k][1].F);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment