Submission #386120

# Submission time Handle Problem Language Result Execution time Memory
386120 2021-04-05T16:43:27 Z AmineWeslati Aliens (IOI16_aliens) C++14
4 / 100
2 ms 384 KB
//Never stop trying
#include "bits/stdc++.h"
using namespace std;
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)

typedef long long ll;
typedef string str;
typedef long double ld;
typedef pair<int, int> pi;
#define fi first
#define se second
typedef vector<int> vi;
typedef vector<pi> vpi;
#define pb push_back
#define eb emplace_back
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define endl "\n"
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)

const int MOD = 1e9 + 7; //998244353
const ll INF = 2e9;
const int nx[4] = {0, 0, 1, -1}, ny[4] = {1, -1, 0, 0}; //right left down up

template<class T> using V = vector<T>;
template<class T> bool ckmin(T& a, const T& b) { return a > b ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
ll cdiv(ll a, ll b) { return a / b + ((a ^ b) > 0 && a % b); } // divide a by b rounded up
//constexpr int log2(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))

mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
//mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
ll random(ll a, ll b){
    return a + rng() % (b - a + 1);
}
#ifndef LOCAL  
#define cerr if(false) cerr
#endif
#define dbg(x) cerr << #x << " : " << x << endl; 
#define dbgs(x,y) cerr << #x << " : " << x << " / " << #y << " : " << y << endl;
#define dbgv(v) cerr << #v << " : " << "[ "; for(auto it : v) cerr << it << ' '; cerr << ']' << endl;
#define here() cerr << "here" << endl;

void IO() {
#ifdef LOCAL
    freopen("input.txt", "r", stdin); 
    freopen("output.txt", "w", stdout);
#endif
}
/////////////////////////ONLY CLEAN CODES ALLOWED/////////////////////////

#ifndef LOCAL
#include "aliens.h"
#endif

int N,M,K; 
V<V<ll>>dp;
vpi vec; 

bool cmp(pi a, pi b){
	if(a.fi!=b.fi) return a.fi<b.fi;
	return a.se>b.se;
}

void filter(){ //change N and segs
	sort(all(vec),cmp);

	vpi vec2;
	int mxr=-INF;
	for(auto x: vec){
		int l=x.fi,r=x.se;
		if(r>mxr) vec2.pb(x);
		ckmax(mxr,r);
	}

	vec.assign(all(vec2));
	N=sz(vec);	
}

ll sq(ll v){
	return v*v;
}


//convex hull stuff

struct line{
	ll a,b; 
}; 
deque<line>st; 

ld inter(line x, line y){
	return ((ld)(y.b-x.b))/(x.a-y.a);
}

bool comp(ll a, ll b, ll c, ll d){
	return (a*d-b*c)*b*d<0;
}

bool check(line a, line b, line c, line d){
	V<ll> aa={a.a,b.a,c.a,d.a};
	V<ll> bb={a.b,b.b,c.b,d.b};

	return comp(bb[1]-bb[0],aa[0]-aa[1],bb[3]-bb[2],aa[2]-aa[3]);
}

bool check2(line a, line b, ll x){
	return comp(b.b-a.b,a.a-b.b,x,1);
}

void add(ll a, ll b){
	while(sz(st)>=2 && 
		//inter(st[sz(st)-1],line{a,b})<inter(st[sz(st)-2],st[sz(st)-1]))
		check(st[sz(st)-1],line{a,b},st[sz(st)-2],st[sz(st)-1]))
			st.pop_back();
	st.pb({a,b});
}
ll query(ll x){
	while(sz(st)>=2
		&& check2(st[0],st[1],x)) st.pop_front();
	ll a=st[0].a,b=st[0].b; 
	return a*x+b;
}


//////////////

ll take_photos(int n, int m, int k, vi rr, vi cc) {
	tie(N,M,K)={n,m,k};
	FOR(i,0,N){
		vec.eb(min(rr[i],cc[i]),max(rr[i],cc[i]));
	}
	filter();

	dp.resize(N+1);
	FOR(i,0,N+1) dp[i].assign(K+1,INF);
	FOR(j,0,K+1) dp[0][j]=0;

	FOR(j,1,K+1){
		st.clear(); //reinitialize convex hull
		FOR(i,1,N+1){
			int l=vec[i-1].fi,r=vec[i-1].se;
			ll a=-2*(l-1); 

			ll b=dp[i-1][j-1] + sq(l-1); 
			if(i>=2) b-=sq(max(0,vec[i-2].se-vec[i-1].fi+1));

			add(a,b);
			dp[i][j]=sq(vec[i-1].se)+query(vec[i-1].se);
			

		}
	}
	return dp[N][K];
}


#ifdef LOCAL
int main() {
    boost; IO();

    int N,M,K; cin>>N>>M>>K;
    vi x(N),y(N);
    FOR(i,0,N) cin>>x[i]>>y[i];
    //FOR(i,0,N) cin>>y[i];
    cout << take_photos(N,M,K,x,y) << endl;

    return 0;
}
#endif
//Change your approach 

/*
5 7 2
0 4 4 4 4
3 4 6 5 6
*/

/*
2 6 2
1 4
4 1
*/

Compilation message

aliens.cpp: In function 'void filter()':
aliens.cpp:73:7: warning: unused variable 'l' [-Wunused-variable]
   73 |   int l=x.fi,r=x.se;
      |       ^
aliens.cpp: In function 'll take_photos(int, int, int, vi, vi)':
aliens.cpp:144:22: warning: unused variable 'r' [-Wunused-variable]
  144 |    int l=vec[i-1].fi,r=vec[i-1].se;
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 4
2 Correct 1 ms 268 KB Correct answer: answer = 4
3 Correct 2 ms 364 KB Correct answer: answer = 4
4 Correct 1 ms 364 KB Correct answer: answer = 12
5 Correct 1 ms 364 KB Correct answer: answer = 52
6 Correct 1 ms 364 KB Correct answer: answer = 210
7 Correct 1 ms 364 KB Correct answer: answer = 88
8 Correct 1 ms 364 KB Correct answer: answer = 7696
9 Correct 1 ms 364 KB Correct answer: answer = 1
10 Correct 1 ms 364 KB Correct answer: answer = 2374
11 Correct 1 ms 364 KB Correct answer: answer = 9502
12 Correct 1 ms 364 KB Correct answer: answer = 49
13 Correct 1 ms 364 KB Correct answer: answer = 151
14 Correct 1 ms 364 KB Correct answer: answer = 7550
15 Correct 1 ms 364 KB Correct answer: answer = 7220
16 Correct 1 ms 364 KB Correct answer: answer = 7550
17 Correct 1 ms 364 KB Correct answer: answer = 10000
18 Correct 1 ms 364 KB Correct answer: answer = 10000
19 Correct 1 ms 364 KB Correct answer: answer = 624
20 Correct 1 ms 364 KB Correct answer: answer = 10000
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 1
2 Incorrect 1 ms 384 KB Wrong answer: output = 2000000001, expected = 4
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 4
2 Correct 1 ms 268 KB Correct answer: answer = 4
3 Correct 2 ms 364 KB Correct answer: answer = 4
4 Correct 1 ms 364 KB Correct answer: answer = 12
5 Correct 1 ms 364 KB Correct answer: answer = 52
6 Correct 1 ms 364 KB Correct answer: answer = 210
7 Correct 1 ms 364 KB Correct answer: answer = 88
8 Correct 1 ms 364 KB Correct answer: answer = 7696
9 Correct 1 ms 364 KB Correct answer: answer = 1
10 Correct 1 ms 364 KB Correct answer: answer = 2374
11 Correct 1 ms 364 KB Correct answer: answer = 9502
12 Correct 1 ms 364 KB Correct answer: answer = 49
13 Correct 1 ms 364 KB Correct answer: answer = 151
14 Correct 1 ms 364 KB Correct answer: answer = 7550
15 Correct 1 ms 364 KB Correct answer: answer = 7220
16 Correct 1 ms 364 KB Correct answer: answer = 7550
17 Correct 1 ms 364 KB Correct answer: answer = 10000
18 Correct 1 ms 364 KB Correct answer: answer = 10000
19 Correct 1 ms 364 KB Correct answer: answer = 624
20 Correct 1 ms 364 KB Correct answer: answer = 10000
21 Correct 1 ms 364 KB Correct answer: answer = 1
22 Incorrect 1 ms 384 KB Wrong answer: output = 2000000001, expected = 4
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 4
2 Correct 1 ms 268 KB Correct answer: answer = 4
3 Correct 2 ms 364 KB Correct answer: answer = 4
4 Correct 1 ms 364 KB Correct answer: answer = 12
5 Correct 1 ms 364 KB Correct answer: answer = 52
6 Correct 1 ms 364 KB Correct answer: answer = 210
7 Correct 1 ms 364 KB Correct answer: answer = 88
8 Correct 1 ms 364 KB Correct answer: answer = 7696
9 Correct 1 ms 364 KB Correct answer: answer = 1
10 Correct 1 ms 364 KB Correct answer: answer = 2374
11 Correct 1 ms 364 KB Correct answer: answer = 9502
12 Correct 1 ms 364 KB Correct answer: answer = 49
13 Correct 1 ms 364 KB Correct answer: answer = 151
14 Correct 1 ms 364 KB Correct answer: answer = 7550
15 Correct 1 ms 364 KB Correct answer: answer = 7220
16 Correct 1 ms 364 KB Correct answer: answer = 7550
17 Correct 1 ms 364 KB Correct answer: answer = 10000
18 Correct 1 ms 364 KB Correct answer: answer = 10000
19 Correct 1 ms 364 KB Correct answer: answer = 624
20 Correct 1 ms 364 KB Correct answer: answer = 10000
21 Correct 1 ms 364 KB Correct answer: answer = 1
22 Incorrect 1 ms 384 KB Wrong answer: output = 2000000001, expected = 4
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 4
2 Correct 1 ms 268 KB Correct answer: answer = 4
3 Correct 2 ms 364 KB Correct answer: answer = 4
4 Correct 1 ms 364 KB Correct answer: answer = 12
5 Correct 1 ms 364 KB Correct answer: answer = 52
6 Correct 1 ms 364 KB Correct answer: answer = 210
7 Correct 1 ms 364 KB Correct answer: answer = 88
8 Correct 1 ms 364 KB Correct answer: answer = 7696
9 Correct 1 ms 364 KB Correct answer: answer = 1
10 Correct 1 ms 364 KB Correct answer: answer = 2374
11 Correct 1 ms 364 KB Correct answer: answer = 9502
12 Correct 1 ms 364 KB Correct answer: answer = 49
13 Correct 1 ms 364 KB Correct answer: answer = 151
14 Correct 1 ms 364 KB Correct answer: answer = 7550
15 Correct 1 ms 364 KB Correct answer: answer = 7220
16 Correct 1 ms 364 KB Correct answer: answer = 7550
17 Correct 1 ms 364 KB Correct answer: answer = 10000
18 Correct 1 ms 364 KB Correct answer: answer = 10000
19 Correct 1 ms 364 KB Correct answer: answer = 624
20 Correct 1 ms 364 KB Correct answer: answer = 10000
21 Correct 1 ms 364 KB Correct answer: answer = 1
22 Incorrect 1 ms 384 KB Wrong answer: output = 2000000001, expected = 4
23 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Correct answer: answer = 4
2 Correct 1 ms 268 KB Correct answer: answer = 4
3 Correct 2 ms 364 KB Correct answer: answer = 4
4 Correct 1 ms 364 KB Correct answer: answer = 12
5 Correct 1 ms 364 KB Correct answer: answer = 52
6 Correct 1 ms 364 KB Correct answer: answer = 210
7 Correct 1 ms 364 KB Correct answer: answer = 88
8 Correct 1 ms 364 KB Correct answer: answer = 7696
9 Correct 1 ms 364 KB Correct answer: answer = 1
10 Correct 1 ms 364 KB Correct answer: answer = 2374
11 Correct 1 ms 364 KB Correct answer: answer = 9502
12 Correct 1 ms 364 KB Correct answer: answer = 49
13 Correct 1 ms 364 KB Correct answer: answer = 151
14 Correct 1 ms 364 KB Correct answer: answer = 7550
15 Correct 1 ms 364 KB Correct answer: answer = 7220
16 Correct 1 ms 364 KB Correct answer: answer = 7550
17 Correct 1 ms 364 KB Correct answer: answer = 10000
18 Correct 1 ms 364 KB Correct answer: answer = 10000
19 Correct 1 ms 364 KB Correct answer: answer = 624
20 Correct 1 ms 364 KB Correct answer: answer = 10000
21 Correct 1 ms 364 KB Correct answer: answer = 1
22 Incorrect 1 ms 384 KB Wrong answer: output = 2000000001, expected = 4
23 Halted 0 ms 0 KB -