답안 #283649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
283649 2020-08-26T04:21:33 Z kdh9949 Žarulje (COI15_zarulje) C++17
60 / 100
1000 ms 5628 KB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;

using vint = vector<int>;
using vll = vector<ll>;
using vld = vector<ld>;
using vpii = vector<pii>;
using vpil = vector<pil>;
using vpli = vector<pli>;
using vpll = vector<pll>;

#define x first
#define y second
#define all(v) v.begin(),v.end()

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n, q;
  cin >> n >> q;

  vint a(n + 2);
  for(int i = 1; i <= n; i++) cin >> a[i];

  const static ll M = ll(1e9) + 7;
  vll fa(n + 1, 1), fi(n + 1, 1);
  auto inv = [&](ll x) {
    ll r = 1;
    for(int k = M - 2; k; k /= 2, x = x * x % M) if(k & 1) r = r * x % M;
    return r;
  };
  for(int i = 1; i <= n; i++) {
    fa[i] = fa[i - 1] * i % M;
    fi[i] = inv(fa[i]);
  }
  auto com = [&](int n, int r) {
    if(n < 0 || r < 0 || r > n) return 0LL;
    return fa[n] * fi[r] % M * fi[n - r] % M;
  };

  while(q--) {
    int x;
    cin >> x;

    stack<pii> lst, rst;
    lst.emplace(0, 1);
    for(int i = 1; i < x; i++) {
      while(a[lst.top().x] > a[i]) lst.pop();
      if(a[lst.top().x] == a[i]) lst.top().y++;
      else lst.emplace(i, 1);
    }
    rst.emplace(n + 1, 1);
    for(int i = n; i > x; i--) {
      while(a[rst.top().x] > a[i]) rst.pop();
      if(a[rst.top().x] == a[i]) rst.top().y++;
      else rst.emplace(i, 1);
    }

    ll ans = 1;
    while(a[lst.top().x] && a[rst.top().x]) {
      int lv = a[lst.top().x], rv = a[rst.top().x];
      if(lv > rv) lst.pop();
      else if(lv < rv) rst.pop();
      else {
        ans = ans * com(lst.top().y + rst.top().y, lst.top().y) % M;
        lst.pop();
        rst.pop();
      }
    }
    cout << ans << '\n';
  }

  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 45 ms 384 KB Output is correct
4 Correct 58 ms 384 KB Output is correct
5 Correct 31 ms 512 KB Output is correct
6 Correct 61 ms 448 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 2304 KB Output is correct
2 Correct 62 ms 4736 KB Output is correct
3 Correct 64 ms 4908 KB Output is correct
4 Correct 65 ms 4988 KB Output is correct
5 Correct 66 ms 5240 KB Output is correct
6 Correct 67 ms 5496 KB Output is correct
7 Correct 71 ms 5628 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 45 ms 384 KB Output is correct
4 Correct 58 ms 384 KB Output is correct
5 Correct 31 ms 512 KB Output is correct
6 Correct 61 ms 448 KB Output is correct
7 Correct 33 ms 2304 KB Output is correct
8 Correct 62 ms 4736 KB Output is correct
9 Correct 64 ms 4908 KB Output is correct
10 Correct 65 ms 4988 KB Output is correct
11 Correct 66 ms 5240 KB Output is correct
12 Correct 67 ms 5496 KB Output is correct
13 Correct 71 ms 5628 KB Output is correct
14 Execution timed out 1092 ms 784 KB Time limit exceeded
15 Halted 0 ms 0 KB -