Submission #402171

# Submission time Handle Problem Language Result Execution time Memory
402171 2021-05-11T11:52:16 Z erray Naan (JOI19_naan) C++14
100 / 100
3342 ms 100800 KB
// author: erray
#include <bits/stdc++.h>
 
using namespace std;
 template<typename A, typename B> string to_string(pair<A, B> p);
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t);
template<typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> t);

string to_string(string s) {
  return '"' + s + '"';
}

string to_string(char c) {
  return string("'") + c + "'";
}

string to_string(const char* c) {
  return to_string(string(c));
}

string to_string(bool b) {
  return (b ? "true" : "false");
}

template<size_t T> string to_string(bitset<T> bs) {
  return bs.to_string();
}

string to_string(vector<bool> v) {
  string res = "{";
  for (int i = 0; i < int(v.size()); ++i) {
    if (i > 0) {
      res += ", ";
    }
    res += to_string(v[i]);
  }
  res += "}";
  return res;
}

template<typename T> string to_string(T v) {
  string res = "{";
  for (auto& e : v) {
    if (int(res.size()) > 1) {
      res += ", ";
    }
    res += to_string(e);
  }
  res += "}";
  return res;
}

template<typename A, typename B> string to_string(pair<A, B> p) {
  return '(' + to_string(p.first) + ", " + to_string(p.second) + ')';
}
template<typename A, typename B, typename C> string to_string(tuple<A, B, C> t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + '}';
}
template<typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> t) {
  return '(' + to_string(get<0>(t)) + ", " + to_string(get<1>(t)) + ", " + to_string(get<2>(t)) + ", " + to_string(get<3>(t)) + '}';   
}

void debug_out() {
  cerr << endl;
}

template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << "  " << to_string(H);
  debug_out(T...);
}

#ifdef DEBUG 
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) void(37)
#endif

long long gcd(long long x, long long y) {
  return (y ? gcd(y, x % y) : x);
}
 
struct fract {
  long long t;
  long long b;
  void normalize() {
    long long d = gcd(t, b);
    t /= d;
    b /= d;
  }
  fract(long long _t = 0, long long _b = 1) : t(_t), b(_b) { normalize(); }
  bool operator<(const fract& ot) {
    long long d = gcd(b, ot.b);
    long long dt = gcd(t, ot.t);
    return __int128(1) * (t / dt) * (ot.b / d) < __int128(1) * (ot.t / dt) * (b / d);
  }
  bool operator==(const fract& ot) {
    return t * ot.b == ot.t * b;
  }
  void operator+=(const fract& ot) {
    long long d = gcd(b, ot.b);
    t = ot.b / d * t + b / d * ot.t;
    b = b / d * ot.b;
    normalize();
  }
};

fract operator/(const fract& me, const fract& ot) {    
  long long d = gcd(me.b, ot.b);
  long long dt = gcd(me.t, ot.t);
  return fract((me.t / dt) * (ot.b / d), (me.b / d) * (ot.t / dt));
}

string to_string(fract x) {
  return to_string(x.t) + "/" + to_string(x.b);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, l;
  cin >> n >> l;
  vector<vector<int>> v(n, vector<int>(l));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < l; ++j) {
      cin >> v[i][j];
    }
  }

  vector<vector<fract>> dv(n, vector<fract>(n - 1));
  for (int i = 0; i < n; ++i) {
    int sum = 0;
    int ind = 0;
    int tot = accumulate(v[i].begin(), v[i].end(), 0);
    fract one = fract(tot, n);
    fract now = one;
    debug(i);
    debug(tot);
    debug(one);
    for (int j = 0; j < n - 1; ++j) {
      while (fract(sum + v[i][ind]) < now) {
        assert(ind < l);
        debug(sum);
        debug(v[i][ind]);
        debug(now);
        sum += v[i][ind];
        ++ind;
      }
      fract left = now;
      left += fract(-sum);
      dv[i][j] = left / fract(v[i][ind]);
      dv[i][j] += fract(ind);
      now += one;
    }
    assert(now == fract(accumulate(v[i].begin(), v[i].end(), 0)));
  }

  debug(dv);


  vector<int> p(n);
  vector<fract> ans(n - 1);
  vector<bool> vis(n);
  for (int j = 0; j < n - 1; ++j) {
    int use = -1;
    for (int i = 0; i < n; ++i) {
      if (!vis[i] && (use == -1 || dv[i][j] < dv[use][j])) {
        use = i;
      }
    }
    vis[use] = true;
    p[j] = use;
    ans[j] = dv[use][j];
  }
  int left = 0;
  while (vis[left]) {
    ++left;
  }
  p[n - 1] = left;

  for (int i = 0; i < n - 1; ++i) {
    cout << ans[i].t << ' ' << ans[i].b << '\n';
  }
  for (int i = 0; i < n; ++i) {
    cout << p[i] + 1 << ' ';
  }
}                                                                        
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 2 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 368 KB Output is correct
6 Correct 2 ms 332 KB Output is correct
7 Correct 1 ms 328 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 2 ms 332 KB Output is correct
10 Correct 2 ms 332 KB Output is correct
11 Correct 2 ms 332 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 2 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 2 ms 328 KB Output is correct
18 Correct 3 ms 404 KB Output is correct
19 Correct 3 ms 332 KB Output is correct
20 Correct 2 ms 332 KB Output is correct
21 Correct 3 ms 332 KB Output is correct
22 Correct 2 ms 332 KB Output is correct
23 Correct 1 ms 204 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 1 ms 332 KB Output is correct
27 Correct 2 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 204 KB Output is correct
2 Correct 1 ms 204 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 204 KB Output is correct
6 Correct 1 ms 204 KB Output is correct
7 Correct 1 ms 204 KB Output is correct
8 Correct 1 ms 332 KB Output is correct
9 Correct 1 ms 204 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 204 KB Output is correct
12 Correct 1 ms 332 KB Output is correct
13 Correct 2 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 1 ms 332 KB Output is correct
16 Correct 1 ms 204 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 2 ms 332 KB Output is correct
19 Correct 2 ms 332 KB Output is correct
20 Correct 2 ms 368 KB Output is correct
21 Correct 2 ms 332 KB Output is correct
22 Correct 1 ms 328 KB Output is correct
23 Correct 1 ms 332 KB Output is correct
24 Correct 2 ms 332 KB Output is correct
25 Correct 2 ms 332 KB Output is correct
26 Correct 2 ms 332 KB Output is correct
27 Correct 1 ms 204 KB Output is correct
28 Correct 2 ms 332 KB Output is correct
29 Correct 2 ms 332 KB Output is correct
30 Correct 2 ms 332 KB Output is correct
31 Correct 2 ms 332 KB Output is correct
32 Correct 2 ms 328 KB Output is correct
33 Correct 3 ms 404 KB Output is correct
34 Correct 3 ms 332 KB Output is correct
35 Correct 2 ms 332 KB Output is correct
36 Correct 3 ms 332 KB Output is correct
37 Correct 2 ms 332 KB Output is correct
38 Correct 1 ms 204 KB Output is correct
39 Correct 2 ms 332 KB Output is correct
40 Correct 2 ms 332 KB Output is correct
41 Correct 1 ms 332 KB Output is correct
42 Correct 2 ms 332 KB Output is correct
43 Correct 293 ms 7852 KB Output is correct
44 Correct 1833 ms 46232 KB Output is correct
45 Correct 648 ms 20276 KB Output is correct
46 Correct 56 ms 2372 KB Output is correct
47 Correct 1069 ms 29144 KB Output is correct
48 Correct 2734 ms 60644 KB Output is correct
49 Correct 597 ms 15164 KB Output is correct
50 Correct 3315 ms 76748 KB Output is correct
51 Correct 1124 ms 27820 KB Output is correct
52 Correct 2771 ms 66084 KB Output is correct
53 Correct 2515 ms 58608 KB Output is correct
54 Correct 2 ms 332 KB Output is correct
55 Correct 1294 ms 32188 KB Output is correct
56 Correct 1503 ms 39456 KB Output is correct
57 Correct 1226 ms 33040 KB Output is correct
58 Correct 2195 ms 57328 KB Output is correct
59 Correct 1184 ms 32388 KB Output is correct
60 Correct 2739 ms 100568 KB Output is correct
61 Correct 2835 ms 100644 KB Output is correct
62 Correct 3342 ms 100160 KB Output is correct
63 Correct 3059 ms 100800 KB Output is correct
64 Correct 2859 ms 100792 KB Output is correct
65 Correct 3118 ms 87300 KB Output is correct
66 Correct 3133 ms 87492 KB Output is correct
67 Correct 2815 ms 87464 KB Output is correct
68 Correct 1192 ms 42732 KB Output is correct
69 Correct 1118 ms 40516 KB Output is correct
70 Correct 1507 ms 52640 KB Output is correct
71 Correct 1790 ms 63004 KB Output is correct