Advertisement
Guest User

Untitled

a guest
May 24th, 2015
322
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <list>
  2. #include <map>
  3. #include <set>
  4. #include <deque>
  5. #include <stack>
  6. #include <queue>
  7. #include <algorithm>
  8. #include <sstream>
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cstdio>
  12. #include <cmath>
  13. #include <cstdlib>
  14. #include <memory.h>
  15. #include <ctime>
  16. #include <bitset>
  17. #include <unordered_map>
  18. #include <unordered_set>
  19.  
  20. using namespace std;
  21.  
  22. #define ABS(a) ((a>0)?a:-(a))
  23. #define MIN(a,b) ((a<b)?(a):(b))
  24. #define MAX(a,b) ((a<b)?(b):(a))
  25. #define FOR(i,a,n) for (int i=(a);i<(n);++i)
  26. #define FI(i,n) for (int i=0; i<(n); ++i)
  27. #define pnt pair <int, int>
  28. #define mp make_pair
  29. #define PI 3.1415926535897
  30. #define MEMS(a,b) memset(a,b,sizeof(a))
  31. #define LL long long
  32. #define U unsigned
  33.  
  34. vector<pair<pnt, int> > a;
  35. vector<pair<pnt, int> > b;
  36. bool cmp(pair<pnt, int> X, pair<pnt, int> Y)
  37. {
  38.     pnt x = X.first;
  39.     pnt y = Y.first;
  40.     return (atan2(x.second, x.first) < atan2(y.second, y.first));
  41. }
  42.  
  43. inline int getCross(int x1, int y1, int x2, int y2, int x3, int y3)
  44. {
  45.     int X1 = x2 - x1;
  46.     int Y1 = y2 - y1;
  47.     int X2 = x3 - x1;
  48.     int Y2 = y3 - y1;
  49.     return X1*Y2 - X2*Y1;
  50. }
  51.  
  52. int solve(int v)
  53. {
  54.     int n = a.size();
  55.     int cnt = 0;
  56.     FOR(i, 0, n)
  57.     {
  58.         if (i != v)
  59.         {
  60.             b[cnt] = a[i];
  61.             b[cnt].first.first -= a[v].first.first;
  62.             b[cnt].first.second -= a[v].first.second;
  63.             cnt++;
  64.         }
  65.     }
  66.     sort(b.begin(), b.end(), cmp);
  67.     int sum = 0;
  68.     FOR(i, 0, b.size())
  69.     {
  70.         b[i].first.first += a[v].first.first;
  71.         b[i].first.second += a[v].first.second;
  72.         sum += b[i].second;
  73.     }
  74.     int who = 0;
  75.     int cursum = 0;
  76.     int res = 2000000000;
  77.     FOR(i, 0, b.size())
  78.     {
  79.         if (who != i)
  80.             cursum -= b[i].second;
  81.         if (who == i)
  82.         {
  83.             who = MAX(who, i + 1);
  84.             if (who == n - 1)
  85.                 who = 0;
  86.         }
  87.         while (who != i)
  88.         {
  89.             int val = getCross(a[v].first.first, a[v].first.second, b[i].first.first, b[i].first.second,
  90.                 b[who].first.first, b[who].first.second);
  91.             if (val > 0)
  92.             {
  93.                 cursum += b[who].second;
  94.                 who++;
  95.                 if (who == n - 1)
  96.                     who = 0;
  97.             }
  98.             else
  99.                 break;
  100.         }
  101.         int sum1 = cursum;
  102.         int sum2 = sum - cursum - b[i].second;
  103.         int sum3 = a[v].second;
  104.         int sum4 = b[i].second;
  105.         res = MIN(res, ABS(sum1 - sum2 + sum3 + sum4));
  106.         res = MIN(res, ABS(sum1 - sum2 + sum3 - sum4));
  107.         res = MIN(res, ABS(sum1 - sum2 - sum3 + sum4));
  108.         res = MIN(res, ABS(sum1 - sum2 - sum3 - sum4));
  109.     }
  110.     return res;
  111. }
  112.  
  113. int main()
  114. {
  115. #ifdef Fcdkbear
  116.     freopen("in.txt", "r", stdin);
  117.     //freopen("out.txt", "w", stdout);
  118.     double beg = clock();
  119. #endif
  120.  
  121.     int n;
  122.     scanf("%d", &n);
  123.     a.resize(n);
  124.     b.resize(n - 1);
  125.     FOR(i, 0, n)
  126.         scanf("%d%d%d", &a[i].first.first, &a[i].first.second, &a[i].second);
  127.     if (n == 1)
  128.     {
  129.         cout << a[0].second << endl;
  130.         return 0;
  131.     }
  132.     int res = 2000000000;
  133.     FOR(i, 0, n)
  134.     {
  135.         res = min(res, solve(i));
  136.     }
  137.     cout << res << endl;
  138.  
  139. #ifdef Fcdkbear
  140.     double end = clock();
  141.     fprintf(stderr, "*** Total time = %.3lf ***\n", (end - beg) / CLOCKS_PER_SEC);
  142. #endif
  143.     return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement