#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int h[100000];
LLI w[100001],dp[100000];
struct line { LLI m,b; };
vector<line> v1,v2;
int main() {
int i;
int n;
scanf("%d",&n);
for (i = 0; i < n; i++) scanf("%d",&h[i]);
for (i = 0; i < n; i++) scanf("%lld",&w[i+1]),w[i+1] += w[i];
int j,s = sqrt(n)+EPS;
v1.pb((line){-2*h[0],(LLI) h[0]*h[0]-w[1]});
for (i = 1; i < n; i++) {
int l = 0,r = v1.size()-1;
while (l < r) {
int m = (l+r) / 2;
if (v1[m].m*h[i]+v1[m].b < v1[m+1].m*h[i]+v1[m+1].b) r = m;
else l = m+1;
}
dp[i] = (LLI) h[i]*h[i]+w[i]+v1[l].m*h[i]+v1[l].b;
for (j = 0; j < v2.size(); j++) dp[i] = min(dp[i],(LLI) h[i]*h[i]+w[i]+v2[j].m*h[i]+v2[j].b);
v2.pb((line){-2*h[i],(LLI) h[i]*h[i]-w[i+1]+dp[i]});
}
printf("%lld\n",dp[n-1]);
return 0;
}
Compilation message
building.cpp: In function 'int main()':
building.cpp:79:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (j = 0; j < v2.size(); j++) dp[i] = min(dp[i],(LLI) h[i]*h[i]+w[i]+v2[j].m*h[i]+v2[j].b);
~~^~~~~~~~~~~
building.cpp:69:11: warning: unused variable 's' [-Wunused-variable]
int j,s = sqrt(n)+EPS;
^
building.cpp:65:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
building.cpp:66:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < n; i++) scanf("%d",&h[i]);
~~~~~^~~~~~~~~~~~
building.cpp:67:50: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < n; i++) scanf("%lld",&w[i+1]),w[i+1] += w[i];
~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
4 ms |
544 KB |
Output is correct |
5 |
Correct |
4 ms |
728 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
3046 ms |
4624 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
4 ms |
544 KB |
Output is correct |
5 |
Correct |
4 ms |
728 KB |
Output is correct |
6 |
Execution timed out |
3046 ms |
4624 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |