niyaznigmatullin

Untitled

Feb 15th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. const int N = 777;
  6.  
  7. int a[N], b[N];
  8. int dp[N][N];
  9.  
  10. int main() {
  11.   int n, m;
  12.   scanf("%d %d", &n, &m);
  13.   for (int i = 0; i < n; i++) {
  14.     scanf("%d", a + i);
  15.   }
  16.   for (int i = 0; i < m; i++) {
  17.     scanf("%d", b + i);
  18.   }
  19.   for (int i = 0; i < n; i++) {
  20.     for (int from = 1; from <= m; from++) {
  21.       dp[i][from] = (i > 0 ? (from - 1) : m);
  22.     }
  23.   }
  24.   for (int pos = 0; pos < m; pos++) {
  25.     for (int i = n - 1; i >= 0; i--) {
  26.       for (int from = 1; from <= b[pos]; from++) {
  27.         int to = dp[i][from];
  28.         if (to < b[pos]) {
  29.           continue;
  30.         }
  31.         if (i == n - 1) {
  32.           puts("Yes");
  33.           return 0;
  34.         }
  35.         int nfrom = from;
  36.         int nto = to;
  37.         if (a[i] < a[i + 1]) {
  38.           nfrom = b[pos] + 1;
  39.         } else {
  40.           nto = b[pos] - 1;
  41.         }
  42.         if (1 <= nfrom && nfrom <= nto && nto <= m) {
  43.           dp[i + 1][nfrom] = max(dp[i + 1][nfrom], nto);
  44.         }
  45.       }
  46.     }
  47.   }
  48.   puts("No");
  49.   return 0;
  50. }
Add Comment
Please, Sign In to add comment