Submission #241860

#TimeUsernameProblemLanguageResultExecution timeMemory
241860rqiAliens (IOI16_aliens)C++14
100 / 100
276 ms9064 KiB
#include "aliens.h" #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef double db; typedef string str; typedef pair<int,int> pi; typedef pair<ll,ll> pl; typedef pair<db,db> pd; typedef vector<int> vi; typedef vector<ll> vl; typedef vector<db> vd; typedef vector<str> vs; typedef vector<pi> vpi; typedef vector<pl> vpl; typedef vector<pd> vpd; #define mp make_pair #define f first #define s second #define sz(x) (int)x.size() #define all(x) begin(x), end(x) #define rall(x) (x).rbegin(), (x).rend() #define rsz resize #define ins insert #define ft front() #define bk back() #define pf push_front #define pb push_back #define eb emplace_back #define lb lower_bound #define ub upper_bound #define FOR(i,a,b) for (int i = (a); i < (b); ++i) #define F0R(i,a) FOR(i,0,a) #define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i) #define R0F(i,a) ROF(i,0,a) #define trav(a,x) for (auto& a: x) const int MOD = 1e9+7; // 998244353; const int MX = 2e5+5; const ll INF = 1e18; const ld PI = acos((ld)-1); const int xd[4] = {1,0,-1,0}, yd[4] = {0,1,0,-1}; mt19937 rng((uint32_t)chrono::steady_clock::now().time_since_epoch().count()); template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } int pct(int x) { return __builtin_popcount(x); } int bits(int x) { return 31-__builtin_clz(x); } // floor(log2(x)) int cdiv(int a, int b) { return a/b+!(a<0||a%b == 0); } // division of a by b rounded up, assumes b > 0 int fstTrue(function<bool(int)> f, int lo, int hi) { hi ++; assert(lo <= hi); // assuming f is increasing while (lo < hi) { // find first index such that f is true int mid = (lo+hi)/2; f(mid) ? hi = mid : lo = mid+1; } return lo; } // INPUT template<class A> void re(complex<A>& c); template<class A, class B> void re(pair<A,B>& p); template<class A> void re(vector<A>& v); template<class A, size_t SZ> void re(array<A,SZ>& a); template<class T> void re(T& x) { cin >> x; } void re(db& d) { str t; re(t); d = stod(t); } void re(ld& d) { str t; re(t); d = stold(t); } template<class H, class... T> void re(H& h, T&... t) { re(h); re(t...); } template<class A> void re(complex<A>& c) { A a,b; re(a,b); c = {a,b}; } template<class A, class B> void re(pair<A,B>& p) { re(p.f,p.s); } template<class A> void re(vector<A>& x) { trav(a,x) re(a); } template<class A, size_t SZ> void re(array<A,SZ>& x) { trav(a,x) re(a); } // TO_STRING #define ts to_string str ts(char c) { return str(1,c); } str ts(bool b) { return b ? "true" : "false"; } str ts(const char* s) { return (str)s; } str ts(str s) { return s; } template<class A> str ts(complex<A> c) { stringstream ss; ss << c; return ss.str(); } str ts(vector<bool> v) { str res = "{"; F0R(i,sz(v)) res += char('0'+v[i]); res += "}"; return res; } template<size_t SZ> str ts(bitset<SZ> b) { str res = ""; F0R(i,SZ) res += char('0'+b[i]); return res; } template<class A, class B> str ts(pair<A,B> p); template<class T> str ts(T v) { // containers with begin(), end() bool fst = 1; str res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += ts(x); } res += "}"; return res; } template<class A, class B> str ts(pair<A,B> p) { return "("+ts(p.f)+", "+ts(p.s)+")"; } // OUTPUT template<class A> void pr(A x) { cout << ts(x); } template<class H, class... T> void pr(const H& h, const T&... t) { pr(h); pr(t...); } void ps() { pr("\n"); } // print w/ spaces template<class H, class... T> void ps(const H& h, const T&... t) { pr(h); if (sizeof...(t)) pr(" "); ps(t...); } // DEBUG void DBG() { cerr << "]" << endl; } template<class H, class... T> void DBG(H h, T... t) { cerr << ts(h); if (sizeof...(t)) cerr << ", "; DBG(t...); } #ifdef LOCAL // compile with -DLOCAL #define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__) #else #define dbg(...) 0 #endif // FILE I/O void setIn(string s) { freopen(s.c_str(),"r",stdin); } void setOut(string s) { freopen(s.c_str(),"w",stdout); } void unsyncIO() { ios_base::sync_with_stdio(0); cin.tie(0); } void setIO(string s = "") { unsyncIO(); // cin.exceptions(cin.failbit); // throws exception when do smth illegal // ex. try to read letter into int if (sz(s)) { setIn(s+".in"), setOut(s+".out"); } // for USACO } /** * Description: LineContainer; add lines of the form $kx+m$, * compute greatest $y$-coordinate for any $x$. * Time: O(\log N) * Source: KACTL * https://github.com/kth-competitive-programming/kactl/commit/165807e28402c9be906f6e6a09452431787bb70d?diff=unified * Verification: * CSA Squared Ends not working :( * https://codeforces.com/contest/1083/problem/E * https://atcoder.jp/contests/arc066/tasks/arc066_d */ bool Q; struct Line { mutable ll k, m, p, lnum; // slope, y-intercept, last optimal x, LAMBDAS used pair<ll, int> eval (ll x) { return mp(k*x+m, lnum); } bool operator<(const Line& o) const { return Q?p<o.p:k<o.k; } }; // for doubles, use inf = 1/.0, divi(a,b) = a/b const ll inf = LLONG_MAX; // floored div ll divi(ll a, ll b) { return a/b-((a^b) < 0 && a%b); } // last x such that first line is better ll bet(const Line& x, const Line& y) { if (x.k == y.k){ if(x.m > y.m) return inf; if(x.m < y.m) return -inf; if(x.lnum <= y.lnum) return inf; return -inf; } if((y.m-x.m) % (x.k-y.k) == 0){ if(x.lnum <= y.lnum){ return (y.m-x.m)/(x.k-y.k)-1; } return (y.m-x.m)/(x.k-y.k); } return divi(y.m-x.m,x.k-y.k); } /** * Description: LineContainer assuming both slopes and queries monotonic. * Time: O(1) * Source: Own * Verification: http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=36005&pid=1009 */ struct LCdeque : deque<Line> { void addBack(Line L) { // assume nonempty while (1) { auto a = bk; pop_back(); a.p = bet(a,L); if (size() && bk.p >= a.p) continue; pb(a); break; } L.p = inf; pb(L); } void addFront(Line L) { while (1) { if (!size()) { L.p = inf; break; } if ((L.p = bet(L,ft)) >= ft.p) pop_front(); else break; } push_front(L); } void add(ll k, ll m, ll lnum) { // line goes to one end of deque k = -k; m = -m; lnum = -lnum; if (!size() || k <= ft.k) addFront({k,m,0,lnum}); else assert(k >= bk.k), addBack({k,m,0,lnum}); } int ord = 1; // 1 = increasing, -1 = decreasing pair<ll, int> query(ll x) { assert(ord); pair<ll, int> rval; if (ord == 1) { while (ft.p < x) pop_front(); rval = ft.eval(x); } else { while(size()>1&&prev(prev(end()))->p>=x)pop_back(); rval = bk.eval(x); } rval.f = -rval.f; rval.s = -rval.s; return rval; } }; vpl points; pair<ll, int> dp[100005]; LCdeque lins; pair<ll, int> testLambda(ll lamb){ //clear memory for(int i = 0; i < sz(points); i++){ dp[i] = mp(INF, -1); } lins.clear(); lins.add(2*(-points[0].f+1), (-points[0].f+1)*(-points[0].f+1)+lamb, 1); for(int i = 0; i < sz(points); i++){ //query to get dp[i] dp[i] = lins.query(points[i].s); dp[i].f+=ll(points[i].s)*points[i].s; //add to deque if it's not sz(points)-1 if(i == sz(points)-1) break; if(points[i].s < points[i+1].f){ lins.add(2*(-points[i+1].f+1), dp[i].f+(-points[i+1].f+1)*(-points[i+1].f+1)+lamb, dp[i].s+1); } else{ lins.add(2*(-points[i+1].f+1), dp[i].f+(-points[i+1].f+1)*(-points[i+1].f+1)-(points[i].s-points[i+1].f+1)*(points[i].s-points[i+1].f+1)+lamb, dp[i].s+1); } } //ps(lamb, dp[sz(points)-1]); return dp[sz(points)-1]; } ll take_photos(int n, int m, int k, vi r, vi c) { for(int i = 0; i < n; i++){ if(r[i] > c[i]) swap(r[i], c[i]); } vector<pair<pi, int>> sortorig; for(int i = 0; i < n; i++){ sortorig.pb(mp(mp(r[i], c[i]), i)); } sort(all(sortorig)); //ps(sortorig); int curr = -1; for(int i = 0; i < sz(sortorig); i++){ pair<pi, int> u = sortorig[i]; if(i+1 < sz(sortorig) && sortorig[i].f.f == sortorig[i+1].f.f) continue; if(u.f.s <= curr) continue; curr = u.f.s; points.pb(mp(u.f.f, u.f.s)); } //ps(points); ll lo = 0; ll hi = 1000000000005LL; while(lo < hi){ ll mid = (lo+hi)/2; pair<ll, int> tans = testLambda(mid); if(tans.s <= k){ hi = mid; } else{ lo = mid+1; } } pair<ll, int> fans = testLambda(lo); ll ans = fans.f; ans-=lo*ll(k); return ans; }

Compilation message (stderr)

aliens.cpp: In function 'void setIn(std::__cxx11::string)':
aliens.cpp:129:31: 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); }
                        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
aliens.cpp: In function 'void setOut(std::__cxx11::string)':
aliens.cpp:130:32: 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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...