結果

問題 No.2434 RAKUTAN de RAKUTAN
ユーザー tnakao0123
提出日時 2024-06-25 12:22:46
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 59 ms / 2,000 ms
コード長 2,534 bytes
コンパイル時間 522 ms
コンパイル使用メモリ 47,872 KB
最終ジャッジ日時 2025-02-22 00:30:14
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:39:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   39 |   scanf("%d%d%d", &n, &h, &x);
      |   ~~~~~^~~~~~~~~~~~~~~~~~~~~~
main.cpp:45:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   45 |   scanf("%d", &gn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:48:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   48 |     scanf("%d", &gi);
      |     ~~~~~^~~~~~~~~~~
main.cpp:53:8: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   53 |   scanf("%d", &bn);
      |   ~~~~~^~~~~~~~~~~
main.cpp:56:10: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   56 |     scanf("%d", &bi);
      |     ~~~~~^~~~~~~~~~~

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2434.cc:  No.2434 RAKUTAN de RAKUTAN - yukicoder
 */

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

/* constant */

const int MAX_GN = 1000;
const int MAX_BN = 1000;
const int MAX_X = 5;
const int MAX_M = MAX_GN + MAX_BN * (MAX_X * 2 - 1) + 2;
const int MAX_H = MAX_BN;
const int INF = 1 << 30;

/* typedef */

using pii = pair<int,int>;

/* global variables */

pii ps[MAX_M], lrs[MAX_M];
int uxs[MAX_M], uvs[MAX_M];
int dp[MAX_M][MAX_H + 1];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, h, x;
  scanf("%d%d%d", &n, &h, &x);

  int m = 0;
  ps[m++] = {0, 0};
  
  int gn;
  scanf("%d", &gn);
  for (int i = 0; i < gn; i++) {
    int gi;
    scanf("%d", &gi);
    ps[m++] = {gi, 1};
  }

  int bn;
  scanf("%d", &bn);
  for (int i = 0; i < bn; i++) {
    int bi;
    scanf("%d", &bi);
    ps[m++] = {bi, -1};
  }

  sort(ps, ps + m);
  if (ps[m - 1].first < n) ps[m++] = {n, 0};
  //printf(" m=%d\n", m);
  //for (int i = 0; i < m; i++)
  //  printf(" %d,%d", ps[i].first, ps[i].second);
  //putchar('\n');

  for (int i = 0; i < m; i++) {
    auto [xi, v] = ps[i];
    if (v < 0) lrs[i] = {max(0, xi - (x - 1)), min(n + 1, xi + x)};
    else lrs[i] = {xi, xi + 1};
  }
  sort(lrs, lrs + m);
  //for (int i = 0; i < m; i++)
  //  printf(" %d,%d", lrs[i].first, lrs[i].second);
  //putchar('\n');

  int l = -1, r = -1, k = 0;
  for (int i = 0; i < m; i++) {
    auto [li, ri] = lrs[i];
    if (r < li) {
      if (l >= 0)
	while (l < r) uxs[k++] = l++;
      l = li, r = ri;
    }
    else
      r = max(r, ri);
  }
  while (l < r) uxs[k++] = l++;
  //for (int i = 0; i < k; i++) printf(" %d", uxs[i]); putchar('\n');

  for (int i = 0, j = 0; i < k; i++) {
    while (j < m && ps[j].first < uxs[i]) j++;
    uvs[i] = (j < m && ps[j].first == uxs[i]) ? ps[j].second : 0;
  }
  //for (int i = 0; i < k; i++) printf(" %d,%d", uxs[i], uvs[i]);
  //putchar('\n');

  h = min(h, bn);
  for (int i = 0; i < k; i++) fill(dp[i], dp[i] + h + 1, -INF);
  dp[0][0] = 0;

  for (int i = 0; i < k - 1; i++)
    for (int j = 0; j <= h; j++)
      if (dp[i][j] > -INF) {
	setmax(dp[i + 1][j], dp[i][j] + uvs[i + 1]);

	if (j < h) {
	  int i1 = i + 1;
	  while (i1 < k && uxs[i1] < uxs[i] + x) i1++;
	  if (i1 < k && uxs[i1] == uxs[i] + x)
	    setmax(dp[i1][j + 1], dp[i][j] + uvs[i1]);
	}
      }

  int maxd = -INF;
  for (int j = 0; j <= h; j++) setmax(maxd, dp[k - 1][j]);

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