nickel2halide

Baleshare

Jan 14th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #define PROG "baleshare"
  2. #define NDEBUG
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <math.h>
  7. #include <string.h>
  8. #include <assert.h>
  9.  
  10. #include <algorithm>
  11. #include <numeric>
  12. #include <iterator>
  13. #include <vector>
  14. #include <list>
  15. #include <stack>
  16. #include <queue>
  17. #include <set>
  18. #include <string>
  19. #include <complex>
  20.  
  21. using namespace std;
  22.  
  23. #define DMP(_fmt, ...) fprintf(stderr, "[file %s, line %d]:" _fmt, __FILE__, __LINE__, __VA_ARGS__)
  24. #define REP(i, n) for(int i = 0; i < (n); ++i)
  25.  
  26. typedef list<int>::iterator liit;
  27. typedef pair<int, int> pii;
  28. typedef unsigned long long ull;
  29. typedef complex<double> pt;
  30.  
  31. FILE *fin, *fout;
  32.  
  33. // constants and givens/pseudo-constants
  34. // const int MAXN = 1005;
  35. // int N;
  36. // http://planetmath.org/encyclopedia/GoodHashTablePrimes.html
  37. // ^ should be obvious what this is for
  38. // const int NHASH = 193;
  39. // const int NHASH = 6151;
  40. // const int NHASH = 24593;
  41. // const int NHASH = 393241;
  42. const int INF = 999999999;
  43. const int MAXN = 25, MAXS = 105;
  44. int N;
  45. int S[MAXN];
  46.  
  47. // variables
  48. // int nfoo, a[MAXN];
  49. int total, minB;
  50. bool dp[MAXN * MAXS][MAXN * MAXS];
  51.  
  52. void init();
  53. void solve();
  54. void init();
  55. void input();
  56. void output();
  57.  
  58. void solve() {
  59.     init();
  60.  
  61.     dp[0][0] = true;
  62.     REP(i, N) {
  63.         for (int w1 = total; w1 >= 0; --w1) {
  64.             for (int w2 = total; max(w1, w2) >= S[i] && w2 >= 0; --w2) {
  65.                 if (w1 >= S[i]) {
  66.                     dp[w1][w2] |= dp[w1 - S[i]][w2];
  67.                 }
  68.                 if (w2 >= S[i]) {
  69.                     dp[w1][w2] |= dp[w1][w2 - S[i]];
  70.                 }
  71.             }
  72.         }
  73.     }
  74.  
  75.     minB = INF;
  76.     REP(w1, total) {
  77.         REP(w2, total) {
  78.             if (dp[w1][w2]) {
  79.                 assert(w1 + w2 <= total);
  80.                 minB = min(minB, max(w1, max(w2, total - w1 - w2)));
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86. void init() {
  87.     sort(S, S+N);
  88.     total = accumulate(S, S+N, 0);
  89. }
  90.  
  91. void input() {
  92.     // fscanf(fin, "%d", &N);
  93.     // for(int i = 0; i < N; ++i) {
  94.     //   fscanf(fin, "%d", a+i);
  95.     // }
  96.     fscanf(fin, "%d", &N);
  97.     REP(i, N) {
  98.         fscanf(fin, "%d", S+i);
  99.     }
  100. }
  101.  
  102. void output() {
  103.     // fprintf(fout, "%d\n", nfoo);
  104.     fprintf(fout, "%d\n", minB);
  105. }
  106.  
  107. int main()
  108. {
  109.     fin = fopen(PROG ".in", "r");
  110.     fout = fopen(PROG ".out", "w");
  111.     assert(fin != NULL);
  112.     assert(fout != NULL);
  113.  
  114.     input();
  115.     solve();
  116.     output();
  117.  
  118.     fclose(fin);
  119.     fclose(fout);
  120.  
  121.     exit(0);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment