please provide the basic idea about how to solve this problem : Sticks
#include<iostream>
using namespace std;
main()
{
int m;
cin>>m;
int re[m];
cout<<sizeof(re);
}
This code is running perfectly in codeforces GNU C++ 4.7. But why?? shouldn't the array size be a constant.
You are going on a long trip. You start on the road at mile post 0. Along the way there are n hotels, at mile posts a1 < a2 < a3...< an, where each ai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the nal hotel (at distance an), which is your destination. You'd ideally like to travel 200 miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (200-x)^2. You want to plan your trip so as to minimize the total penaltythat is, the sum, over all travel days, of the daily penalties. Give an efcient algorithm that determines the optimal sequence of hotels at which to stop.
#include<iostream>
#include<vector>
using namespace std;
void main()
{
int n; cin>>n;
vector<int> hotels(n),dp(n,INT_MAX);
for(int i=0;i<n;i++) cin>>hotels[i];
dp[0]=0;
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
{
int x = hotels[j]-hotels[i];
if(x<=200) dp[j] = min(dp[j],dp[i]+(200-x)*(200-x));
}
cout<<"\n\n\n penalty = "<<dp[n-1]<<"\n\n\n ";
}
please tell me if this can be improved!!!
long long calc(long long a) {
if (a < 10) return a;
long long r = a / 10;
r += 8;
long long f = a;
while (f >= 10) f /= 10;
if (f <= a % 10) r++;
return r;
}