Guest User

Untitled

a guest
Sep 15th, 2017
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define all(x) x.begin(), x.end()
  6.  
  7. typedef long long ll;
  8. typedef long double ld;
  9.  
  10. const int N = 123456;
  11. const int M = 1234567;
  12. const int P = 400004;
  13. const int A = 256;
  14.  
  15. int d, k;
  16. vector<ll> clusters, assigned[A];
  17. vector<pair<pair<ll, ll>, int>> points;
  18.  
  19. int mn, mx;
  20.  
  21. int randy(){
  22.     return (rand() % (mx - mn + 1)) + mn;
  23. }
  24.  
  25. ll minimize(){
  26.  
  27.     sort(all(clusters));
  28.  
  29.     // for(auto x : clusters)
  30.     //  cout << "cluster " << x << endl;
  31.  
  32.     for(int i = 0; i < d; ++i){
  33.         int bst = lower_bound(all(clusters), points[i].first.first) - clusters.begin();
  34.         if(bst == clusters.size())
  35.             bst--;
  36.         if(bst - 1 >= 0 && abs(points[i].first.first - clusters[bst - 1]) < abs(points[i].first.first - clusters[bst]))
  37.             bst--;      
  38.  
  39.         bst = max(0, bst);
  40.  
  41.         if(bst > 255) throw runtime_error("FAILED");
  42.  
  43.         points[i].second = bst;
  44.  
  45.         assigned[bst].push_back(i);
  46.  
  47.         //cout << i << "  " << bst << endl;
  48.     }
  49.  
  50.  
  51.     for(int i = 0; i < k; ++i){
  52.  
  53.         if(assigned[i].size()){
  54.  
  55.             ll tot = 0, pixels = 0;
  56.  
  57.             for(auto x : assigned[i]){
  58.                 pixels += points[x].first.second;
  59.                 tot += points[x].first.second * points[x].first.first;
  60.             }
  61.  
  62.             tot /= pixels;
  63.             clusters[i] = tot;
  64.         }
  65.         else
  66.             clusters[i] = randy();
  67.  
  68.         assigned[i].clear();
  69.     }
  70.  
  71.  
  72.     ll ans = 0;
  73.     for(int i = 0; i < d; ++i){
  74.         ll tt = abs(points[i].first.first - clusters[points[i].second]);
  75.         tt *= tt;
  76.         ans += points[i].first.second * tt;
  77.     }
  78.  
  79.     return ans;
  80. }
  81.  
  82.  
  83. int main(int argc, char const *argv[])
  84. {
  85.    
  86.     #ifdef Cyborg1o1
  87.         freopen("input.txt", "r", stdin);
  88.         freopen("output.txt", "w", stdout);
  89.     #endif  
  90.  
  91.     cin.sync_with_stdio(0);
  92.     cin.tie(0);
  93.     cout.tie(0);
  94.  
  95.  
  96.     cin >> d >> k;
  97.  
  98.  
  99.     points.resize(d);
  100.     clusters.resize(k);
  101.  
  102.     for(int i = 0; i < d; ++i){
  103.         int r, p;
  104.         cin >> r >> p;
  105.         points[i] = make_pair(make_pair(r, p), 0);
  106.     }
  107.  
  108.     mn = points[0].first.first;
  109.     mx = points[d - 1].first.first;
  110.  
  111.     srand(time(0));
  112.  
  113.     for(int i = 0; i < k; ++i){
  114.         clusters[i] = randy();
  115.     }
  116.    
  117.     ll a = minimize();
  118.     ll b = minimize();
  119.  
  120.     while(a > b){
  121.         a = minimize();
  122.         b = minimize();
  123.     }
  124.    
  125.     cout << a << endl;
  126.  
  127.     return 0;                  
  128. }
Add Comment
Please, Sign In to add comment