結果
| 問題 |
No.335 門松宝くじ
|
| コンテスト | |
| ユーザー |
emthrm
|
| 提出日時 | 2020-03-16 03:19:29 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 114 ms / 2,000 ms |
| コード長 | 3,345 bytes |
| コンパイル時間 | 2,462 ms |
| コンパイル使用メモリ | 211,096 KB |
| 最終ジャッジ日時 | 2025-01-09 07:15:15 |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 10 |
ソースコード
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
using ll = long long;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;
const double EPS = 1e-8;
const int MOD = 1000000007;
// const int MOD = 998244353;
const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1};
const int dy8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dx8[] = {0, -1, -1, -1, 0, 1, 1, 1};
template <typename T, typename U> inline bool chmax(T &a, U b) { return a < b ? (a = b, true) : false; }
template <typename T, typename U> inline bool chmin(T &a, U b) { return a > b ? (a = b, true) : false; }
struct IOSetup {
IOSetup() {
cin.tie(nullptr);
ios_base::sync_with_stdio(false);
cout << fixed << setprecision(20);
}
} iosetup;
int solve(const vector<int> &e) {
int n = e.size();
vector<int> pos(n + 1, -1);
REP(i, n) pos[e[i]] = i;
vector<int> mx_l(n, 0), mn_l(n, 0), mx_r(n, 0), mn_r(n, 0);
{
int mx = 0;
REP(i, n) {
if (mx > e[i]) mx_l[i] = mx;
chmax(mx, e[i]);
}
}
{
int mx = 0;
for (int i = n - 1; i >= 0; --i) {
if (mx > e[i]) mx_r[i] = mx;
chmax(mx, e[i]);
}
}
{
set<int> st{e[0]};
FOR(i, 1, n) {
if (*st.begin() < e[i]) mn_l[i] = *prev(st.lower_bound(e[i]));
st.emplace(e[i]);
}
}
{
set<int> st{e[n - 1]};
for (int i = n - 2; i >= 0; --i) {
if (*st.begin() < e[i]) mn_r[i] = *prev(st.lower_bound(e[i]));
st.emplace(e[i]);
}
}
vector<vector<int> > mx_l2(n, vector<int>(n, 0)), mn_l2(n, vector<int>(n, 0)), mx_r2(n, vector<int>(n, 0)), mn_r2(n, vector<int>(n, 0));
REP(i, n) {
int mx = 0;
FOR(j, i + 1, n) {
if (mx > e[i]) mx_l2[i][j] = mx;
chmax(mx, e[j]);
}
}
for (int i = n - 1; i >= 0; --i) {
int mx = 0;
for (int j = i - 1; j >= 0; --j) {
if (mx > e[i]) mx_r2[j][i] = mx;
chmax(mx, e[j]);
}
}
REP(i, n - 1) {
set<int> st{e[i + 1]};
FOR(j, i + 2, n) {
if (*st.begin() < e[i]) mn_l2[i][j] = *prev(st.lower_bound(e[i]));
st.emplace(e[i]);
}
}
for (int i = n - 1; i > 0; --i) {
set<int> st{e[i - 1]};
for (int j = i - 2; j >= 0; --j) {
if (*st.begin() < e[i]) mn_r2[j][i] = *prev(st.lower_bound(e[i]));
st.emplace(e[i]);
}
}
int ans = 0;
FOR(i, 1, n + 1) FOR(j, i + 1, n + 1) {
int u = pos[i], v = pos[j];
if (u < v) {
if (mx_l[u] == 0 && mn_r[v] == 0 && mn_l2[u][v] == 0 && mx_r2[u][v] == 0) continue;
// cout << i << ' ' << j << " : " << max({i, j, mx_l[u], mn_r[v], mn_l2[u][v], mx_r2[u][v]}) << '\n';
ans += max({i, j, mx_l[u], mn_r[v], mn_l2[u][v], mx_r2[u][v]});
} else {
if (mn_l[v] == 0 && mx_r[u] == 0 && mx_l2[v][u] == 0 && mn_r2[v][u] == 0) continue;
// cout << i << ' ' << j << " : " << max({i, j, mn_l[v], mx_r[u], mx_l2[v][u], mn_r2[v][u]}) << '\n';
ans += max({i, j, mn_l[v], mx_r[u], mx_l2[v][u], mn_r2[v][u]});
}
}
// cout << ans << '\n';
return ans;
}
int main() {
int n, m; cin >> n >> m;
int ans = 0, money = 0;
REP(i, m) {
vector<int> e(n); REP(i, n) cin >> e[i];
int now = solve(e);
if (now > money) {
ans = i;
money = now;
}
}
cout << ans << '\n';
return 0;
}
emthrm