結果

問題 No.59 鉄道の旅
ユーザー kosakkunkosakkun
提出日時 2016-11-22 17:27:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 2,082 bytes
コンパイル時間 915 ms
コンパイル使用メモリ 100,220 KB
実行使用メモリ 11,928 KB
最終ジャッジ日時 2023-08-26 07:30:11
合計ジャッジ時間 1,961 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,820 KB
testcase_01 AC 4 ms
6,728 KB
testcase_02 AC 4 ms
6,784 KB
testcase_03 AC 4 ms
6,916 KB
testcase_04 AC 60 ms
9,144 KB
testcase_05 AC 4 ms
6,768 KB
testcase_06 AC 4 ms
6,864 KB
testcase_07 AC 5 ms
6,716 KB
testcase_08 AC 9 ms
7,304 KB
testcase_09 AC 8 ms
6,924 KB
testcase_10 AC 9 ms
6,784 KB
testcase_11 AC 5 ms
6,924 KB
testcase_12 AC 17 ms
6,828 KB
testcase_13 AC 54 ms
9,244 KB
testcase_14 AC 66 ms
11,928 KB
testcase_15 AC 4 ms
6,820 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <sstream>
#include <cmath>
#include <set>
#include <iomanip>
#include <deque>
#include <stdio.h>
using namespace std;

#define REP(i,n) for(int (i)=0;(i)<(int)(n);(i)++)
#define RREP(i,n) for(int (i)=(int)(n)-1;i>=0;i--)
#define iREP(i,Itr) for(auto (i)=(Itr).begin();(i)!=(Itr).end();(i)++)
#define REMOVE(Itr,n) (Itr).erase(remove((Itr).begin(),(Itr).end(),n),(Itr).end())
#define PB_VEC(Itr1,Itr2) (Itr1).insert((Itr1).end(),(Itr2).begin(),(Itr2).end())
#define UNIQUE(Itr) sort((Itr).begin(),(Itr).end()); (Itr).erase(unique((Itr).begin(),(Itr).end()),(Itr).end())
#define LBOUND(Itr,val) lower_bound((Itr).begin(),(Itr).end(),(val))
#define UBOUND(Itr,val) upper_bound((Itr).begin(),(Itr).end(),(val))
typedef long long ll;

class BinaryIndexTree{
public:
    vector<int> bit;
    int bit_size;
    BinaryIndexTree(int size){
        bit_size=size+1;
        bit.resize(size+1);
        for(int i=0;i<size+1;i++)bit[i]=0;
    }
    
    int sum(int i){
        int res=0;
        while(i>0){
            res+=bit[i];
            i-=i&-i;
        }
        return res;
    }
    
    void add(int i, int x){
        while(i<=bit_size){
            bit[i]+=x;
            i+=i&-i;
        }
    }
};

map<int,int> wcnt;

int main(){
    
    BinaryIndexTree inst(1000010);
    int N,K; cin>>N>>K;
    
    REP(i,N){
        int w; cin>>w;
        if(w<0){
            if(wcnt.find(abs(w))!=wcnt.end()){
                if(wcnt[abs(w)]>0){
                    wcnt[abs(w)]--;
                    inst.add(abs(w),-1);
                }
            }
        }else{
            int sum=inst.sum(1000010)-inst.sum(abs(w)-1);
            if(sum<K){
                if(wcnt.find(abs(w))!=wcnt.end()){
                    wcnt[abs(w)]++;
                }else{
                    wcnt[abs(w)]=1;
                }
                inst.add(abs(w),1);
            }
        }
        
    }
    
    cout<<inst.sum(1000010)<<endl;
    
    return 0;
}
0