Submission #693312

# Submission time Handle Problem Language Result Execution time Memory
693312 2023-02-02T17:03:38 Z sudheerays123 Watching (JOI13_watching) C++17
50 / 100
1000 ms 12268 KB
#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
#define ll int
const ll N = 2000+5 , INF = 1e9+5 , MOD = 1e9+7;

vector<ll> a(N);
ll n,p,q;

bool check(ll dist){

	vector<vector<ll>> dp(n+5,vector<ll>(p+5,INF));
	// dp[i][j] = min large cameras needed for 1 to i and having j small cameras

	for(ll i = 0; i <= p; i++) dp[0][i] = 0;
	dp[1][0] = 1;
	for(ll i = 1; i <= p; i++) dp[1][i] = 0;


	for(ll i = 2; i <= n; i++){
		ll curmini = INF;
		for(ll j = 0; j <= p; j++){

			ll low1 = 1 , high1 = i;
			ll ind1 = i;
			while(low1 <= high1){
				ll mid = (low1+high1)/2;
				if(a[i]-a[mid] < dist){
					ind1 = mid;
					high1 = mid-1;
				}
				else low1 = mid+1;
			}

			if(j) dp[i][j] = min(dp[i][j],dp[ind1-1][j-1]);

			ll low2 = 1 , high2 = i;
			ll ind2 = i; 
			while(low2 <= high2){
				ll mid = (low2+high2)/2;
				if(a[i]-a[mid] < 2*dist){
					ind2 = mid;
					high2 = mid-1;
				}
				else low2 = mid+1;
			}

			dp[i][j] = min(dp[i][j],dp[ind2-1][j]+1);

			curmini = min(curmini,dp[i][j]);
		}	
		if(curmini > q) return false;
	}

	ll ans = INF;
	for(ll i = 0; i <= p; i++) ans = min(ans,dp[n][i]);

	return (ans <= q);
}

void solve(){

	cin >> n >> p >> q;
	for(ll i = 1; i <= n; i++) cin >> a[i];

	if(p >= n || q >= n){
		cout << "1";
		return;
	}

	sort(a.begin()+1,a.begin()+n+1);

	ll low = 1 , high = a[n];
	ll ans;

	while(low <= high){
		ll mid = (low+high)/2;
		if(check(mid)){
			ans = mid;
			high = mid-1;
		}
		else low = mid+1;
	}

	cout << ans;
}

int main(){

	fast;

	ll tc = 1;
	// cin >> tc;
	while(tc--) solve();

	return 0;
}

Compilation message

watching.cpp: In function 'void solve()':
watching.cpp:85:10: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
   85 |  cout << ans;
      |          ^~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 0 ms 212 KB Output is correct
7 Correct 1 ms 212 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 4 ms 340 KB Output is correct
12 Correct 4 ms 340 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Execution timed out 1092 ms 12268 KB Time limit exceeded
4 Halted 0 ms 0 KB -