結果
| 問題 | No.1767 BLUE to RED | 
| コンテスト | |
| ユーザー |  tnakao0123 | 
| 提出日時 | 2021-11-30 10:00:36 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 57 ms / 2,000 ms | 
| コード長 | 952 bytes | 
| コンパイル時間 | 278 ms | 
| コンパイル使用メモリ | 41,800 KB | 
| 実行使用メモリ | 5,376 KB | 
| 最終ジャッジ日時 | 2024-07-03 02:44:00 | 
| 合計ジャッジ時間 | 3,703 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
/* -*- coding: utf-8 -*-
 *
 * 1767.cc:  No.1767 BLUE to RED - yukicoder
 */
#include<cstdio>
#include<algorithm>
 
using namespace std;
/* constant */
const int MAX_N = 200000;
const int INF = (1U << 31) - 1;
const int MINF = -(1 << 30);
/* typedef */
/* global variables */
int as[MAX_N + 2], bs[MAX_N];
/* subroutines */
/* main */
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", as + i);
  as[0] = MINF, as[n + 1] = INF;
  n += 2;
  for (int i = 0; i < m; i++) scanf("%d", bs + i);
  int sum = 0;
  for (int i = 0, k = 0; i < m;) {
    while (as[k + 1] < bs[i]) k++;
    int j = i + 1;
    while (j < m && bs[j] < as[k + 1]) j++;
    int mind = INF;
    for (int l = i; l <= j; l++) {
      int d = 0;
      if (l > i) d += bs[l - 1] - as[k];
      if (l < j) d += as[k + 1] - bs[l];
      mind = min(mind, d);
    }
    sum += mind;
    i = j;
  }
  printf("%d\n", sum);
  return 0;
}
            
            
            
        