Description
Roy has just moved into a new apartment. Well, actually the apartment itself is not very new, even dating back to the days before people had electricity in their houses. Because of this, Roy's apartment has only one single wall outlet, so Roy can only power one of his electrical appliances at a time.
Roy likes to watch TV as he works on his computer, and to listen to his HiFi system (on high volume) while he vacuums, so using just the single outlet is not an option. Actually, he wants to have all his appliances connected to a powered outlet, all the time. The answer, of course, is power strips, and Roy has some old ones that he used in his old apartment. However, that apartment had many more wall outlets, so he is not sure whether his power strips will provide him with enough outlets now.
Your task is to help Roy compute how many appliances he can provide with electricity, given a set of power strips. Note that without any power strips, Roy can power one single appliance through the wall outlet. Also, remember that a power strip has to be powered itself to be of any use.
Roy likes to watch TV as he works on his computer, and to listen to his HiFi system (on high volume) while he vacuums, so using just the single outlet is not an option. Actually, he wants to have all his appliances connected to a powered outlet, all the time. The answer, of course, is power strips, and Roy has some old ones that he used in his old apartment. However, that apartment had many more wall outlets, so he is not sure whether his power strips will provide him with enough outlets now.
Your task is to help Roy compute how many appliances he can provide with electricity, given a set of power strips. Note that without any power strips, Roy can power one single appliance through the wall outlet. Also, remember that a power strip has to be powered itself to be of any use.
Input
Input will start with a single integer 1 <= N <= 20, indicating the number of test cases to follow. Then follow N lines, each describing a test case. Each test case starts with an integer 1 <= K <= 10, indicating the number of power strips in the test case. Then follow, on the same line, K integers separated by single spaces, O1 O2 . . . OK, where 2 <= Oi <= 10, indicating the number of outlets in each power strip.
Output
Output one line per test case, with the maximum number of appliances that can be powered.
Sample Input
3 3 2 3 4 10 4 4 4 4 4 4 4 4 4 4 4 10 10 10 10
Sample Output
7 31 37
大致翻译:
一个公寓里墙上只有一个插座,现在有许多其他的移动插座,问最多能提供多少个插座孔
思路:
感觉这个题的结构是一个树型结构,统计出最后的孩子的个数就能做出来 但是我刚开始看树型结构 还没看完 写不出来....
先排序 最大的排到前面 假设有k个插座 把最大的插到墙上 还有k-1需要插 如果k-1个全部能插到大的插座上 就用剩下的k-1的插座口加上最大的上面的未被占用的插座口即可
如果不能,则第一个上面已经被插满 再算第二个能不能全部插上,以此类推
参考代码:
#include <stdio.h>
void sort(int *a,int n) //排序 从大到小
{
int i,j;
int temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int sum=0,k,i;
int a[12]={0};
scanf("%d",&k);
for(i=0;i<k;i++)
scanf("%d",&a[i]);
sort(a,k);
int m=k-1;
if((m-a[i])<=0)
a[i]+=(m-a[i]);
else
{
for(i=0;(m-=a[i])>0;i++)
a[i]=0;
a[i]=-m;
}
for(i=0;i<k;i++)
sum+=a[i];
printf("%d\n",sum);
}
return 0;
}