EntropyIncreaser

CodeForces 456D Fishes

Jan 6th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.40 KB | None | 0 0
  1. #include <cstdio>
  2.  
  3. #include <set>
  4.  
  5. #define LOG(FMT...) // fprintf(stderr, "[LOG] "FMT)
  6.  
  7. using namespace std;
  8.  
  9. typedef long long ll;
  10.  
  11. struct tpair {
  12.     ll s;
  13.     int x, y;
  14.  
  15.     tpair() {}
  16.  
  17.     tpair(ll s, int x, int y) : s(s), x(x), y(y) {}
  18.  
  19.     bool operator>(const tpair& a) const;
  20. };
  21.  
  22. int n, m, r, k;
  23.  
  24. ll calc_s(int x, int n);
  25. ll calc_sum(int x, int n);
  26.  
  27. int main() {
  28.     set<tpair, greater<tpair> > st;
  29.     ll whole, cnt = 0;
  30.     scanf("%d%d%d%d", &n, &m, &r, &k);
  31.     whole = (ll)(n - r + 1) * (m - r + 1);
  32.     ll s;
  33.     int x, y;
  34.     x = (n + 1) / 2;
  35.     y = (m + 1) / 2;
  36.     s = calc_s(x, n) * calc_s(y, m);
  37.     st.insert(tpair(s, x, y));
  38.     while (k > 0) {
  39.         tpair tmp = *st.begin();
  40.         st.erase(st.begin());
  41.         x = tmp.x;
  42.         y = tmp.y;
  43.         s = tmp.s;
  44.         ll sum = calc_sum(x, n) * calc_sum(y, m);
  45.         if (sum >= k) {
  46.             cnt += k * s;
  47.             k = 0;
  48.         } else {
  49.             LOG("%lld %lld\n", sum, s);
  50.             k -= sum;
  51.             cnt += sum * s;
  52.             if (x > 1)
  53.                 st.insert(tpair(calc_s(x - 1, n) * calc_s(y, m), x - 1, y));
  54.             if (y > 1)
  55.                 st.insert(tpair(calc_s(x, n) * calc_s(y - 1, m), x, y - 1));
  56.         }
  57.     }
  58.     printf("%.9lf\n", (double)cnt / whole);
  59.     return 0;
  60. }
  61.  
  62. ll calc_s(int x, int n) {
  63.     return min(x, min(n - r + 1, r));
  64. }
  65.  
  66. ll calc_sum(int x, int n) {
  67.     if (x == n - x + 1)
  68.         return 1;
  69.     return 2;
  70. }
  71.  
  72. bool tpair::operator>(const tpair& a) const {
  73.     if (s != a.s)
  74.         return s > a.s;
  75.     if (x != a.x)
  76.         return x > a.x;
  77.     return y > a.y;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment