結果

問題 No.59 鉄道の旅
ユーザー tnakao0123tnakao0123
提出日時 2016-03-04 18:35:49
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 32 ms / 5,000 ms
コード長 1,852 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 83,484 KB
実行使用メモリ 7,864 KB
最終ジャッジ日時 2023-08-26 06:55:12
合計ジャッジ時間 1,453 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
7,576 KB
testcase_01 AC 3 ms
7,584 KB
testcase_02 AC 4 ms
7,580 KB
testcase_03 AC 3 ms
7,764 KB
testcase_04 AC 32 ms
7,864 KB
testcase_05 AC 3 ms
7,628 KB
testcase_06 AC 3 ms
7,704 KB
testcase_07 AC 4 ms
7,568 KB
testcase_08 AC 6 ms
7,580 KB
testcase_09 AC 6 ms
7,636 KB
testcase_10 AC 6 ms
7,576 KB
testcase_11 AC 4 ms
7,636 KB
testcase_12 AC 15 ms
7,796 KB
testcase_13 AC 32 ms
7,792 KB
testcase_14 AC 31 ms
7,852 KB
testcase_15 AC 3 ms
7,640 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 59.cc: No.59 鉄道の旅 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MAX_W = 1000000;

/* typedef */

template <typename T, const int MAX_N>
struct BIT {
  int n;
  T bits[MAX_N + 1];
  
  BIT() {}
  BIT(int _n) { init(_n); }

  void init(int _n) {
    n = _n;
    memset(bits, 0, sizeof(bits));
  }

  T sum(int x) {
    T s = 0;
    while (x > 0) {
      s += bits[x];
      x -= (x & -x);
    }
    return s;
  }

  void add(int x, T v) {
    while (x <= n) {
      bits[x] += v;
      x += (x & -x);
    }
  }
};

/* global variables */

int wis[MAX_N];
BIT<int,MAX_W> bit;

/* subroutines */

/* main */

int main() {
  int n, k;
  cin >> n >> k;

  int maxw = 0;
  for (int i = 0; i < n; i++) {
    cin >> wis[i];
    if (maxw < wis[i]) maxw = wis[i];
  }

  bit.init(maxw);
  int wn = 0;

  for (int i = 0; i < n; i++) {
    int wi = wis[i];

    if (wi > 0) {  // load
      int c = wn - bit.sum(wi - 1);
      if (c < k) {
	bit.add(wi, 1);
	wn++;
	//printf("%d: %d loaded: c=%d, k=%d\n", i, wi, c, k);
	//for (int j = 1; j <= maxw; j++) printf("%d ", bit.sum(j));
	//putchar('\n');
      }
      //else
      //printf("%d: %d not loaded: c=%d\n", i, wi, c);
    }
    else { // unload
      wi = -wi;
      int c = bit.sum(wi) - bit.sum(wi - 1);
      if (c > 0) {
	bit.add(wi, -1);
	wn--;
	//printf("%d: %d unloaded\n", i, wi);
      }
      //else
      //printf("%d: %d not unloaded: c=%d\n", i, wi, c);
    }
  }

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