Submission #9190141


Source Code Expand

//~ while (clock()<=69*CLOCKS_PER_SEC)
//~ #pragma comment(linker, "/stack:200000000")
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
//~ #pragma GCC optimize("Ofast")
//~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//~ #pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define st first
#define nd second

using namespace __gnu_pbds;
using namespace std;
 
template <typename T>
using ordered_set =
	tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
 
#define sim template < class c
#define ris return * this
#define dor > debug & operator <<
#define eni(x) sim > typename \
  enable_if<sizeof dud<c>(0) x 1, debug&>::type operator<<(c i) {
sim > struct rge { c b, e; };
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c* x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug {
#ifdef LOCAL
~debug() { cerr << endl; }
eni(!=) cerr << boolalpha << i; ris; }
eni(==) ris << range(begin(i), end(i)); }
sim, class b dor(pair < b, c > d) {
  ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d) {
  *this << "[";
  for (auto it = d.b; it != d.e; ++it)
	*this << ", " + 2 * (it == d.b) << *it;
  ris << "]";
}
#else
sim dor(const c&) { ris; }
#endif
};
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
 
#define shandom_ruffle random_shuffle
 
using ll=long long;
using pii=pair<int,int>;
using pll=pair<ll,ll>;
using vi=vector<int>;
using vll=vector<ll>;

const int MAXN = 500;
const int mod = 998244353;

int n;
int pwr2[MAXN];

struct DpRes {
	vi need, noneed;
	int width;

	DpRes(int W) : width(W) {
		need.resize(width + 2);
		noneed.resize(width + 2);
	}

	static DpRes Init() {
		DpRes res(1);
		res.need[1] = 1;
		res.noneed[0] = 1;
		return res;
	}

	DpRes Add() {
		DpRes ans(width);
		for (int bad = 0; bad <= width; ++bad) {
			const int coef = pwr2[width - bad];
			debug() << imie(bad) << imie(coef) << imie(width);
			ans.noneed[bad] = ((ll)coef * noneed[bad] + need[bad]) % mod;
			ans.need[bad] = (ll)(coef - 1) * need[bad] % mod;
		}
		return ans;
	}

	DpRes Join(const DpRes &other) {
		DpRes ans(width + other.width);
		
		for (int mybad = 0; mybad <= width; ++mybad) {
			for (int obad = 0; obad <= other.width; ++obad) {
				const int addbad = mybad + obad;
				ans.need[addbad] = (
					ans.need[addbad] +
					(ll)need[mybad] * other.need[obad] +
					(ll)noneed[mybad] * other.need[obad] +
					(ll)need[mybad] * other.noneed[obad]) % mod;
					
				ans.noneed[addbad] = (
					ans.noneed[addbad] + (ll)noneed[mybad] * other.noneed[obad]) % mod;
			}
		}
		return ans;
	}
};

int hs[MAXN];


DpRes Solve(int L, int R, int h) {
	int minh = 1e9;
	for (int i = L; i <= R; ++i) {
		minh = min(minh, hs[i]);
	}

	if (h < minh) {
		return Solve(L, R, h+1).Add();
	}

	vector<DpRes> subs;
	int start = L;
	for (int i = L; i <= R; ++i) {
		if (hs[i] == h) {
			if (start < i) {
				subs.push_back(Solve(start, i-1, h));
			}
			subs.push_back(DpRes::Init());
			start = i + 1;
		}
	}
	if (start <= R) {
		subs.push_back(Solve(start, R, h));
	}

	DpRes ans = subs[0];
	for (int i = 1; i < SZ(subs); ++i) {
		ans = ans.Join(subs[i]);
	}
	return ans;
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cout << fixed << setprecision(11);
	cerr << fixed << setprecision(6);

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

	pwr2[0] = 1;
	for (int i = 1; i < MAXN; ++i) {
		pwr2[i] = (pwr2[i-1] * 2) % mod;
	}

	auto ans_vec = Solve(0, n-1, 0);
	int total = 0;

	for (int i = 0; i <= n; ++i) {
		if (i % 2 == 0) {
			total = (total + ans_vec.noneed[i]) % mod;
		} else {
			total = (total + mod - ans_vec.noneed[i]) % mod;
		}
	}
	cout << total << "\n";
}

Submission Info

Submission Time
Task F - Histogram Rooks
User mnbvmar
Language C++14 (GCC 5.4.1)
Score 2000
Code Size 4052 Byte
Status AC
Exec Time 11 ms
Memory 640 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 2000 / 2000
Status
AC × 4
AC × 76
Set Name Test Cases
Sample 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 00-sample-04.txt
All 00-sample-01.txt, 00-sample-02.txt, 00-sample-03.txt, 00-sample-04.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 01-27.txt, 01-28.txt, 01-29.txt, 01-30.txt, 01-31.txt, 01-32.txt, 01-33.txt, 01-34.txt, 01-35.txt, 01-36.txt, 01-37.txt, 01-38.txt, 01-39.txt, 01-40.txt, 01-41.txt, 01-42.txt, 01-43.txt, 01-44.txt, 01-45.txt, 01-46.txt, 01-47.txt, 01-48.txt, 01-49.txt, 01-50.txt, 01-51.txt, 01-52.txt, 01-53.txt, 01-54.txt, 01-55.txt, 01-56.txt, 01-57.txt, 01-58.txt, 01-59.txt, 01-60.txt, 01-61.txt, 01-62.txt, 01-63.txt, 01-64.txt, 01-65.txt, 01-66.txt, 01-67.txt, 01-68.txt, 01-69.txt, 01-70.txt, 01-71.txt, 01-72.txt
Case Name Status Exec Time Memory
00-sample-01.txt AC 1 ms 256 KiB
00-sample-02.txt AC 1 ms 256 KiB
00-sample-03.txt AC 1 ms 256 KiB
00-sample-04.txt AC 1 ms 256 KiB
01-01.txt AC 1 ms 256 KiB
01-02.txt AC 1 ms 256 KiB
01-03.txt AC 1 ms 256 KiB
01-04.txt AC 1 ms 256 KiB
01-05.txt AC 2 ms 384 KiB
01-06.txt AC 3 ms 512 KiB
01-07.txt AC 4 ms 384 KiB
01-08.txt AC 3 ms 384 KiB
01-09.txt AC 2 ms 384 KiB
01-10.txt AC 3 ms 384 KiB
01-11.txt AC 3 ms 384 KiB
01-12.txt AC 5 ms 512 KiB
01-13.txt AC 1 ms 256 KiB
01-14.txt AC 2 ms 384 KiB
01-15.txt AC 5 ms 512 KiB
01-16.txt AC 3 ms 384 KiB
01-17.txt AC 1 ms 256 KiB
01-18.txt AC 2 ms 384 KiB
01-19.txt AC 1 ms 256 KiB
01-20.txt AC 5 ms 512 KiB
01-21.txt AC 5 ms 512 KiB
01-22.txt AC 5 ms 512 KiB
01-23.txt AC 5 ms 512 KiB
01-24.txt AC 5 ms 512 KiB
01-25.txt AC 11 ms 512 KiB
01-26.txt AC 3 ms 640 KiB
01-27.txt AC 3 ms 640 KiB
01-28.txt AC 3 ms 640 KiB
01-29.txt AC 3 ms 640 KiB
01-30.txt AC 3 ms 640 KiB
01-31.txt AC 3 ms 512 KiB
01-32.txt AC 3 ms 640 KiB
01-33.txt AC 3 ms 640 KiB
01-34.txt AC 3 ms 512 KiB
01-35.txt AC 3 ms 512 KiB
01-36.txt AC 3 ms 512 KiB
01-37.txt AC 3 ms 512 KiB
01-38.txt AC 3 ms 512 KiB
01-39.txt AC 3 ms 512 KiB
01-40.txt AC 3 ms 512 KiB
01-41.txt AC 3 ms 512 KiB
01-42.txt AC 3 ms 512 KiB
01-43.txt AC 3 ms 512 KiB
01-44.txt AC 3 ms 512 KiB
01-45.txt AC 3 ms 512 KiB
01-46.txt AC 3 ms 512 KiB
01-47.txt AC 3 ms 512 KiB
01-48.txt AC 3 ms 512 KiB
01-49.txt AC 3 ms 512 KiB
01-50.txt AC 5 ms 512 KiB
01-51.txt AC 6 ms 512 KiB
01-52.txt AC 5 ms 512 KiB
01-53.txt AC 3 ms 512 KiB
01-54.txt AC 3 ms 512 KiB
01-55.txt AC 3 ms 512 KiB
01-56.txt AC 3 ms 640 KiB
01-57.txt AC 3 ms 640 KiB
01-58.txt AC 3 ms 512 KiB
01-59.txt AC 2 ms 512 KiB
01-60.txt AC 3 ms 512 KiB
01-61.txt AC 2 ms 512 KiB
01-62.txt AC 3 ms 512 KiB
01-63.txt AC 2 ms 512 KiB
01-64.txt AC 2 ms 512 KiB
01-65.txt AC 2 ms 512 KiB
01-66.txt AC 2 ms 512 KiB
01-67.txt AC 2 ms 512 KiB
01-68.txt AC 3 ms 512 KiB
01-69.txt AC 3 ms 512 KiB
01-70.txt AC 3 ms 512 KiB
01-71.txt AC 3 ms 512 KiB
01-72.txt AC 3 ms 512 KiB