結果

問題 No.335 門松宝くじ
ユーザー tomatoma
提出日時 2019-09-24 23:16:06
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 162 ms / 2,000 ms
コード長 2,302 bytes
コンパイル時間 2,176 ms
コンパイル使用メモリ 192,920 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 18:04:57
合計ジャッジ時間 3,854 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 26 ms
4,348 KB
testcase_06 AC 38 ms
4,348 KB
testcase_07 AC 64 ms
4,348 KB
testcase_08 AC 162 ms
4,348 KB
testcase_09 AC 95 ms
4,348 KB
testcase_10 AC 94 ms
4,348 KB
testcase_11 AC 94 ms
4,348 KB
testcase_12 AC 94 ms
4,348 KB
testcase_13 AC 94 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
constexpr ll INF = 1ll << 60;

template<typename T>
class SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

ll estimate(vector<ll>& e) {
	// seg init
	const int N = e.size();
	auto f1 = [](ll a, ll b) {return max(a, b); };
	auto f2 = [](ll a, ll b) {return min(a, b); };
	SegmentTree<ll> seg_max(f1, -INF), seg_min(f2, INF);
	seg_max.build(e), seg_min.build(e);

	// map init
	map<ll, ll> mp;
	rep(i, N)mp[e[i]] = i;

	// query
	ll res = 0;
	REP(b, 1, N + 1)REP(a, 1, b) {
		ll l = mp[a], r = mp[b];
		ll tres = 0;
		if (l < r) {
			ll big = seg_max.query(0, r);
			if (big > b)tres = big;
			else {
				if (seg_max.query(0, l) > a ||
					seg_min.query(r, N) < b ||
					seg_min.query(l, N) < a)
					tres = b;
			}
		}
		else {
			swap(l, r);
			ll big = seg_max.query(l, N);
			if (big > b)tres = big;
			else {
				if (seg_max.query(r, N) > a ||
					seg_min.query(0, l) < b ||
					seg_min.query(0, r) < a)
					tres = b;
			}
		}
		res += tres;
	}
	return res;
}

int main()
{
	// input
	int N, M;
	cin >> N >> M;
	vector<vector<ll>> E(M, vector<ll>(N));
	rep(i, M)rep(j, N)cin >> E[i][j];

	vector<pair<ll, ll>> score;
	rep(i, M) {
		score.push_back({ estimate(E[i]),-i });
	}
	sort(score.begin(), score.end());
	cout << -score.back().second << endl;

	return 0;
}
0