結果

問題 No.596 郵便配達
ユーザー FF256grhyFF256grhy
提出日時 2020-01-10 16:11:13
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,070 bytes
コンパイル時間 1,562 ms
コンパイル使用メモリ 170,200 KB
実行使用メモリ 62,268 KB
最終ジャッジ日時 2023-08-15 14:29:41
合計ジャッジ時間 6,283 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 9 ms
10,816 KB
testcase_01 AC 10 ms
10,932 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 275 ms
23,140 KB
testcase_05 AC 372 ms
62,268 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 349 ms
26,608 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 483 ms
57,916 KB
testcase_21 AC 467 ms
57,788 KB
testcase_22 AC 461 ms
57,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using LL = long long int;
#define incID(i, l, r) for(int i = (l)    ; i <  (r); ++i)
#define incII(i, l, r) for(int i = (l)    ; i <= (r); ++i)
#define decID(i, l, r) for(int i = (r) - 1; i >= (l); --i)
#define decII(i, l, r) for(int i = (r)    ; i >= (l); --i)
#define inc(i, n)  incID(i, 0, n)
#define inc1(i, n) incII(i, 1, n)
#define dec(i, n)  decID(i, 0, n)
#define dec1(i, n) decII(i, 1, n)
#define inID(v, l, r) ((l) <= (v) && (v) <  (r))
#define inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define FI first
#define SE second
#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
auto setmin   = [](auto & a, auto b) { return (b <  a ? a = b, true : false); };
auto setmax   = [](auto & a, auto b) { return (b >  a ? a = b, true : false); };
auto setmineq = [](auto & a, auto b) { return (b <= a ? a = b, true : false); };
auto setmaxeq = [](auto & a, auto b) { return (b >= a ? a = b, true : false); };
auto fl = [](auto a, auto b) { assert(b != 0); return a / b - (a % b != 0 && ((a >= 0) != (b >= 0)) ? 1 : 0); };
auto ce = [](auto a, auto b) { assert(b != 0); return a / b + (a % b != 0 && ((a >= 0) == (b >= 0)) ? 1 : 0); };
auto mo = [](auto a, auto b) { assert(b != 0); a %= b; if(a < 0) { a += abs(b); } return a; };
LL gcd(LL a, LL b) { return (b == 0 ? a : gcd(b, a % b)); }
LL lcm(LL a, LL b) { return a / gcd(a, b) * b; }
#define bit(b, i) (((b) >> (i)) & 1)
#define SI(v) static_cast<int>(v.size())
#define RF(e, v) for(auto & e: v)
#define until(e) while(! (e))
#define if_not(e) if(! (e))
#define ef else if
#define UR assert(false)

// ---- ----

bool all = false; // 現在地と宛先の座標が等しい郵便物も回収する必要があるか

int f(vector<int> const & v) {
	int n = SI(v);
	assert(n % 2 == 0);
	
	vector<int> d(n - 1), sl(n), sr(n);
	inc(i, n - 1) { if(i % 2 == 0) { d[i] = 2 * (v[i + 1] - v[i]); } }
	inc(i, n - 1) { sl[i + 1] = sl[i] + d[i]; }
	dec(i, n - 1) { sr[i] = sr[i + 1] + d[i]; }
	inc(i, n) { setmin(sl[i], v[i] - v.front()); }
	inc(i, n) { setmin(sr[i], v.back() - v[i]); }
	
	int ans = 1e9;
	inc(i, n) { setmin(ans, sl[i] + sr[i]); }
	assert(ans <= 2 * (v.back() - v.front()));
	
	return ans + v.back() - v.front();
}

int main() {
	int n, m;
	cin >> n >> m;
	vector<int> L(n + 1), R(n + 1);
	int mi = n, ma = -1;
	inc(i, m) {
		int x, d;
		cin >> x >> d;
		inc(j, d) {
			int y;
			cin >> y;
			if(all || x != y) {
				setmin(mi, min(x, y));
				setmax(ma, max(x, y));
			}
			if(y < x) { L[y]++; L[x]--; }
			if(x < y) { R[x]++; R[y]--; }
		}
	}
	inc(i, n) {
		L[i + 1] += L[i];
		R[i + 1] += R[i];
		if(L[i] != 0) { L[i] = 1; }
		if(R[i] != 0) { R[i] = 1; }
	}
	assert(L[n] == 0);
	assert(R[n] == 0);
	
	vector<int> vl, vr;
	int pl = 0, pr = 0;
	incII(i, 0, n) {
		if(i == mi || i == ma) { vl.PB(i); vl.PB(i); vr.PB(i); vr.PB(i); }
		if(L[i] != pl) { vl.PB(i); }
		if(R[i] != pr) { vr.PB(i); }
		pl = L[i];
		pr = R[i];
	}
	
	cout << min(f(vl), f(vr)) << endl;
	
	return 0;
}
0