結果

問題 No.335 門松宝くじ
ユーザー kurenaifkurenaif
提出日時 2016-02-12 16:28:41
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 297 ms / 2,000 ms
コード長 3,048 bytes
コンパイル時間 908 ms
コンパイル使用メモリ 80,392 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 02:35:50
合計ジャッジ時間 3,641 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 74 ms
4,348 KB
testcase_06 AC 110 ms
4,348 KB
testcase_07 AC 197 ms
4,348 KB
testcase_08 AC 186 ms
4,348 KB
testcase_09 AC 297 ms
4,348 KB
testcase_10 AC 293 ms
4,348 KB
testcase_11 AC 294 ms
4,348 KB
testcase_12 AC 294 ms
4,348 KB
testcase_13 AC 293 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
#include <map>
#include <list>
#include <vector>
#include <string>
#include <limits>

using namespace std;

#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)

#define inf INT_MAX/3
#define INF INT_MAX/3
#define PB push_back
#define MP make_pair
#define ALL(a) (a).begin(),(a).end()
#define SET(a,c) memset(a,c,sizeof a)
#define CLR(a) memset(a,0,sizeof a)
#define pii pair<int,int>
#define pcc pair<char,char>
#define pic pair<int,char>
#define pci pair<char,int>
#define VS vector<string>
#define VI vector<int>
#define DEBUG(x) cout<<#x<<": "<<x<<endl
#define MIN(a,b) (a>b?b:a)
#define MAX(a,b) (a>b?a:b)
#define pi 2*acos(0.0)
#define INFILE() freopen("in0.txt","r",stdin)
#define OUTFILE()freopen("out0.txt","w",stdout)
#define in scanf
#define out printf
#define LL long long
#define ULL unsigned long long
#define eps 1e-14
#define FST first
#define SEC second

template<class T>
class segtree {
private:
	int n;
	vector<T> dat;

	T query(int a, int b, int k, int l, int r) {
		if (r <= a || b <= l) return numeric_limits<T>::max();

		if (a <= l & r <= b) return dat[k];
		else {
			T vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
			T vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
			return min(vl, vr);
		}
	}
public:
	segtree(int n_) {
		n = 1;
		while (n < n_) n *= 2;
		dat.resize(2 * n - 1);
		for (int i = 0; i < 2 * n - 1; ++i) dat[i] = numeric_limits<T>::max();
	}

	//kの値をaに変更
	void update(int k, int a) {
		k += n - 1;
		dat[k] = a;
		while (k > 0) {
			k = (k - 1) / 2;
			dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
		}
	}


	int query(int a, int b) {
		return query(a, b, 0, 0, n);
	}
};

int main(void) {
	int N, M;
	cin >> N >> M;
	pair<int, int> buy(0,0);
	REP(m, M) {
		int E[1000];
		segtree<int> smin(N);
		segtree<int> smax(N);

		REP(n, N) {
			int e; cin >> e;
			smin.update(n, e);
			smax.update(n, -e);
			E[n] = e;
		}

		int res = 0;
		for (int i = 0; i < N; ++i) {
			for (int j = i + 1; j < N; ++j) {
				int value = 0;
				bool inv = false;
				if (E[i] > E[j]) inv = true;
				if (!inv) {
					int a = -smax.query(0, i);
					if (a > E[i]) {
						value = max( value, E[j]);
						value = max( value, a);
					}

					a = smin.query(i + 1, j);
					if (a < E[i]) value = max(value, E[j]);

					a = -smax.query(i + 1, j);
					if (a > E[j]) value = max(value, a);

					a = smin.query(j + 1, N);
					if (a < E[j]) value = max(value, E[j]);
				}
				else {
					int a = smin.query(0, i);
					if (a < E[i]) value = max(value, E[i]);

					a = -smax.query(i + 1, j);
					if (a > E[i]) value = max(value, a);

					a = smin.query(i + 1, j);
					if (a < E[j]) value = max(value, E[i]);

					a = -smax.query(j + 1, N);
					if (a > E[j]) {
						value = max(value, E[i]);
						value = max(value, a);
					}
				}
				res += value;
			}
		}
		if (buy.second < res)
			buy = MP(m, res);
	}
	cout << buy.first << endl;
	return 0;
}
0