結果

問題 No.59 鉄道の旅
ユーザー ctyl_0ctyl_0
提出日時 2015-10-21 03:51:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 38 ms / 5,000 ms
コード長 1,784 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 88,916 KB
実行使用メモリ 11,508 KB
最終ジャッジ日時 2023-08-26 06:48:50
合計ジャッジ時間 1,726 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
10,732 KB
testcase_01 AC 4 ms
10,844 KB
testcase_02 AC 4 ms
10,640 KB
testcase_03 AC 5 ms
10,644 KB
testcase_04 AC 38 ms
11,152 KB
testcase_05 AC 5 ms
10,784 KB
testcase_06 AC 5 ms
10,752 KB
testcase_07 AC 5 ms
10,676 KB
testcase_08 AC 8 ms
10,676 KB
testcase_09 AC 8 ms
10,644 KB
testcase_10 AC 8 ms
10,888 KB
testcase_11 AC 6 ms
11,084 KB
testcase_12 AC 19 ms
11,284 KB
testcase_13 AC 38 ms
11,508 KB
testcase_14 AC 37 ms
11,180 KB
testcase_15 AC 5 ms
10,828 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>

#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define var auto
#define mod 1000000007
#define inf 2147483647
typedef long long ll;

using namespace std;

int inputValue(){
    int a;
    cin >> a;
    return a;
}

template <typename T>
void output(T a, int precision) {
    if(precision > 0){
        cout << fixed << setprecision(precision)  << a << "\n";
    }
    else{
        cout << a << "\n";
    }
}

// end of template
struct bit {
    vector<ll> v; // 部分和をbitで管理 仮想配列はa[1...n]
    bit(int n){ // bit(int n) : v(n + 1) {}
        v.resize(n + 1);
    }
    
    ll sum(int i){ // 仮想配列a[1]~a[i]の和を求める
        ll ret = 0;
        for(; i > 0; i -= i & -i){ // i & -i: iの1が立っている最高位bitをの2^を返す
            ret += v[i];
        }
        return ret;
    }
    
    void add(int i, ll w) { // index:iにwを加える
        for (; i < v.size(); i += i & -i) {
            v[i] += w;
        }
    }
    
    ll decode(int i){
        return sum(i) - sum(i - 1);
    }
};

int main() {
    // source code
    int N = inputValue();
    int K = inputValue();
    vector<int> W(N);
    rep(i, N){
        W[i] = inputValue();
    }
    
    int num = 1000001;
    
    bit b(num);
    
    rep(i, N){
        if (W[i] > 0 && b.sum(num) - b.sum(W[i] - 1) < K) {
            b.add(W[i], 1);
        }
        if (W[i] < 0 && b.decode(-W[i]) > 0) {
            b.add(-W[i], -1);
        }
    }
    
    output(b.sum(num), 0);
    
    return 0;
}
0