Submission #480451

#TimeUsernameProblemLanguageResultExecution timeMemory
480451nicolaalexandraClimbers (RMI18_climbers)C++14
0 / 100
55 ms98236 KiB
#include <bits/stdc++.h> #define DIM 5010 #define INF 2000000000 using namespace std; priority_queue <pair<int,pair<int,int> >, vector<pair<int,pair<int,int> > >, greater<pair<int,pair<int,int> > > > h; int dp[DIM][DIM],v[DIM]; int n,i,j; void update (int i, int j, int val){ if (val < dp[i][j]){ dp[i][j] = val; h.push(make_pair(val,make_pair(i,j))); } } int main (){ //ifstream cin ("date.in"); //ofstream cout ("date.out"); cin>>n; for (i=1;i<=n;i++) cin>>v[i]; int k = 1; for (i=2;i<n;i++){ if ( (v[i] > v[i-1] && v[i] > v[i+1]) || (v[i] < v[i-1] && v[i] < v[i+1])) v[++k] = v[i]; } v[++k] = 0; n = k; for (i=1;i<=n;i++) for (j=1;j<=n;j++) dp[i][j] = INF; /// dp[capat][segment] - A/B se afla intr un capat de segm si celalalt e pe un segm dp[1][n-1] = 0; h.push(make_pair(0,make_pair(1,n-1))); while (!h.empty()){ int cost = h.top().first; int capat = h.top().second.first; int segm = h.top().second.second; h.pop(); if (capat == segm || capat == segm+1){ /// au ajuns in acelasi punct cout<<cost; return 0; } if (capat < segm){ /// A e in capat if (v[capat] < v[capat+1]){ /// trebuie sa urc if (v[segm] > v[segm+1]){ /// poate si B sa urce if (v[capat+1] <= v[segm]) if (v[capat+1] < v[segm]) update (capat+1,segm,dp[capat][segm] + v[capat+1] - v[capat]); else update (capat+1,segm-1,dp[capat][segm] + v[capat+1] - v[capat]); else update (segm,capat,dp[capat][segm] + v[segm] - v[capat]); } else { /// B trebuie sa se intoarca inapoi if (v[capat+1] <= v[segm+1] ){ update (capat+1,segm,dp[capat][segm] + v[capat+1] - v[capat]); } else update (segm+1,capat,dp[capat][segm] + v[segm+1] - v[capat] ); } } else { /// A coboara if (v[segm] > v[segm+1]){ /// trb sa coboare si B, dar se duce inapoi if (v[capat] - v[capat+1] <= v[capat] - v[segm+1]) update (capat+1,segm,dp[capat][segm] + v[capat] - v[capat+1]); else update (segm+1,capat,dp[capat][segm] + v[capat] - v[segm+1]); } else { /// coboara B, merge inainte if ( v[capat+1] >= v[segm]) { if (v[capat+1] > v[segm]) update (capat+1,segm,dp[capat][segm] + v[capat] - v[capat+1]); else update (capat+1,segm-1,dp[capat][segm] + v[capat] - v[capat+1]); } else update (segm,capat, dp[capat][segm] + v[capat] - v[capat+1]); } } } else { /// B e in capat if (v[capat] < v[capat-1]){ /// B urca if (v[segm+1] > v[segm]){ /// poate si A sa urce if (v[segm+1] >= v[capat-1]) if (v[segm+1] > v[capat-1]) update (capat-1,segm,dp[capat][segm] + v[capat-1] - v[capat]); else update (capat-1,segm+1,dp[capat][segm] + v[capat-1] - v[capat]); else update (segm+1,capat-1,dp[capat][segm] + v[segm+1] - v[capat]); } else { /// A urca, dar se intoarce inapoi if (v[segm] >= v[capat-1]) update (capat-1,segm,dp[capat][segm] + v[capat-1] - v[capat]); else update (segm,capat-1,dp[capat][segm] + v[segm] - v[capat]); } } else { /// B coboara if (v[segm] < v[segm+1]){ /// A cobora, dar se intoarce inapoi if (v[segm] <= v[capat-1]) update (capat-1,segm,dp[capat][segm] + v[capat] - v[capat-1]); else update (segm,capat-1,dp[capat][segm] + v[capat] - v[segm]); } else { /// A coboara, merge inainte if (v[segm+1] <= v[capat-1]){ if (v[segm+1] < v[capat-1]) update (capat-1,segm,dp[capat][segm] + v[capat] - v[capat-1]); else update (capat-1,segm+1,dp[capat][segm] + v[capat] - v[capat-1]); } else update (segm+1,capat-1,dp[capat][segm] + v[capat] - v[segm+1]); } } } } cout<<"NO"; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...