結果

問題 No.59 鉄道の旅
ユーザー msm1993msm1993
提出日時 2020-05-15 15:09:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 43 ms / 5,000 ms
コード長 775 bytes
コンパイル時間 599 ms
コンパイル使用メモリ 69,432 KB
実行使用メモリ 7,224 KB
最終ジャッジ日時 2023-10-19 04:36:57
合計ジャッジ時間 1,577 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
7,224 KB
testcase_01 AC 4 ms
7,224 KB
testcase_02 AC 4 ms
7,224 KB
testcase_03 AC 4 ms
7,224 KB
testcase_04 AC 43 ms
7,224 KB
testcase_05 AC 4 ms
7,224 KB
testcase_06 AC 4 ms
7,224 KB
testcase_07 AC 4 ms
7,224 KB
testcase_08 AC 8 ms
7,224 KB
testcase_09 AC 8 ms
7,224 KB
testcase_10 AC 8 ms
7,224 KB
testcase_11 AC 6 ms
7,224 KB
testcase_12 AC 19 ms
7,224 KB
testcase_13 AC 40 ms
7,224 KB
testcase_14 AC 38 ms
7,224 KB
testcase_15 AC 4 ms
7,224 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:32:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   32 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
//1-indexed
#include<vector>
template<typename T>
struct BIT{
	int n;
	vector<T>bit;
	BIT(int n_=0):n(n_),bit(n_+1){}
	T sum(int i)
	{
		T ans=0;
		for(;i>0;i-=i&-i)ans+=bit[i];
		return ans;
	}
	void add(int i,T a)
	{
		if(i==0)return;
		for(;i<=n;i+=i&-i)bit[i]+=a;
	}
	int lower_bound(T k)//k<=sum(ret)
	{
		if(k<=0)return 0;
		int ret=0,i=1;
		while((i<<1)<=n)i<<=1;
		for(;i;i>>=1)
			if(ret+i<=n&&bit[ret+i]<k)k-=bit[ret+=i];
		return ret+1;
	}
};
int N,K;
main()
{
	cin>>N>>K;
	BIT<int>P(1e6);
	int c=0;
	for(int i=0;i<N;i++)
	{
		int w;cin>>w;
		if(w>0)
		{
			if(c-P.sum(w-1)<K)
			{
				c++;
				P.add(w,1);
			}
		}
		else
		{
			w=-w;
			if(P.sum(w)>P.sum(w-1))
			{
				c--;
				P.add(w,-1);
			}
		}
	}
	cout<<c<<endl;
}
0