Submission #101706

# Submission time Handle Problem Language Result Execution time Memory
101706 2019-03-19T14:21:08 Z Benq Akvizna (COCI19_akvizna) C++14
65 / 130
1500 ms 9868 KB
#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/rope>

using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
typedef double ll;
typedef long double ld;
typedef complex<ld> cd;

typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;

template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= (a); i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto& a : x)

#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define beg(x) x.begin()
#define en(x) x.end()
#define all(x) beg(x), en(x)
#define resz resize

const int MOD = 1000000007; // 998244353
const ll INF = 1e18;
const int MX = 100001;
const ld PI = 4*atan((ld)1);

template<class T> void ckmin(T &a, T b) { a = min(a, b); }
template<class T> void ckmax(T &a, T b) { a = max(a, b); }

template<class A, class B> A operator+=(A& l, const B& r) { return l = l+r; }
template<class A, class B> A operator-=(A& l, const B& r) { return l = l-r; }
template<class A, class B> A operator*=(A& l, const B& r) { return l = l*r; }
template<class A, class B> A operator/=(A& l, const B& r) { return l = l/r; }

namespace input {
    template<class T> void re(complex<T>& x);
    template<class T1, class T2> void re(pair<T1,T2>& p);
    template<class T> void re(vector<T>& a);
    template<class T, size_t SZ> void re(array<T,SZ>& a);

    template<class T> void re(T& x) { cin >> x; }
    void re(double& x) { string t; re(t); x = stod(t); }
    void re(ld& x) { string t; re(t); x = stold(t); }
    template<class Arg, class... Args> void re(Arg& first, Args&... rest) { 
        re(first); re(rest...); 
    }

    template<class T> void re(complex<T>& x) { T a,b; re(a,b); x = cd(a,b); }
    template<class T1, class T2> void re(pair<T1,T2>& p) { re(p.f,p.s); }
    template<class T> void re(vector<T>& a) { F0R(i,sz(a)) re(a[i]); }
    template<class T, size_t SZ> void re(array<T,SZ>& a) { F0R(i,SZ) re(a[i]); }
}

using namespace input;

namespace output {
    template<class T1, class T2> void pr(const pair<T1,T2>& x);
    template<class T, size_t SZ> void pr(const array<T,SZ>& x);
    template<class T> void pr(const vector<T>& x);
    template<class T> void pr(const set<T>& x);
    template<class T1, class T2> void pr(const map<T1,T2>& x);

    template<class T> void pr(const T& x) { cout << x; }
    template<class Arg, class... Args> void pr(const Arg& first, const Args&... rest) { 
        pr(first); pr(rest...); 
    }

    template<class T1, class T2> void pr(const pair<T1,T2>& x) { 
        pr("{",x.f,", ",x.s,"}"); 
    }
    template<class T> void prContain(const T& x) {
        pr("{");
        bool fst = 1; trav(a,x) pr(!fst?", ":"",a), fst = 0; 
        pr("}");
    }
    template<class T, size_t SZ> void pr(const array<T,SZ>& x) { prContain(x); }
    template<class T> void pr(const vector<T>& x) { prContain(x); }
    template<class T> void pr(const set<T>& x) { prContain(x); }
    template<class T1, class T2> void pr(const map<T1,T2>& x) { prContain(x); }
    
    void ps() { pr("\n"); } 
    template<class Arg, class... Args> void ps(const Arg& first, const Args&... rest) { 
        pr(first," "); ps(rest...); // print w/ spaces
    }
}

using namespace output;

namespace io {
    void setIn(string s) { freopen(s.c_str(),"r",stdin); }
    void setOut(string s) { freopen(s.c_str(),"w",stdout); }
    void setIO(string s = "") {
        ios_base::sync_with_stdio(0); cin.tie(0); // fast I/O
        if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO
    }
}

using namespace io;

bool Q;

struct Line {
	mutable ll k, m, p; // slope, y-intercept, last optimal x
	int num;
	bool operator<(const Line& o) const {
		return Q ? p < o.p : k < o.k;
	}
};

struct LineContainer : multiset<Line> { 
	const ll inf = LLONG_MAX;
	ll div(ll a, ll b) { // floored division
	    if (b < 0) a *= -1, b *= -1;
	    if (a >= 0) return a/b;
	    return -((-a+b-1)/b);
	}
	
	// updates x->p, determines if y is unneeded
	bool isect(iterator x, iterator y) { 
		if (y == end()) { x->p = inf; return 0; }
		if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
		else x->p = div(y->m - x->m, x->k - y->k);
		return x->p >= y->p;
	}
	
	void add(ll k, ll m, int num) {
		auto z = insert({k, m, 0,num}), y = z++, x = y;
		while (isect(y, z)) z = erase(z);
		if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
		while ((y = x) != begin() && (--x)->p >= y->p) isect(x, erase(y));
	}
	
	pair<ll,int> query(ll x) { 
		assert(!empty());
		Q = 1; auto l = *lb({0,0,x}); Q = 0;
		
		return {l.k * x + l.m, l.num};
	}
};

int N,K;

pair<ll,int> solve(double c) {
    LineContainer L;
    pair<ll,int> ans[MX];
    F0R(i,N+1) {
        if (i == 0) ans[i] = {0,0};
        else {
            ans[i] = L.query(i); 
            ans[i].s ++;
        }
        if (i < N) L.add(1.0/(N-i),ans[i].f-(double)i/(N-i)-c,ans[i].s); 
        // (j-i)/(N-i)
    }
    /*F0R(i,N+1) ps(i,ans[i]);
    ps();*/
    
    return ans[N];
}

int main() {
    setIO(); re(N,K);
    double lo = 0, hi = 2;
    F0R(i,60) {
        double mid = (lo+hi)/2;
        if (solve(mid).s >= K) lo = mid;
        else hi = mid;
    }
    auto t = solve(lo);
    cout << fixed << setprecision(10) << t.f+lo*K;
    // if >= go higher  
    // if <= go lower 
    // ps(solve(0),solve(0.2),solve(0.4),solve(0.6),solve(0.8));
}

/* stuff you should look for
    * int overflow, array bounds
    * special cases (n=1?), set tle
    * do smth instead of nothing and stay organized
*/

Compilation message

akvizna.cpp: In function 'void io::setIn(std::__cxx11::string)':
akvizna.cpp:117:35: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setIn(string s) { freopen(s.c_str(),"r",stdin); }
                            ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
akvizna.cpp: In function 'void io::setOut(std::__cxx11::string)':
akvizna.cpp:118:36: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     void setOut(string s) { freopen(s.c_str(),"w",stdout); }
                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1920 KB Output is correct
2 Correct 9 ms 1920 KB Output is correct
3 Correct 9 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1920 KB Output is correct
2 Correct 8 ms 1920 KB Output is correct
3 Correct 8 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 1920 KB Output is correct
2 Correct 10 ms 1984 KB Output is correct
3 Correct 11 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 1920 KB Output is correct
2 Correct 10 ms 1920 KB Output is correct
3 Correct 9 ms 1920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 35 ms 2176 KB Output is correct
2 Correct 35 ms 2176 KB Output is correct
3 Correct 31 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 32 ms 2176 KB Output is correct
2 Correct 33 ms 2048 KB Output is correct
3 Correct 30 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2176 KB Output is correct
2 Correct 32 ms 2132 KB Output is correct
3 Correct 35 ms 2168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2176 KB Output is correct
2 Correct 33 ms 2176 KB Output is correct
3 Correct 32 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 39 ms 2176 KB Output is correct
2 Correct 34 ms 2176 KB Output is correct
3 Correct 30 ms 2296 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 2176 KB Output is correct
2 Correct 49 ms 2168 KB Output is correct
3 Correct 37 ms 2048 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 37 ms 2176 KB Output is correct
2 Correct 36 ms 2296 KB Output is correct
3 Correct 35 ms 2196 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 42 ms 2048 KB Output is correct
2 Correct 31 ms 2176 KB Output is correct
3 Correct 30 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 33 ms 2048 KB Output is correct
2 Correct 31 ms 2304 KB Output is correct
3 Correct 36 ms 2176 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1551 ms 9240 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1552 ms 9212 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1524 ms 9180 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1537 ms 9436 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1548 ms 9308 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1549 ms 9868 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1554 ms 9620 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1556 ms 9328 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1535 ms 9680 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1542 ms 9580 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1518 ms 9728 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1548 ms 9748 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1545 ms 9772 KB Time limit exceeded
2 Halted 0 ms 0 KB -