(程序设计方法与实践)帆布画

该博客介绍了Samuel W. E. R. Craft的最新项目——创建不重复颜色图案的帆布画。为了自动化绘画过程,他设计了一台机器。机器按照指定顺序排列帆布,选择颜色和数量,然后从左到右为相同颜色的帆布涂色,前F个涂上新颜色X,其余涂上新颜色Y,确保颜色不重复。问题求解的是找到使所有帆布颜色不同的最小用墨量。博客提供了输入输出格式和示例,并邀请读者解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

帆布画

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);  
    }  
}  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值