結果

問題 No.2759 Take Pictures, Elements?
ユーザー tnakao0123
提出日時 2024-05-22 15:19:29
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 42 ms / 2,000 ms
コード長 2,529 bytes
コンパイル時間 932 ms
コンパイル使用メモリ 68,124 KB
実行使用メモリ 7,844 KB
最終ジャッジ日時 2025-06-20 13:21:11
合計ジャッジ時間 1,778 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:75:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   75 |   scanf("%d%d", &n, &qn);
      |   ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:76:36: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   76 |   for (int i = 0; i < n; i++) scanf("%d", as + i);
      |                               ~~~~~^~~~~~~~~~~~~~
main.cpp:77:37: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   77 |   for (int i = 0; i < qn; i++) scanf("%d", bs + i);
      |                                ~~~~~^~~~~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2759.cc:  No.2759 Take Pictures, Elements? - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 1000;
const int MAX_QN = 1000;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;

template <typename T>
struct SegTreeMin {
  int e2;
  vector<T> nodes;
  T defv;
  SegTreeMin() {}

  void init(int n, T _defv) {
    defv = _defv;
    for (e2 = 1; e2 < n; e2 <<= 1);
    nodes.assign(e2 * 2, defv);
  }

  T &geti(int i) { return nodes[e2 - 1 + i]; }
  void seti(int i, T v) { geti(i) = v; }

  void setall() {
    for (int j = e2 - 2; j >= 0; j--)
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
  }

  void set(int i, T v) {
    int j = e2 - 1 + i;
    nodes[j] = v;
    while (j > 0) {
      j = (j - 1) / 2;
      nodes[j] = min(nodes[j * 2 + 1], nodes[j * 2 + 2]);
    }
  }

  T min_range(int r0, int r1, int k, int i0, int i1) {
    if (r1 <= i0 || i1 <= r0) return defv;
    if (r0 <= i0 && i1 <= r1) return nodes[k];

    int im = (i0 + i1) / 2;
    T v0 = min_range(r0, r1, k * 2 + 1, i0, im);
    T v1 = min_range(r0, r1, k * 2 + 2, im, i1);
    return min(v0, v1);
  }
  T min_range(int r0, int r1) { return min_range(r0, r1, 0, 0, e2); }
};

/* global variables */

int as[MAX_N], bs[MAX_QN], uas[MAX_N], dp[MAX_N];
vi avs[MAX_N];

/* subroutines */

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);
  for (int i = 0; i < n; i++) scanf("%d", as + i);
  for (int i = 0; i < qn; i++) scanf("%d", bs + i);

  copy(as, as + n, uas);
  sort(uas, uas + n);
  int m = unique(uas, uas + n) - uas;
  
  for (int i = 0; i < n; i++) {
    int ai = lower_bound(uas, uas + m, as[i]) - uas;
    avs[ai].push_back(i);
  }

  SegTreeMin<int> st0, st1;
  st0.init(n, INF); st0.set(0, 0); // st0: min(dp[i] - i)
  st1.init(n, INF); st1.set(0, 0); // st1: min(dp[i] + i)

  for (int i = 0; i < qn; i++) {
    SegTreeMin<int> xst0, xst1;
    xst0.init(n, INF);
    xst1.init(n, INF);
    //printf(" i=%d:", i);

    int bi = lower_bound(uas, uas + m, bs[i]) - uas;
    for (auto j: avs[bi]) {
      auto e0 = st0.min_range(0, j);
      auto e1 = st1.min_range(j, n);
      int dj = min(e0 + j, e1 - j);
      xst0.set(j, dj - j);
      xst1.set(j, dj + j);
      //printf(" %d,%d", j, dj);
    }
    //putchar('\n');

    swap(st0, xst0);
    swap(st1, xst1);
  }

  int mind = INF;
  for (int i = 0; i < n; i++)
    mind = min(mind, st0.geti(i) + i);

  printf("%d\n", mind);
  
  return 0;
}
0