Submission #46000

# Submission time Handle Problem Language Result Execution time Memory
46000 2018-04-16T23:59:04 Z RezwanArefin01 Aliens (IOI16_aliens) C++17
0 / 100
2 ms 476 KB
#include <bits/stdc++.h>
#include "aliens.h"
using namespace std;

typedef long long ll; 
typedef pair<int, int> ii;
typedef pair<ll, int> li;

#define sq(a) ((ll)(a) * (a))

struct cht {
	vector<ll> m, b, k; int ptr = 0;
	bool bad(int l1, int l2, int l3) {
		return 1.0 * (b[l3] - b[l1]) * (m[l1] - m[l2]) <= 1.0 * (b[l2] - b[l1]) * (m[l1] - m[l3]);
	}
	void add(ll _m, ll _b, int _k) {
		m.push_back(_m); b.push_back(_b); k.push_back(_k);
		int s = m.size(); 
		while(s >= 3 && bad(s - 3, s - 2, s - 1)) {
			m.erase(m.end() - 2); 
			b.erase(b.end() - 2); 
			k.erase(k.end() - 2);
			s--;
		}
	}
	ll f(int i, ll x) { return m[i] * x + b[i]; }
	li query(ll x) {
		if(ptr >= m.size()) ptr = m.size() - 1; 
		while(ptr < m.size() - 1 && 
			f(ptr, x) > f(ptr + 1, x)) ptr++;
		return { f(ptr, x), k[ptr] };
	}
	void clear() { m.clear(), b.clear(), k.clear(); ptr = 0; }
} ds;

vector<int> l = {-1}, r = {-1};

li get(ll C) {
	int n = l.size() - 1; 
	li dp[n + 1]; 
	dp[0] = {0, 0}; 
	ds.clear(); ds.add(-2ll * l[1], sq(l[1]), 0);
	for(int i = 1; i <= n; i++) {
		ll x = r[i] + 1;
		li tmp = ds.query(x);
		dp[i].first = tmp.first + sq(x) + C; 
		dp[i].second = tmp.second + 1; 
		if(i == n) break; 
		ds.add(-2ll * l[i + 1], dp[i].first + sq(l[i + 1])- sq(max(0, r[i] - l[i + 1] + 1)), dp[i].second);  
	}
	return dp[n];
}  

ll take_photos(int n, int m, int K, vector<int> R, vector<int> C) {
	vector<ii> tmp; 
	for(int i = 0; i < n; i++) {
		if(R[i] > C[i]) swap(R[i], C[i]); 
		tmp.emplace_back(C[i], R[i]);
	}
	sort(tmp.begin(), tmp.end());
	tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end());
	for(ii seg : tmp) {
		if(seg.first == r.back()) continue; 
		while(l.size() && seg.second <= l.back() && r.back() <= seg.first) 
			r.pop_back(), l.pop_back();
		l.push_back(seg.second); 
		r.push_back(seg.first);
	}	
	ll lo = 0, hi = sq(m + 1), idx = -1;
	ll l, r, lval, rval; 
	while(lo <= hi) {
		ll mid = lo + hi >> 1; 
		li tmp = get(mid); 
		if(tmp.second == K) return tmp.first - K * mid; 
		if(tmp.second < K) {
			hi = mid - 1; 
			if(l < tmp.second) {
				l = tmp.second;
				lval = tmp.first - l * mid; 
			}
		} else {
			lo = mid + 1; 
			if(tmp.second < r) {
				r = tmp.second; 
				rval = tmp.first - r * mid;
			}
		}
	}

	return lval + ((K - l) * (rval - lval)) / (r - l); 

}

#ifdef LOCAL_TESTING
int main() {
	freopen("in", "r", stdin);
    int n, m, k;
    assert(3 == scanf("%d %d %d", &n, &m, &k));
    std::vector<int> r(n), c(n);
    for (int i = 0; i < n; i++) {
        assert(2 == scanf("%d %d", &r[i], &c[i]));
    }
    ll ans = take_photos(n, m, k, r, c);
    
    printf("%lld\n", ans);
    return 0;
}
#endif

Compilation message

aliens.cpp: In member function 'li cht::query(ll)':
aliens.cpp:28:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if(ptr >= m.size()) ptr = m.size() - 1; 
      ~~~~^~~~~~~~~~~
aliens.cpp:29:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while(ptr < m.size() - 1 && 
         ~~~~^~~~~~~~~~~~~~
aliens.cpp: In function 'll take_photos(int, int, int, std::vector<int>, std::vector<int>)':
aliens.cpp:72:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   ll mid = lo + hi >> 1; 
            ~~~^~~~
aliens.cpp:69:29: warning: unused variable 'idx' [-Wunused-variable]
  ll lo = 0, hi = sq(m + 1), idx = -1;
                             ^~~
aliens.cpp:90:33: warning: 'rval' may be used uninitialized in this function [-Wmaybe-uninitialized]
  return lval + ((K - l) * (rval - lval)) / (r - l); 
                           ~~~~~~^~~~~~~
aliens.cpp:90:33: warning: 'lval' may be used uninitialized in this function [-Wmaybe-uninitialized]
aliens.cpp:83:4: warning: 'r' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if(tmp.second < r) {
    ^~
aliens.cpp:77:4: warning: 'l' may be used uninitialized in this function [-Wmaybe-uninitialized]
    if(l < tmp.second) {
    ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Correct answer: answer = 4
2 Incorrect 2 ms 396 KB Wrong answer: output = 0, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 420 KB Correct answer: answer = 1
2 Correct 2 ms 476 KB Correct answer: answer = 4
3 Incorrect 2 ms 476 KB Wrong answer: output = 0, expected = 1
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Correct answer: answer = 4
2 Incorrect 2 ms 396 KB Wrong answer: output = 0, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Correct answer: answer = 4
2 Incorrect 2 ms 396 KB Wrong answer: output = 0, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Correct answer: answer = 4
2 Incorrect 2 ms 396 KB Wrong answer: output = 0, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 248 KB Correct answer: answer = 4
2 Incorrect 2 ms 396 KB Wrong answer: output = 0, expected = 4
3 Halted 0 ms 0 KB -