Advertisement
Guest User

Untitled

a guest
Jan 12th, 2023
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long int
  3. #define vi vector<ll>
  4. using namespace std;
  5.  
  6. int main() {
  7. int t;
  8. cin>>t;
  9. while(t--){
  10. ll n,m,k;
  11. cin>>n>>m>>k;
  12. if(k==0){
  13. ll ans=n+m-2;
  14. cout<<ans<<endl;
  15. continue;
  16. }
  17. vector<vector<ll>> v(k,vector<ll>(2));
  18. for(ll i=0;i<k;i++) cin>>v[i][0]>>v[i][1];
  19. sort(v.begin(),v.end());
  20. // for(auto i:v){
  21. // cout<<i[1]<<" ";
  22. // }
  23. // cout<<endl;
  24. vector<vi> arr;
  25. for(ll i=0;i<k;i++){
  26. if(v[i][0]!=n && v[i][1]!=m){
  27. arr.push_back(v[i]);
  28. }
  29. }
  30. // for(auto i:arr){
  31. // cout<<i[0]<<" "<<i[1]<<endl;
  32. // }
  33. ll l=arr.size();
  34. vector<ll> dp(l+1,LLONG_MAX);
  35. dp[0]=0;
  36. ll res=0;
  37. for(ll i=0;i<l;){
  38. ll z1=i;
  39. vector<vi> temp;
  40. while(i<l && arr[i][0]==arr[z1][0]){
  41. ll idx=upper_bound(dp.begin(),dp.end(),arr[i][1])-dp.begin();
  42. if(idx!=l+1 && dp[idx-1]<arr[i][1]){
  43. temp.push_back({idx,arr[i][1]});
  44. res=max(res,idx);
  45. }
  46. i++;
  47. }
  48.  
  49. for(auto j:temp){
  50. // cout<<j[0]<<" "<<j[1]<<endl;
  51. dp[j[0]]=min(dp[j[0]],j[1]);
  52. }
  53. }
  54. ll ans=n+m-2-res;
  55. cout<<ans<<endl;
  56. }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement