帆布画
Description
After last year’s success, Samuel W. E. R. Craft’s reputation has grown and he has now funded various projects. His latest idea involves creating an array of canvases with colored patterns without repeating colors.
Samuel bought a set of white canvasses of varying sizes. Since painting them manually would take too much time, he devised a huge machine to automate the painting process. The painting process works as follows:
Assemble all canvasses in a line in the machine’s conveyor belt, disposed in some chosen order.
Pick a color C and a number F (which should be less than the number of color C canvasses).
Going from left to right, all canvasses with color C are painted. The first F color C canvasses are painted with a new color X and the remaining color C canvasses are painted with a new color Y . Colors X and Y are selected by the machine, are distinct, and are different from any color used previously. The amount of ink spent in this step is equal to the sum of the sizes of the painted canvasses.
Repeat 2) and 3) until all canvasses have distinct colors.
Consider for example that Samuel bought four canvasses of sizes 3, 5, 5 and 7. The following picture shows 2 different options for painting them.

Given the sizes of the canvasses Samuel bought, can you help Samuel finding the minimum amount of ink the machine needs to spend in order to have all canvasses with different colors?
Input
The first line consists of a single integer T, the number of test cases. Each test case is composed by two lines. The first line consists of a single integer N representing the number of canvasses. The next line contains N space separated integers representing the sizes of the canvasses.
Output
The output contains T lines, one for each test case: the minimum amount of ink the machine needs in order to have all canvasses with different colors.
Note


代码如下:
#include<stdio.h>
#include<stdlib.h>
long a[100010];
long n,temp;
void carryout(int i)
{
long t,flag=0;
while(i*2<=n&&flag==0)
{
if(a[i]<=a[i*2]) t=i;
else if(a[i]>a[i*2]) t=i*2;
if(n>=i*2+1&&a[t]>a[i*2+1]) t=i*2+1;
if(t!=i)
{
temp=a[t];
a[t]=a[i];
a[i]=temp;
i=t;
}
else if(t==i) flag = 1;
}
}
int main()
{
int T;
long least=0,i,count=0;
scanf("%d",&T);
while(T--)
{
least=0;
count=0;
scanf("%ld",&count);
for(i=1;i<=count;i++) scanf("%ld",&a[i]);
if(count==1)
{
printf("0\n");
continue;
}
n=count;
for(i=n/2;i>=1;i--) carryout(i);
while(n>2)
{
if(a[2]>=a[3])
{
least=least+a[1]+a[3];
a[3]=a[1]+a[3];
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
carryout(3);
carryout(1);
}
else if(a[2]<a[3])
{
least=least+a[1]+a[2];
a[2]=a[1]+a[2];
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
carryout(2);
carryout(1);
}
}
least=least+a[1]+a[2];
printf("%ld\n",least);
}
}
该博客介绍了Samuel W. E. R. Craft的最新项目——创建不重复颜色图案的帆布画。为了自动化绘画过程,他设计了一台机器。机器按照指定顺序排列帆布,选择颜色和数量,然后从左到右为相同颜色的帆布涂色,前F个涂上新颜色X,其余涂上新颜色Y,确保颜色不重复。问题求解的是找到使所有帆布颜色不同的最小用墨量。博客提供了输入输出格式和示例,并邀请读者解决这个问题。
540





