Submission #283652

# Submission time Handle Problem Language Result Execution time Memory
283652 2020-08-26T04:31:52 Z 임성재(#5753) Žarulje (COI15_zarulje) C++17
60 / 100
1000 ms 4472 KB
#include<bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(false); cin.tie(0);
#define fi first
#define se second
#define em emplace
#define eb emplace_back
#define mp make_pair
#define all(v) (v).begin(), (v).end()

typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const ll INF = 1e18;
const ll inf = 1e9 + 7;

ll inv(ll a) {
	ll b = inf - 2;
	ll ret = 1;

	while(b) {
		if(b & 1) ret *= a;
		a *= a;
		b >>= 1;
		ret %= inf;
		a %= inf;
	}

	return ret;
}

int n, k;
int a[200010];
ll f[200010];
ll in[200010];

int main() {
	fast;

	cin >> n >> k;

	f[0] = 1;
	for(int i=1; i<=n; i++)
		f[i] = f[i-1] * i % inf;

	in[n] = inv(f[n]);

	for(int i=n; i>=1; i--) {
		in[i-1] = in[i] * i % inf;
	}

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


	while(k--) {
		int p;
		cin >> p;

		vector<pii> l, r;

		for(int i=1; i<p; i++) {
			while(l.size() && l.back().fi > a[i]) l.pop_back();
			if(l.size() && l.back().fi == a[i]) l.back().se++;
			else l.eb(a[i], 1);
		}

		for(int i = n; i > p; i--) {
			while(r.size() && r.back().fi > a[i]) r.pop_back();
			if(r.size() && r.back().fi == a[i]) r.back().se++;
			else r.eb(a[i], 1);
		}

		ll ans = 1;

		while(l.size() + r.size()) {
			if(l.empty()) r.pop_back();
			else if(r.empty()) l.pop_back();
			else if(l.back().fi > r.back().fi) l.pop_back();
			else if(l.back().fi < r.back().fi) r.pop_back();
			else {
				ans *= f[l.back().se + r.back().se];
				ans %= inf;
				ans *= in[l.back().se];
				ans %= inf;
				ans *= in[r.back().se];
				ans %= inf;

				l.pop_back();
				r.pop_back();
			}
		}

		cout << ans << "\n";
	}
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 39 ms 384 KB Output is correct
4 Correct 44 ms 384 KB Output is correct
5 Correct 25 ms 456 KB Output is correct
6 Correct 55 ms 460 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 2304 KB Output is correct
2 Correct 31 ms 4352 KB Output is correct
3 Correct 32 ms 4352 KB Output is correct
4 Correct 34 ms 4352 KB Output is correct
5 Correct 35 ms 4352 KB Output is correct
6 Correct 43 ms 4472 KB Output is correct
7 Correct 38 ms 4344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 39 ms 384 KB Output is correct
4 Correct 44 ms 384 KB Output is correct
5 Correct 25 ms 456 KB Output is correct
6 Correct 55 ms 460 KB Output is correct
7 Correct 17 ms 2304 KB Output is correct
8 Correct 31 ms 4352 KB Output is correct
9 Correct 32 ms 4352 KB Output is correct
10 Correct 34 ms 4352 KB Output is correct
11 Correct 35 ms 4352 KB Output is correct
12 Correct 43 ms 4472 KB Output is correct
13 Correct 38 ms 4344 KB Output is correct
14 Execution timed out 1095 ms 888 KB Time limit exceeded
15 Halted 0 ms 0 KB -