結果

問題 No.59 鉄道の旅
ユーザー myantamyanta
提出日時 2017-05-11 02:45:28
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 80 ms / 5,000 ms
コード長 1,083 bytes
コンパイル時間 195 ms
コンパイル使用メモリ 26,040 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-13 10:09:24
合計ジャッジ時間 1,618 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,356 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,352 KB
testcase_03 AC 1 ms
4,348 KB
testcase_04 AC 61 ms
4,348 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 1 ms
4,352 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 4 ms
4,352 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 4 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 10 ms
4,348 KB
testcase_13 AC 80 ms
4,372 KB
testcase_14 AC 15 ms
4,348 KB
testcase_15 AC 1 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<cstdio>
#include<cstring>


int n, k;
int lnum;

int list[100000];

void list_clear(void)
{
	lnum=0;
}


int list_search(int w, int& index)
{
	int s, e, c;
	int ret=0;

	index=0;
	s=0;
	e=lnum-1;
	for(;s<=e;)
	{
		c=(s+e)/2;
		if(w<list[c])
		{
			s=c+1;
			index=c+1;
		}
		if(w>list[c])
		{
			e=c-1;
			index=c;
		}
		if(w==list[c])
		{
			ret=1;
			index=c;
			s=c+1;
			break;
		}
	}
	if(ret)
	{
		while(s<=e)
		{
			c=(s+e)/2;
			if(w>list[c])
			{
				e=c-1;
			}
			else
			{
				s=c+1;
				index=c;
			}
		}
	}
	return ret;
}


void list_add(int w)
{
	int index, r;

	if(w>0)
	{
		r=list_search(w, index);
		if(r==1) index++;

		if(index>=k) return;
		memmove(list+index+1, list+index, (lnum-index)*sizeof(list[0]));
		list[index]=w;
		lnum++;
	}
	else
	{
		if(list_search(-w, index)==0) return;
		lnum--;
		memmove(list+index, list+index+1, (lnum-index)*sizeof(list[0]));
	}
}


int main(void)
{
	int i, w;

	while(scanf("%d%d", &n, &k)==2)
	{
		list_clear();
		for(i=0;i<n;i++)
		{
			scanf("%d", &w);
			list_add(w);
		}
		printf("%d\n", lnum);
	}

	return 0;
}
0