您的位置:首页 > 其它

pat 1079 another solution

2014-04-28 20:36 169 查看
this solution shows well in time and space complexity,but it exceeds limited time at last case.

even change cin to scanf.maybe there are some leaves that far away from the root.

#include<iostream>
#include<cmath>
using namespace std;
struct _leaf
{
int amount;
int index;
};

int main()
{
//	freopen("C:\\Users\\Frank Wang\\Desktop\\in.txt","r",stdin);
const int N=100001;
int n,ii=0;
_leaf leaf
;
int parent
;
double p,r,pr;
scanf("%d%lf%lf",&n,&p,&r);
pr=1+r/100;
for(int i=0;i<n;i++)
{
int k;
scanf("%d",&k);
if(k==0)
{
scanf("%d",&leaf[ii].amount);
leaf[ii].index=i;
++ii;
}
else
{
for(int j=0;j<k;j++)
{
int x;
scanf("%d",&x);
parent[x]=i;
}
}
}

//compute the level of leaf
//compute sales from all retailers
double ans=0;
for(int i=0;i<ii;i++)
{
int tmp=leaf[i].index;
double pr_tmp=1;
while(tmp!=0) {
pr_tmp*=pr;
tmp=parent[tmp];
}
ans+=pr_tmp*leaf[i].amount;
}
printf("%.1lf",ans*p);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  pat