Submission #941898

# Submission time Handle Problem Language Result Execution time Memory
941898 2024-03-09T16:19:51 Z Minbaev Building Bridges (CEOI17_building) C++17
60 / 100
523 ms 15940 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pii pair<int,int>
using namespace __gnu_pbds;
using namespace std;
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define f first
#define int long long
#define s second
#define pii pair<int,int>
template<class T>bool umax(T &a,T b){if(a<b){a=b;return true;}return false;}
template<class T>bool umin(T &a,T b){if(b<a){a=b;return true;}return false;}
typedef tree<int, null_type, less_equal<int>, rb_tree_tag,
	tree_order_statistics_node_update> ordered_set;
const int N=3e5 + 5	;
const int inf = 3e18 + 7;
const int mod = 998244353;


void solve(){
	
	int n,m,k;
	
	cin>>n;
	
	vector<int>h(n+1),w(n+1);
	
	for(int i = 1;i<=n;i++){
		cin>>h[i];
	}
	
	for(int i = 1;i<=n;i++){
		cin>>w[i];
	}
	
	if(n<=1000){
	vector<int>dp(n + 1,inf);
	
	dp[1] = 0;
	
	for(int i = 2;i<=n;i++){
		int cost = 0;
		for(int j = i - 1;j >= 1;j--){
				dp[i] = min(dp[i],(h[i]-h[j])*(h[i]-h[j])+cost+dp[j]);
				cost += w[j];
		}
	}
	
	cout<<dp[n]<<"\n";
	return;	
	}
	
	map<int,set<int>>st;
	
	vector<int>dp(n + 1);
	int sum = 0;
	
	for(int i = 2;i<=n;i++){
		dp[i] = (h[1]-h[i])*(h[1]-h[i]) + sum;
		sum += w[i];
	}
	sum = 0;
	for(int i = n - 1;i>=2;i--){
		dp[i] += (h[n]-h[i])*(h[n]-h[i]) + sum;
		sum += w[i];
	}
	
	int ans = inf;
	
	for(int i = 2;i<=n;i++){
		umin(ans,dp[i]);
	}
	
	st[w[2]].insert(h[2]);
	
	for(int i = 3;i<=n;i++){
		int blyat = (h[1] + h[i])/2;
		for(int j = -20;j<=20;j++){
			//~ cout<<i<<" "<<j<<"\n";
			if(st[j].size() == 0)continue;
			//~ cout<<i<<" "<<j<<"\n";
			auto it = st[j].upper_bound(blyat);
			
			if(it != st[j].end()){
				//~ cout<<*it<<" "<<j<<" "<<i<<"\n";
				int val = dp[i] - (h[1]-h[i])*(h[1]-h[i]);
				val += (h[1]-*it)*(h[1]-*it) + (*it-h[i])*(*it-h[i]);
				val -= j;
				umin(ans,val);
			}
			
			it = st[j].lower_bound(blyat);
			
			//~ if(i == 5 && j == 9)cout<<*it<<"\n";
			if(it != st[j].end()){
				
				int val = dp[i] - (h[1]-h[i])*(h[1]-h[i]);
				
				val += (h[1]-*it)*(h[1]-*it) + (*it-h[i])*(*it-h[i]);
				//~ if(i == 5 && j == 9)cout<<val<<" "<<"\n";
				val -= j;
				
				umin(ans,val);
			}
			
		}
		st[w[i]].insert(h[i]);
	}
	cout<<ans<<"\n";
}

signed main()
{
//	freopen("seq.in", "r", stdin);
//  freopen("seq.out", "w", stdout);
	ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL);
	int tt=1;//cin>>tt>>n;
	while(tt--)solve();

}

Compilation message

building.cpp: In function 'void solve()':
building.cpp:25:8: warning: unused variable 'm' [-Wunused-variable]
   25 |  int n,m,k;
      |        ^
building.cpp:25:10: warning: unused variable 'k' [-Wunused-variable]
   25 |  int n,m,k;
      |          ^
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 408 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 516 ms 7052 KB Output is correct
2 Correct 505 ms 6480 KB Output is correct
3 Correct 523 ms 6744 KB Output is correct
4 Correct 99 ms 4012 KB Output is correct
5 Correct 430 ms 8292 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 1 ms 408 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 516 ms 7052 KB Output is correct
7 Correct 505 ms 6480 KB Output is correct
8 Correct 523 ms 6744 KB Output is correct
9 Correct 99 ms 4012 KB Output is correct
10 Correct 430 ms 8292 KB Output is correct
11 Incorrect 162 ms 15940 KB Output isn't correct
12 Halted 0 ms 0 KB -