結果

問題 No.370 道路の掃除
ユーザー ozisan19
提出日時 2016-05-14 02:31:49
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
WA  
実行時間 -
コード長 943 bytes
コンパイル時間 621 ms
コンパイル使用メモリ 49,472 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-05 21:55:26
合計ジャッジ時間 1,922 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 34
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:15:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   15 |         scanf("%d %d", &N, &M);
      |         ~~~~~^~~~~~~~~~~~~~~~~
main.cpp:20:22: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   20 |                 scanf("%d", &D[i]);
      |                 ~~~~~^~~~~~~~~~~~~

ソースコード

diff #

#include <cstdio>
#include <map>
#include <algorithm>
#include <vector>

using namespace std;

int main(void){
	int N, M;
	//int D[1010];
	int ans = 100000000; //ansを大きいめの値で初期化
	int sum = 0;

	//入力 N個を拾う
	scanf("%d %d", &N, &M);

	vector<int> D(M);
	for (int i = 0; i < M; ++i)
	{
		scanf("%d", &D[i]);
	}

	//vectorのソートを行う 昇順にソート
	sort(D.begin(), D.end());

	for (int i = 0; i <= M - N; ++i)
	{
		//どちらも負から取りに行く時
		if (D[i] <= 0 && D[i + N -1] >= 0)
		{
			sum = D[i] * -1; //D[i]の方が絶対値が大きい i < i + N - 1
		//どちらも正から取りに行く時
		}else if(D[i] >= 0 && D[i + N - 1] >= 0){
			sum = D[i + N - 1];
		//正と負をどちらも取りに行く
		}else{
			sum = D[i + N - 1] - D[i] + min(D[i] * -1, D[i + N - 1]);
		}

		// ans の更新
		if (sum < ans)
		{
			ans = sum;
		}
	}

	printf("%d\n", ans);
	return 0;
}
0