結果

問題 No.335 門松宝くじ
ユーザー FF256grhyFF256grhy
提出日時 2017-07-25 21:10:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 149 ms / 2,000 ms
コード長 3,923 bytes
コンパイル時間 1,413 ms
コンパイル使用メモリ 161,892 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-17 23:35:15
合計ジャッジ時間 2,607 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 17 ms
6,944 KB
testcase_06 AC 23 ms
6,944 KB
testcase_07 AC 42 ms
6,944 KB
testcase_08 AC 149 ms
6,944 KB
testcase_09 AC 61 ms
6,940 KB
testcase_10 AC 60 ms
6,940 KB
testcase_11 AC 60 ms
6,940 KB
testcase_12 AC 61 ms
6,940 KB
testcase_13 AC 61 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef long long   signed int LL;
typedef long long unsigned int LU;

#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 inII(v, l, r) ((l) <= (v) && (v) <= (r))
#define inID(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 UB upper_bound
#define LB lower_bound
#define PQ priority_queue

#define  ALL(v)  v.begin(),  v.end()
#define RALL(v) v.rbegin(), v.rend()
#define  FOR(it, v) for(auto it =  v.begin(); it !=  v.end(); ++it)
#define RFOR(it, v) for(auto it = v.rbegin(); it != v.rend(); ++it)

template<typename T> bool   setmin(T & a, T b) { if(b <  a) { a = b; return true; } else { return false; } }
template<typename T> bool   setmax(T & a, T b) { if(b >  a) { a = b; return true; } else { return false; } }
template<typename T> bool setmineq(T & a, T b) { if(b <= a) { a = b; return true; } else { return false; } }
template<typename T> bool setmaxeq(T & a, T b) { if(b >= a) { a = b; return true; } else { return false; } }
template<typename T> T gcd(T a, T b) { return (b == 0 ? a : gcd(b, a % b)); }
template<typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }

// ---- ----

template<typename T> class SegTree {
private:
	T * a = NULL;
	int M, S;
	T (*F)(T, T);
	T I;

public:
	// [0, size) が使用可能になる、func が演算、id は単位元
	void init(int size, T (*func)(T, T), T id) {
		assert(size > 0);
		M = size;
		F = func;
		I = id;
		S = 1;
		while(S < size) { S *= 2; }
		delete[] a;
		a = new T[S * 2];
		inc(i, S * 2) { a[i] = I; }
	}
	
	// 参照(値を変更した後は calc() を呼ぶこと)
	T & operator[](int p) {
		assert(inID(p, 0, M));
		p += S;
		return a[p];
	}
	
	// テーブルの再計算
	void calc() {
		decID(i, 1, S) {
			a[i] = F(a[i * 2], a[i * 2 + 1]);
		}
	}
	
	// a[p] を v に変更し再計算
	void update(int p, T v) {
		assert(inID(p, 0, M));
		p += S;
		a[p] = v;
		while(p != 1) {
			p /= 2;
			a[p] = F(a[p * 2], a[p * 2 + 1]);
		}
	}
	
	// [l, r) に f を適用した結果を返す
	T range(int l, int r) {
		assert(inII(l, 0, M));
		assert(inII(r, 0, M));
		// if(l > r) { return F(range(l, M), range(0, r)); }
		assert(l <= r);
		l += S;
		r += S;
		
		T v = I, w = I;
		while(l < r) {
			if(l + 1 == r) { v = F(v, a[l]); break; }
			
			if(l % 2 == 1) { v = F(v, a[l]); }
			if(r % 2 == 1) { w = F(a[r - 1], w); }
			
			l = (l + 1) / 2;
			r = r / 2;
		}
		
		return F(v, w);
	}
};

template<typename T> T MIN(T x, T y) { return min(x, y); }
template<typename T> T MAX(T x, T y) { return max(x, y); }

// ---- ---- ---- ----

int n, m, e[800];

int main() {
	cin >> n >> m;
	
	int ans_v = 0, ans_q = 0;
	inc(q, m) {
		inc(i, n) { cin >> e[i]; }
		
		SegTree<int> st_max, st_min;
		st_max.init(n, MAX, 0);
		st_min.init(n, MIN, n + 1);
		
		inc(i, n) { st_max[i] = st_min[i] = e[i]; }
		st_max.calc();
		st_min.calc();
		
		int v = 0;
		inc(j, n) {
		inc(i, j) {
			if(e[i] < e[j]) {
				int ma = st_max.range(0, j);
				if(ma > e[j]) {
					v += ma;
				} else {
					if(
						st_max.range(0    , i) > e[i] ||
						st_min.range(i + 1, j) < e[i] ||
						st_min.range(j + 1, n) < e[j]
					) { v += e[j]; }
				}
			} else {
				int ma = st_max.range(i + 1, n);
				if(ma > e[i]) {
					v += ma;
				} else {
					if(
						st_max.range(j + 1, n) > e[j] ||
						st_min.range(i + 1, j) < e[j] ||
						st_min.range(0    , i) < e[i]
					) { v += e[i]; }
				}
			}
		}
		}
		
		if(setmax(ans_v, v)) { ans_q = q; }
	}
	
	cout << ans_q << endl;
	
	return 0;
}
0