답안 #101708

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
101708 2019-03-19T14:22:07 Z Benq Akvizna (COCI19_akvizna) C++14
130 / 130
1342 ms 9972 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,40) {
        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); }
                             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1920 KB Output is correct
2 Correct 8 ms 1920 KB Output is correct
3 Correct 7 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 7 ms 1920 KB Output is correct
2 Correct 7 ms 1920 KB Output is correct
3 Correct 8 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 1920 KB Output is correct
2 Correct 7 ms 1920 KB Output is correct
3 Correct 7 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 1920 KB Output is correct
2 Correct 8 ms 1920 KB Output is correct
3 Correct 6 ms 1920 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 30 ms 2176 KB Output is correct
2 Correct 25 ms 2176 KB Output is correct
3 Correct 22 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2176 KB Output is correct
2 Correct 32 ms 2176 KB Output is correct
3 Correct 22 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 2048 KB Output is correct
2 Correct 23 ms 2176 KB Output is correct
3 Correct 22 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2048 KB Output is correct
2 Correct 24 ms 2176 KB Output is correct
3 Correct 26 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2176 KB Output is correct
2 Correct 26 ms 2204 KB Output is correct
3 Correct 22 ms 2048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 2168 KB Output is correct
2 Correct 25 ms 2176 KB Output is correct
3 Correct 24 ms 2304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 26 ms 2168 KB Output is correct
2 Correct 31 ms 2176 KB Output is correct
3 Correct 22 ms 2092 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 2140 KB Output is correct
2 Correct 24 ms 2176 KB Output is correct
3 Correct 27 ms 2176 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 2048 KB Output is correct
2 Correct 28 ms 2176 KB Output is correct
3 Correct 25 ms 2152 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1112 ms 9312 KB Output is correct
2 Correct 1153 ms 9628 KB Output is correct
3 Correct 1074 ms 9168 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1115 ms 9332 KB Output is correct
2 Correct 1069 ms 9596 KB Output is correct
3 Correct 1129 ms 9656 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1110 ms 9312 KB Output is correct
2 Correct 1181 ms 9808 KB Output is correct
3 Correct 1220 ms 9768 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1110 ms 9516 KB Output is correct
2 Correct 1147 ms 9608 KB Output is correct
3 Correct 1115 ms 9444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1091 ms 9524 KB Output is correct
2 Correct 1181 ms 9916 KB Output is correct
3 Correct 1075 ms 9312 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1186 ms 9876 KB Output is correct
2 Correct 1152 ms 9508 KB Output is correct
3 Correct 1072 ms 9484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1184 ms 9544 KB Output is correct
2 Correct 1112 ms 9208 KB Output is correct
3 Correct 1183 ms 9224 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1151 ms 9256 KB Output is correct
2 Correct 1212 ms 9800 KB Output is correct
3 Correct 1098 ms 9304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1162 ms 9756 KB Output is correct
2 Correct 1097 ms 9744 KB Output is correct
3 Correct 1307 ms 9972 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1252 ms 9740 KB Output is correct
2 Correct 1065 ms 9440 KB Output is correct
3 Correct 1230 ms 9848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1228 ms 9856 KB Output is correct
2 Correct 1161 ms 9840 KB Output is correct
3 Correct 1231 ms 9844 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1179 ms 9972 KB Output is correct
2 Correct 1272 ms 9876 KB Output is correct
3 Correct 1342 ms 9848 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1266 ms 9800 KB Output is correct
2 Correct 1248 ms 9900 KB Output is correct
3 Correct 1197 ms 9720 KB Output is correct