Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. const int M = 1000100;
  5. const long long oo =std::numeric_limits<int>::min();
  6. const int PW = 20;
  7. const int md = int(1e9) + 7;
  8. const int N = int(2e6)+1;
  9. int a[1000001];
  10. long long sum[1000001];
  11. long long ans[1000001];
  12. int main()
  13. {
  14.     ios_base::sync_with_stdio(false);
  15.     cin.tie(NULL);
  16.     cout.tie(NULL);
  17.     int n,m,q;
  18.     cin>>n>>m>>q;
  19.     int x,y;
  20.     x=0,y=0;
  21.     long long ans=q;
  22.     for(int i=0;i<q;i++){
  23.         int cl;
  24.         cin>>cl;
  25.         cl--;
  26.         int x1=cl%m,y1=(cl/m);
  27.         ans+=min(abs(n-max(y1,y)-min(y,y1)),abs(y-y1))+ min(abs(m-max(x1,x)-min(x,x1)),abs(x-x1));
  28.         x=x1,y=y1;
  29.     }
  30.     cout<<ans;
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement