ログインしてください。
提出 #10528505
ソースコード 拡げる
#pragma warning (disable:4996)
#include"bits/stdc++.h"
#include<cassert>
#define int long long
#define MRE assert(0);
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const long long mod = 1000000007;
const long long inf = 1e17;
typedef pair<int, int> P;
int n;
int h[105];
int dp1[105][105], dp2[105][105];
int modpow(int a, int x) {
int res = 1;
while (x) {
if (x & 1)res = res*a%mod;
a = a*a%mod;
x >>= 1;
}
return res;
}
void memo(int l, int r) {
if (dp1[l][r] != -1)return;
vector<P>V;
int idx = l, mn = inf;
for (int i = l; i <= r; i++)mn = min(mn, h[i]);
int idx2 = l;
while (1) {
while (idx2<=r&&h[idx2] == mn)idx2++;
if (idx2 > r)break;
int idx3 = idx2;
while (idx3<=r&&h[idx3] != mn)idx3++;
V.push_back(P(idx2, idx3-1));
idx2 = idx3 + 1;
}
if (V.size()) {//適切なマージ
vector<P>V2;
rep(j, V.size()) {
memo(V[j].first, V[j].second);
V2.push_back(P(dp1[V[j].first][V[j].second], dp2[V[j].first][V[j].second]));
}
int m = 0;
for(int i=l;i<=r;i++)if (h[i] == mn)m++;
int sum = 1, sum2 = 2;
int H = h[r + 1];
if (l)H = max(H, h[l - 1]);
H = mn - H;
rep(i, V2.size()) {
sum *= V2[i].first + 2 * V2[i].second;
sum %= mod;
sum2 *= V2[i].second;
sum2 %= mod;
}
sum = sum*modpow(2, m) % mod;
int T = (sum + mod - sum2) % mod;
sum2 = sum2*modpow(2, H-1) % mod;
dp1[l][r] = T;
dp2[l][r] = sum2;
}
else {
int H = h[r + 1];
if (l)H = max(H, h[l - 1]);
int t = (modpow(2, r - l + 1) + mod - 2) % mod;
int u = modpow(2, h[l]-H);
dp1[l][r] = t;
dp2[l][r] = u;
}
}
signed main() {
cin >> n;
rep(i, n)cin >> h[i];
rep(i, n)rep(j, n)dp1[i][j] = dp2[i][j]= -1;
memo(0, n - 1);
cout << (dp1[0][n - 1] + dp2[0][n - 1])%mod << endl;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | D - Histogram Coloring |
| ユーザ | Rho17 |
| 言語 | C++14 (GCC 5.4.1) |
| 得点 | 1100 |
| コード長 | 1825 Byte |
| 結果 | AC |
| 実行時間 | 1 ms |
| メモリ | 384 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 1100 / 1100 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | example_0, example_1, example_2, example_3 |
| All | bigrand_0, bigrand_1, bigrand_2, bigrand_3, bigrand_4, bigrand_5, bigrand_6, bigrand_7, bigrand_8, bigrand_9, example_0, example_1, example_2, example_3, perm2_0, perm2_1, perm2_2, perm2_3, perm2_4, perm2_5, perm_0, perm_1, perm_2, perm_3, perm_4, perm_5, rand_0, rand_1, rand_2, rand_3, rand_4, rand_5, rand_6, rand_7, rand_8, rand_9, smallc_0, smallc_1, smallc_2, smallc_3, smallc_4, smallc_5, smallc_6, smallc_7, smallc_8, smallc_9 |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| bigrand_0 | AC | 1 ms | 384 KiB |
| bigrand_1 | AC | 1 ms | 384 KiB |
| bigrand_2 | AC | 1 ms | 384 KiB |
| bigrand_3 | AC | 1 ms | 384 KiB |
| bigrand_4 | AC | 1 ms | 384 KiB |
| bigrand_5 | AC | 1 ms | 384 KiB |
| bigrand_6 | AC | 1 ms | 384 KiB |
| bigrand_7 | AC | 1 ms | 384 KiB |
| bigrand_8 | AC | 1 ms | 384 KiB |
| bigrand_9 | AC | 1 ms | 384 KiB |
| example_0 | AC | 1 ms | 256 KiB |
| example_1 | AC | 1 ms | 256 KiB |
| example_2 | AC | 1 ms | 256 KiB |
| example_3 | AC | 1 ms | 256 KiB |
| perm2_0 | AC | 1 ms | 384 KiB |
| perm2_1 | AC | 1 ms | 384 KiB |
| perm2_2 | AC | 1 ms | 384 KiB |
| perm2_3 | AC | 1 ms | 256 KiB |
| perm2_4 | AC | 1 ms | 384 KiB |
| perm2_5 | AC | 1 ms | 256 KiB |
| perm_0 | AC | 1 ms | 384 KiB |
| perm_1 | AC | 1 ms | 384 KiB |
| perm_2 | AC | 1 ms | 384 KiB |
| perm_3 | AC | 1 ms | 384 KiB |
| perm_4 | AC | 1 ms | 384 KiB |
| perm_5 | AC | 1 ms | 256 KiB |
| rand_0 | AC | 1 ms | 384 KiB |
| rand_1 | AC | 1 ms | 384 KiB |
| rand_2 | AC | 1 ms | 384 KiB |
| rand_3 | AC | 1 ms | 256 KiB |
| rand_4 | AC | 1 ms | 384 KiB |
| rand_5 | AC | 1 ms | 256 KiB |
| rand_6 | AC | 1 ms | 256 KiB |
| rand_7 | AC | 1 ms | 384 KiB |
| rand_8 | AC | 1 ms | 384 KiB |
| rand_9 | AC | 1 ms | 384 KiB |
| smallc_0 | AC | 1 ms | 384 KiB |
| smallc_1 | AC | 1 ms | 384 KiB |
| smallc_2 | AC | 1 ms | 384 KiB |
| smallc_3 | AC | 1 ms | 384 KiB |
| smallc_4 | AC | 1 ms | 384 KiB |
| smallc_5 | AC | 1 ms | 384 KiB |
| smallc_6 | AC | 1 ms | 384 KiB |
| smallc_7 | AC | 1 ms | 384 KiB |
| smallc_8 | AC | 1 ms | 384 KiB |
| smallc_9 | AC | 1 ms | 384 KiB |