Codeforces 863B Kayaking 暴力 水题
传送门:http://codeforces.com/problemset/problem/863/B
啊啊啊啊啊……做的时候卡了好久啊,想多了,还觉得是dp啥的……其实就是个暴力……
暴力选两个人乘坐单人船,剩下的排序以后分为两组,记录差的最小值即可。
#include<iostream> #include<string> #include<string.h> #include<cmath> #include<algorithm> #include<map> using namespace std; typedef long long ll; int n,sum,ans; int a[105],c[105]; int main() { while (cin >> n) { sum = 0; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; sum += a[i]; } sort(a, a + 2 * n); ans = 1e9; for (int i = 0; i < 2 * n; i++) { for (int j = i+1; j < 2 * n; j++) { int cur = 0,b=0; sum -= a[i]; sum -= a[j]; for (int k = 0; k < 2 * n; k++) { if (k != i&&k != j) c[cur++] = a[k]; } for (int k = 0; k < 2 * n-2; k+=2) b += c[k]; ans = min(ans, abs(sum - b - b)); sum += a[i] + a[j]; } } cout << ans << endl; } return 0; }
#include<iostream> #include<string> #include<string.h> #include<cmath> #include<algorithm> #include<map> using namespace std; typedef long long ll; int n,sum,ans; int a[105],c[105]; int main() { while (cin >> n) { sum = 0; for (int i = 0; i < 2 * n; i++) { cin >> a[i]; sum += a[i]; } sort(a, a + 2 * n); ans = 1e9; for (int i = 0; i < 2 * n; i++) { for (int j = i+1; j < 2 * n; j++) { int cur = 0,b=0; sum -= a[i]; sum -= a[j]; for (int k = 0; k < 2 * n; k++) { if (k != i&&k != j) c[cur++] = a[k]; } for (int k = 0; k < 2 * n-2; k+=2) b += c[k]; ans = min(ans, abs(sum - b - b)); sum += a[i] + a[j]; } } cout << ans << endl; } return 0; }