結果

問題 No.335 門松宝くじ
ユーザー 👑 emthrmemthrm
提出日時 2020-03-16 03:19:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 107 ms / 2,000 ms
コード長 3,345 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 215,008 KB
実行使用メモリ 13,416 KB
最終ジャッジ日時 2023-08-18 03:38:17
合計ジャッジ時間 4,089 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 28 ms
7,052 KB
testcase_06 AC 41 ms
7,120 KB
testcase_07 AC 72 ms
13,104 KB
testcase_08 AC 61 ms
13,052 KB
testcase_09 AC 107 ms
13,144 KB
testcase_10 AC 107 ms
13,100 KB
testcase_11 AC 107 ms
13,412 KB
testcase_12 AC 107 ms
13,416 KB
testcase_13 AC 107 ms
13,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
}
0