DuongNhi99

C - Karat Competition (24-11)

Dec 10th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n;
  5. int a[55], b[55];
  6. int v[55][55], dp[55][55];
  7.  
  8. void solve(int test){
  9.     for(int i = 1; i <= n; ++i)
  10.         for(int j = 1; j <= n; ++j){
  11.             if(a[i] > b[j]) v[i][j] = 2;
  12.             else if(a[i] < b[j]) v[i][j] = 0;
  13.             else v[i][j] = 1;
  14.         }
  15.  
  16.     for(int i = 1; i <= n; ++i){
  17.         for(int j = 1; j <= n; ++j){
  18.             dp[i][j] = max({
  19.                             dp[i-1][j],
  20.                             dp[i][j-1],
  21.                             dp[i-1][j-1] + v[i][j]
  22.                        });
  23.         }
  24.     }
  25.     cout << "Case " << test << ": " << dp[n][n] << '\n';
  26. }
  27.  
  28. int main() {
  29.     //freopen("in.txt", "r", stdin);
  30.     //freopen("C-KaratCompetition.inp", "r", stdin);
  31.     //freopen("C-KaratCompetition.out", "w", stdout);
  32.     ios_base::sync_with_stdio(false);
  33.     cin.tie(NULL); cout.tie(NULL);
  34.  
  35.     int t; cin >> t;
  36.     for(int test = 1; test <= t; ++test) {
  37.         cin >> n;
  38.         for(int i = 1; i <= n; ++i)
  39.             cin >> a[i];
  40.         for(int i = 1; i <= n; ++i)
  41.             cin >> b[i];
  42.         sort(a + 1, a + n + 1);
  43.         sort(b + 1, b + n + 1);
  44.  
  45.         solve(test);
  46.     }
  47.     return 0;
  48. }
  49.  
Advertisement
Add Comment
Please, Sign In to add comment