B. Candy Boxes
Problem's Link: http://codeforces.com/contest/488/problem/B
Mean:
analyse:
这道题还是很有意思的,需要考虑到各种情况才能AC。
解这个题目之前,首先要推出两条式子
x4=3x1
4x1=x2+x3
然后就是分类讨论,枚举各种情况就可。
Time complexity: O(1)
Source code:
// Memory Time // 1347K 0MS // by : Snarl_jsb // 2014-11-26-21.20 #include<algorithm> #include<cstdio> #include<cstring> #include<cstdlib> #include<iostream> #include<vector> #include<queue> #include<stack> #include<map> #include<string> #include<climits> #include<cmath> #define N 1000010 #define LL long long using namespace std; int ans[10], input[10]; bool flag = true; bool Solve(int n) { if (n == 0) { puts("YES"); printf("%d %d %d %d ", 1, 1, 3, 3); } if (n == 1) { puts("YES"); ans[1] = input[1], ans[4] = ans[1] * 3, ans[2] = ans[1], ans[3] = ans[4]; for (int i = 2; i <= 4; i++) printf("%d ", ans[i]); } if (n == 2) { if (input[2] % 3) { if (input[1] * 3 < input[2]) return flag = false; puts("YES"); ans[1] = input[1], ans[4] = ans[1] * 3; int tmp = 4 * ans[1] - input[2]; printf("%d %d ", ans[4], tmp); } else { if (input[2] / 3 > input[1]) return flag = false; puts("YES"); ans[1] = input[2] / 3; int tmp = 4 * ans[1] - input[1]; printf("%d %d ", ans[1], tmp); } } if (n == 3) { if (input[3] % 3) { if (input[1] * 3 < input[3]) return flag = false; for (int i = 1; i <= 3; i++) ans[i] = input[i]; ans[4] = ans[1] * 3; if (4 * ans[1] != ans[2] + ans[3]) return flag = false; puts("YES"); printf("%d ", ans[4]); } else { if (input[3] / 3 > input[1]) return flag = false; if (input[3] / 3 == input[1]) { puts("YES"); printf("%d ", input[1] * 4 - input[2]); return true; } ans[1] = input[3] / 3, ans[4] = input[3], ans[2] = input[1], ans[3] = input[2]; if (4 * ans[1] != ans[2] + ans[3]) return flag = false; puts("YES"); printf("%d ", ans[1]); } } if (n == 4) { for (int i = 1; i <= 4; i++) ans[i] = input[i]; if (4 * ans[1] == ans[2] + ans[3]) puts("YES"); else flag = false; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); // freopen("C:\Users\ASUS\Desktop\cin.cpp","r",stdin); // freopen("C:\Users\ASUS\Desktop\cout.cpp","w",stdout); int n, i, j; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &input[i]); sort(input + 1, input + n + 1); Solve(n); if (!flag) { puts("NO"); return 0; } return 0; } /* */