結果

問題 No.484 収穫
ユーザー tansunogontansunogon
提出日時 2017-05-05 17:45:25
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,031 ms / 3,000 ms
コード長 2,017 bytes
コンパイル時間 876 ms
コンパイル使用メモリ 84,976 KB
実行使用メモリ 99,468 KB
最終ジャッジ日時 2024-04-21 14:31:29
合計ジャッジ時間 13,501 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 935 ms
58,120 KB
testcase_13 AC 1,958 ms
99,468 KB
testcase_14 AC 1,022 ms
65,268 KB
testcase_15 AC 351 ms
50,848 KB
testcase_16 AC 861 ms
71,268 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 2,023 ms
96,212 KB
testcase_20 AC 2,011 ms
96,212 KB
testcase_21 AC 2,031 ms
96,088 KB
testcase_22 AC 652 ms
56,080 KB
testcase_23 AC 3 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

struct iostream_init_struct
{ 
	iostream_init_struct()
	{
		std::cin.tie(0);
		std::cin.sync_with_stdio(false);
	}
} iostream_init;

#include <queue>
#include <set>
#include <utility>
#include <algorithm>

int N;
int A[2000];

enum Position
{
	LEFT,
	RIGHT
};

class Agent
{
public:
	int time;
	int left;
	int right;
	Position place;
	Agent(int time, int left, int right, Position place) :
		time(time), left(left), right(right), place(place) {}

	Agent nextRight() const
	{
		int next_time;
		int next_left = left;
		int next_right = right - 1;
		Position next_place = RIGHT;

		if (place == RIGHT)
		{
			next_time = max(time, A[right]) + 1;
		}
		else
		{
			next_time = max(time + right - left, A[right]) + 1;
		}

		return Agent(next_time, next_left, next_right, next_place);
	}

	Agent nextLeft() const
	{
		int next_time;
		int next_left = left + 1;
		int next_right = right;
		Position next_place = LEFT;

		if (place == RIGHT)
		{
			next_time = max(time + right - left, A[left]) + 1;
		}
		else
		{
			next_time = max(time, A[left]) + 1;
		}

		return Agent(next_time, next_left, next_right, next_place);
	}

	bool finished() const
	{
		return left >= right;
	}

	int finalTime() const
	{
		return max(time, A[left]);
	}

	int toInt() const
	{
		return left + 10000 * (right + 10000 * place);
	}

	bool operator<(const Agent& rhs) const
	{
		return this->time > rhs.time;
	}
};

priority_queue<Agent> que;
set<int> visitedSet;

int main(void)
{
	cin >> N;
	for (int i = 0; i < N; ++i)
	{
		cin >> A[i];
	}

	que.push(Agent(/*time*/0, /*left*/0, /*right*/N - 1, RIGHT));
	que.push(Agent(/*time*/0, /*left*/0, /*right*/N - 1, LEFT));

	while (true)
	{
		Agent agent(que.top());
		que.pop();

		int hash = agent.toInt();
		if (visitedSet.find(hash) != visitedSet.end())
		{
			continue;
		}
		visitedSet.insert(hash);

		if (agent.finished())
		{
			cout << agent.finalTime() << endl;
			return 0;
		}

		que.push(agent.nextRight());
		que.push(agent.nextLeft());
	}
}
0