結果

問題 No.59 鉄道の旅
ユーザー 37zigen37zigen
提出日時 2016-05-16 01:16:08
言語 Java21
(openjdk 21)
結果
AC  
実行時間 533 ms / 5,000 ms
コード長 983 bytes
コンパイル時間 1,778 ms
コンパイル使用メモリ 73,640 KB
実行使用メモリ 65,128 KB
最終ジャッジ日時 2023-08-26 07:01:44
合計ジャッジ時間 6,449 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
60,056 KB
testcase_01 AC 108 ms
60,196 KB
testcase_02 AC 111 ms
59,876 KB
testcase_03 AC 119 ms
60,032 KB
testcase_04 AC 480 ms
64,768 KB
testcase_05 AC 122 ms
59,564 KB
testcase_06 AC 125 ms
60,028 KB
testcase_07 AC 121 ms
59,832 KB
testcase_08 AC 234 ms
64,020 KB
testcase_09 AC 237 ms
64,416 KB
testcase_10 AC 240 ms
64,000 KB
testcase_11 AC 223 ms
63,228 KB
testcase_12 AC 398 ms
64,608 KB
testcase_13 AC 533 ms
65,128 KB
testcase_14 AC 487 ms
64,636 KB
testcase_15 AC 111 ms
60,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package yukicoder;
import java.util.Scanner;
public class Main{
	public static void main(String[] args){
		new Main().solve();
	}
	void solve(){
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int k=sc.nextInt();
		FenwickTree ft=new FenwickTree(1_000_000);
		for(int i = 0; i < n; i++){
			int w=sc.nextInt();
			if(w>0){
				if(ft.sum(w, 1_000_000)>=k)continue;
				else{
					ft.add(w, 1);
				}
			}else if(w<0){
				if(ft.sum(-w, -w)>0){
					ft.add(-w, -1);
				}
			}
		}
		System.out.println(ft.sum(1_000_000));
	}
	class FenwickTree{
		int[] bit;
		int n;

		public FenwickTree(int n){
			this.n = n;
			bit = new int[n + 1];
		}

		//xにiを加える。
		void add(int i, int x)
		{
			while(i <= n){
				bit[i] += x;
				i += i & -i;
			}
		}
		//iまでの和を求める。
		int sum(int i)
		{
			int sum = 0;
			while(i > 0){
				sum += bit[i];
				i -= i & -i;
			}
			return sum;
		}
		int sum(int l, int r)
		{
			return sum(r) - sum(l - 1);
		}
	}
}
0