Alex_tz307

USACO 2017 February Contest, Gold Problem 2. Why Did the Cow Cross the Road II

Jul 26th, 2021 (edited)
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.69 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. ifstream fin("nocross.in");
  6. ofstream fout("nocross.out");
  7.  
  8. void max_self(short &x, short y) {
  9.   x = max(x, y);
  10. }
  11.  
  12. int main() {
  13.   short n;
  14.   fin >> n;
  15.   vector<short> a(n + 1), b(n + 1);
  16.   for (short i = 1; i <= n; ++i)
  17.     fin >> a[i];
  18.   for (short i = 1; i <= n; ++i)
  19.     fin >> b[i];
  20.   vector<vector<short>> dp(n + 1, vector<short>(n + 1));
  21.   for (short i = 1; i <= n; ++i)
  22.     for (short j = 1; j <= n; ++j) {
  23.       dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
  24.       if (abs(a[i] - b[j]) <= 4)
  25.         max_self(dp[i][j], dp[i - 1][j - 1] + 1);
  26.     }
  27.   fout << dp[n][n] << '\n';
  28.   fin.close();
  29.   fout.close();
  30.   return 0;
  31. }
  32.  
Add Comment
Please, Sign In to add comment