結果

問題 No.602 隠されていたゲーム2
ユーザー NekosyndromeNekosyndrome
提出日時 2017-12-02 06:29:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 82 ms / 2,000 ms
コード長 2,676 bytes
コンパイル時間 2,703 ms
コンパイル使用メモリ 153,508 KB
実行使用メモリ 5,060 KB
最終ジャッジ日時 2023-08-22 05:37:13
合計ジャッジ時間 3,208 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 12 ms
4,380 KB
testcase_17 AC 25 ms
4,380 KB
testcase_18 AC 55 ms
4,848 KB
testcase_19 AC 82 ms
4,848 KB
testcase_20 AC 72 ms
5,060 KB
testcase_21 AC 47 ms
5,048 KB
testcase_22 AC 65 ms
4,932 KB
testcase_23 AC 48 ms
4,968 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
#define REP(x,y,z) for(int x=y;x<=z;x++)
#define FORD(x,y,z) for(int x=y;x>=z;x--)
#define MSET(x,y) memset(x,y,sizeof(x))
#define FOR(x,y) for(__typeof(y.begin()) x=y.begin();x!=y.end();x++)
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define SZ size()
#define M 100005
void RI(){}
template<typename... T>
void RI( int& head, T&... tail ) {
    scanf("%d",&head);
    RI(tail...);
}
using namespace std;
typedef long long LL;
int n;
LL d[M],xt,yt;
vector<LL> d2[2];

pair<LL,LL> dis(LL x,pair<LL,LL> y)
{
	assert(y.F<=y.S);
	if (x < y.F)
		return MP(y.F-x, y.S-x);
	if (x > y.S)
		return MP(x-y.S, x-y.F);
	return MP(
		0LL, 
		max(x-y.F, y.S-x)
	);
}
LL findBetween(LL x,LL y,int z) //[x,y]
{
	assert(0<=z && z<=1);
	assert(x<=y);
	int pos = lower_bound(d2[z].begin(), d2[z].end(), x) - d2[z].begin();
	if(pos<(int)d2[z].size() && d2[z][pos]<=y) return d2[z][pos];
	return -1;
}
int getans(LL dd)
{
	if(dd == max(abs(xt), abs(yt)))
		return 1;
	
	LL tmp, tmp2;
	pair<LL,LL> rng;
	int mod;
	
	// +-dd, ?
	mod = ((dd+yt)%2+2)%2;
	rng = dis(yt, MP(-dd, dd)); //dy
	if (rng.F%2 != mod) rng.F++;
	if (rng.S%2 != mod) { rng.S--; assert(0); }
	if (rng.F > rng.S) {
		assert(0);
		rng = MP(1LL<<62, (1LL<<62)*-1);
	}

	tmp2 = min(abs(xt-dd), abs(xt+dd));
	if (rng.S >= tmp2) {
		tmp = findBetween(max(rng.F,tmp2), rng.S, mod);
		if(tmp!=-1) return 2;
	}
	if (rng.F <= tmp2) {
		tmp = findBetween(tmp2, tmp2, tmp2%2);
		if(tmp!=-1) return 2;
	}
	tmp2 = max(abs(xt-dd), abs(xt+dd));
	if (rng.F <= tmp2) {
		tmp = findBetween(tmp2, tmp2, tmp2%2);
		if(tmp!=-1) return 2;
	}

	// ?, +-dd
	mod = ((dd+xt)%2+2)%2;
	rng = dis(xt, MP(-dd, dd));
	if (rng.F%2 != mod) rng.F++;
	if (rng.S%2 != mod) { rng.S--; assert(0); }
	if (rng.F > rng.S) {
		assert(0);
		rng = MP(1LL<<62, (1LL<<62)*-1);
	}
	
	tmp2 = min(abs(yt-dd), abs(yt+dd));
	if (rng.S >= tmp2) {
		tmp = findBetween(max(rng.F,tmp2), rng.S, mod);
		if(tmp!=-1) return 2;
	}
	if (rng.F <= tmp2) {
		tmp = findBetween(tmp2, tmp2, tmp2%2);
		if(tmp!=-1) return 2;
	}
	tmp2 = max(abs(yt-dd), abs(yt+dd));
	if (rng.F <= tmp2) {
		tmp = findBetween(tmp2, tmp2, tmp2%2);
		if(tmp!=-1) return 2;
	}

	return 3;
}
int main()
{
	while(~scanf("%d",&n))
	{
		d2[0].clear();
		d2[1].clear();
		REP(i,1,n) {
			cin >> d[i];
			d2[d[i]%2].PB(d[i]);
		}
		sort(d+1, d+n+1);
		sort(d2[0].begin(), d2[0].end());
		sort(d2[1].begin(), d2[1].end());

		cin >> xt >> yt;

		{
			LL x2 = xt;
			xt += yt;
			yt = x2 - yt;
		}

		if(xt==0 && yt==0) {
			puts("0");
			continue;
		}

		int ans=3;
		REP(i,1,n) ans = min(ans, getans(d[i]));
		if(ans==3) ans=-1;
		printf("%d\n", ans);
	}
	return 0;
}


0