Problem Link:
[https://uva.onlinejudge.org/external/118/11824.pdf]
Catagory: STL Algorithm.
Strategy: First, sort out the data in descending order. Then use (2*(Li)^t) Formula, where Li is the initial price of land and t is the year. Use Sort function with your own comparator and try to avoid pow function to avoid double type values.
Code:
#include<bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
ll f[10000],n,m,o,s;
vector<ll>pr;
ll cmp (ll a,ll b)
{
return a>b;
}
ll power(ll x,ll y)
{
ll a=1;
while(y>0)
{
if(y%2)
a*=x;
x*=x;
y/=2;
}
return a;
}
int main()
{
cin>>n;
while(n--)
{ pr.clear();
while(cin>>m)
{
if(!m)
break;
pr.push_back(m);
}
sort(pr.begin(),pr.end(),cmp);
s=0,o=0;
for(ll i=0; i<pr.size(); i++)
{ //cout<<pr[i]<<" "<<power(pr[i],i+1)<<endl;
s+=2*power(pr[i],i+1);
if(s>5000000)
{cout<<"Too expensive"<<endl;
o=1;
break;
}
}
if(!o)
cout<<s<<endl;
}
}
[https://uva.onlinejudge.org/external/118/11824.pdf]
Catagory: STL Algorithm.
Strategy: First, sort out the data in descending order. Then use (2*(Li)^t) Formula, where Li is the initial price of land and t is the year. Use Sort function with your own comparator and try to avoid pow function to avoid double type values.
Code:
#include<bits/stdc++.h>
#define ll unsigned long long int
using namespace std;
ll f[10000],n,m,o,s;
vector<ll>pr;
ll cmp (ll a,ll b)
{
return a>b;
}
ll power(ll x,ll y)
{
ll a=1;
while(y>0)
{
if(y%2)
a*=x;
x*=x;
y/=2;
}
return a;
}
int main()
{
cin>>n;
while(n--)
{ pr.clear();
while(cin>>m)
{
if(!m)
break;
pr.push_back(m);
}
sort(pr.begin(),pr.end(),cmp);
s=0,o=0;
for(ll i=0; i<pr.size(); i++)
{ //cout<<pr[i]<<" "<<power(pr[i],i+1)<<endl;
s+=2*power(pr[i],i+1);
if(s>5000000)
{cout<<"Too expensive"<<endl;
o=1;
break;
}
}
if(!o)
cout<<s<<endl;
}
}
No comments:
Post a Comment