結果

問題 No.206 数の積集合を求めるクエリ
ユーザー startcppstartcpp
提出日時 2015-03-28 18:35:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4,822 ms / 7,000 ms
コード長 1,910 bytes
コンパイル時間 755 ms
コンパイル使用メモリ 67,112 KB
実行使用メモリ 5,584 KB
最終ジャッジ日時 2023-09-11 10:50:50
合計ジャッジ時間 38,743 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 3 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 5 ms
4,384 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 8 ms
4,380 KB
testcase_13 AC 6 ms
4,384 KB
testcase_14 AC 4 ms
4,384 KB
testcase_15 AC 3 ms
4,384 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 4,804 ms
5,512 KB
testcase_18 AC 1,227 ms
4,612 KB
testcase_19 AC 3,896 ms
5,396 KB
testcase_20 AC 20 ms
4,696 KB
testcase_21 AC 918 ms
4,908 KB
testcase_22 AC 1,212 ms
4,820 KB
testcase_23 AC 4,117 ms
5,500 KB
testcase_24 AC 4,822 ms
5,584 KB
testcase_25 AC 4,449 ms
5,440 KB
testcase_26 AC 1,239 ms
4,836 KB
testcase_27 AC 1,455 ms
4,820 KB
testcase_28 AC 2,930 ms
5,036 KB
testcase_29 AC 2,689 ms
5,024 KB
testcase_30 AC 1,955 ms
4,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<vector>
#define DB(x) 
using namespace std;

class AandBProblem {
private:
	int L, M, N;
	int A[100000], B[100000];
	int Q;

public:
	void input(int l, int m, int n, int *a, int *b, int q) {
		L = l;
		M = m;
		N = n;
		for(int i = 0; i < l; i++) A[i] = a[i];
		for(int i = 0; i < m; i++) B[i] = b[i];
		Q = q;
	}

	//O(N(L+M)Q)
	vector<int> solve1() {
		int i, j, k;
		vector<int> ret;
		ret.resize(Q);
		for( i = 0; i < Q; i++ ) {
			ret[i] = 0;
			for( j = 1; j <= N; j++ ) {
				for( k = 0; k < L; k++ )
					if ( A[k] == j ) break;
				if ( k == L ) continue;
				for( k = 0; k < M; k++ )
					if ( B[k] + i == j ) break;
				if ( k == M ) continue;
				ret[i]++;
			}
		}
		return ret;
	}
	//O(NQ)の定数1なやつ。
	vector<int> solve2() {
		int i, j;
		vector<int> ret;
		ret.resize(Q);
		for( i = 0; i < Q; i++ ) {
			ret[i] = 0;
			vector<int> paket;
			paket.resize(N+1+i);
			for( j = 0; j < N+1+i; j++ ) paket[j] = 0;
			
			for( j = 0; j < L; j++ ) {
				paket[A[j]]++;
			}
			for( j = 0; j < M; j++ ) {
				paket[B[j]+i]++;
			}

			for( j = 0; j < N+1; j++ ) {
				if ( paket[j] == 2 )
					ret[i]++;
			}
		}
		return ret;
	}

	//O(LM)の定数ちょっと速いやつ
	vector<int> solve3() {
		int i, j;
		int ans[100001] = {0};
		sort(A, A+L);
		sort(B, B+M);

		for( i = 0; i < L; i++ ) {
			for( j = 0; j < M; j++ ) {
				if ( B[j] > A[i] ) break;
				ans[ A[i] - B[j] ]++;
			}
		}

		vector<int> ret;
		for( i = 0; i < Q; i++ )
			ret.push_back(ans[i]);
		return ret;
	}

}test;

int L, M, N;
int A[100000], B[100000];
int Q;

int main() {
	scanf("%d%d%d", &L, &M, &N);
	for(int i = 0; i < L; i++ ) scanf("%d", A+i);
	for(int i = 0; i < M; i++ ) scanf("%d", B+i);
	scanf("%d", &Q);
	test.input(L, M, N, A, B, Q);

	vector<int> res3 = test.solve3();
	for(int i = 0; i < Q; i++ )
		printf("%d\n", res3[i]);
	return 0;
}
0