結果

問題 No.165 四角で囲え!
ユーザー koyumeishikoyumeishi
提出日時 2015-03-13 01:33:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 239 ms / 5,000 ms
コード長 2,777 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 84,156 KB
実行使用メモリ 4,740 KB
最終ジャッジ日時 2023-08-26 23:35:57
合計ジャッジ時間 3,269 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 85 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 131 ms
4,428 KB
testcase_06 AC 35 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 55 ms
4,376 KB
testcase_12 AC 22 ms
4,376 KB
testcase_13 AC 25 ms
4,376 KB
testcase_14 AC 18 ms
4,384 KB
testcase_15 AC 15 ms
4,376 KB
testcase_16 AC 168 ms
4,568 KB
testcase_17 AC 108 ms
4,660 KB
testcase_18 AC 239 ms
4,696 KB
testcase_19 AC 139 ms
4,648 KB
testcase_20 AC 109 ms
4,740 KB
testcase_21 AC 109 ms
4,628 KB
testcase_22 AC 110 ms
4,684 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

int main(){
	int N,B;
	cin >> N >> B;

	vector<int> X(N), Y(N), P(N);
	for(int i=0; i<N; i++) cin >> X[i] >> Y[i] >> P[i];

	auto compress = [&](vector<int>& V){
		auto V__ = V;
		sort(V__.begin(), V__.end());
		V__.erase( unique(V__.begin(), V__.end()), V__.end());

		for(int i=0; i<N; i++){
			V[i] = lower_bound(V__.begin(), V__.end(), V[i]) - V__.begin();
		}
	};

	compress(X); compress(Y);

	int width = *max_element(X.begin(), X.end()) +1;
	int height = *max_element(Y.begin(), Y.end()) +1;

	vector<vector<int>> sum_p(width, vector<int>(height, 0));
	vector<vector<int>> sum_c(width, vector<int>(height, 0));
	for(int i=0; i<N; i++){
		sum_p[ X[i] ][ Y[i] ] += P[i];
		sum_c[ X[i] ][ Y[i] ] += 1;
	}

	for(int i=0; i<width; i++){
		for(int j=0; j<height; j++){
			if(j>0){
				sum_p[i][j] += sum_p[i][j-1];
				sum_c[i][j] += sum_c[i][j-1];
			}
			if(i>0){
				sum_p[i][j] += sum_p[i-1][j];
				sum_c[i][j] += sum_c[i-1][j];
			}
			if(i>0 && j>0){
				sum_p[i][j] -= sum_p[i-1][j-1];
				sum_c[i][j] -= sum_c[i-1][j-1];
			}
		}
	}

	auto chekcer = [&](int x, int y) -> bool {
		if(x<0 || x >= width || y<0 || y>=height) return false;
		return true;
	};

	//[ (a,b) , (c,d) ]
	auto get_points = [&](int a, int b, int c, int d){
		if(chekcer(a,b) && chekcer(c,d)){
			int ret = 0;
			ret += sum_p[c][d];
			if(a>0) ret -= sum_p[a-1][d];
			if(b>0) ret -= sum_p[c][b-1];
			if(a>0 && b>0) ret += sum_p[a-1][b-1];

			return ret;
		}else{
			return 1<<30;
		}
	};


	//[ (a,b) , (c,d) ]
	auto get_counts = [&](int a, int b, int c, int d){
		if(chekcer(a,b) && chekcer(c,d)){
			int ret = 0;
			ret += sum_c[c][d];
			if(a>0) ret -= sum_c[a-1][d];
			if(b>0) ret -= sum_c[c][b-1];
			if(a>0 && b>0) ret += sum_c[a-1][b-1];

			return ret;
		}else{
			return 0;
		}
	};

	int ans = 0;

/*
	for(int i=0; i<width; i++){
		for(int j=0; j<height; j++){
			for(int k=i; k<width; k++){
				int lb = j;
				int ub = height;

				while(ub-lb>1){
					int med = (lb+ub)/2;
					int val = get_points(i,j, k,med);
					if(val > B){
						ub = med;
					}else{
						lb = med;
					}
				}

				if(get_points(i,j, k,lb) <= B)	ans = max(ans, get_counts(i,j, k,lb));

			}
		}
	}
*/

	//[i,j]
	for(int i=0; i<width; i++){
		for(int j=i; j<width; j++){
			int tmp = 0;

			//[l,r]
			int l = 0;
			for(int r=0; r<height; r++){
				int p = get_points(i,l, j,r);
				if(p>B){
					while(l<=r && p>B){
						l++;
						if(l<=r) p = get_points(i,l, j,r);
					}
				}

				if(l<=r && p <= B){
					ans = max(ans, get_counts(i,l, j,r));
				}
			}
		}
	}
	cout << ans << endl;


	return 0;
}
0