Advertisement
MathQ_

Untitled

Nov 22nd, 2020
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. typedef long long ll;
  6. typedef long double lb;
  7.  
  8. #define fast ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  9. #define file_in freopen("input.txt", "r", stdin);
  10. #define file_in_out freopen("knapsack.in", "r", stdin); freopen("knapsack.out", "w", stdout);
  11. #define all(x) (x).begin(), (x).end()
  12.  
  13. using namespace std;
  14.  
  15. int nvp(vector<int> &v) {
  16.     int n = v.size();
  17.     vector<int> dp(n, 1);
  18.     vector<int> t(n + 1, 1);
  19.     t[0] = 0;
  20.     for (int i = 1; i < n; ++i) {
  21.         dp[i] = max(dp[i], t[v[i] - 1] + 1);
  22.         t[v[i]] = dp[i];
  23.     }
  24.     return *max_element(all(dp));
  25. }
  26.  
  27. int main() {
  28.     fast
  29. //  file_in
  30. //  file_in_out
  31.      
  32.     int n, q, a, b;
  33.     cin >> n >> q;
  34.     vector<int> v(n);
  35.     cin >> v;
  36.     cout << n - nvp(v) << endl;
  37.     while (q--) {
  38.         cin >> a >> b;
  39.         swap(v[a - 1], v[b - 1]);
  40.         cout << n - nvp(v) << endl;
  41.     }
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement