Skip to content

Instantly share code, notes, and snippets.

@Leko
Created July 12, 2013 03:54
Show Gist options
  • Save Leko/5981280 to your computer and use it in GitHub Desktop.
Save Leko/5981280 to your computer and use it in GitHub Desktop.
AOJ 1153 Equal Totaol Score http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1153&lang=jp nが100以下なので全探索。
// start: 2:41
// AC: 3:02
#include <iostream>
#include <vector>
#include <queue>
#define REP(i, n) for ( int i = 0; i < n; i++ )
#define MP(a, b) make_pair(a, b)
#define MAX 101
using namespace std;
typedef pair<int, int> ab;
int sum(vector<int> arr) {
int ret = 0;
REP(i, arr.size()) ret += arr[i];
return ret;
}
bool flg = false;
int N, M;
ab all_search(vector<int> &a, vector<int> &b) {
int aa = MAX, bb = MAX;
REP(ai, N) REP(bi, M) {
// トレード
int tmp = a[ai];
a[ai] = b[bi];
b[bi] = tmp;
// 等しく、かつ最小
if ( sum(a) == sum(b) && a[ai] + b[bi] < aa + bb ) {
aa = b[bi];
bb = a[ai];
}
// 戻す
b[bi] = a[ai];
a[ai] = tmp;
}
return MP(aa, bb);
}
int main() {
while(cin >> N >> M, N||M) {
vector<int> a(N), b(M);
queue<ab> open;
REP(i, N) cin >> a[i];
REP(i, M) cin >> b[i];
ab ans = all_search(a, b);
if ( ans.first != MAX ) cout << ans.first << " " << ans.second << endl;
else cout << -1 << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment