SDNU 1501.Problem_J 贪心

解决一个关于如何将一块整肉切割成N份指定大小小块肉,并使总消耗能量最小的问题。采用贪心策略,通过排序和逐步合并最小单位的方法找到最优解。

1501.Problem_J
Time Limit: 2000 MS    Memory Limit: 32768 KB

Description

So long coding time make ChaoChao be more nonsense. In order to spend the boring time, ChaoChao plan to practice his cooking stills.

Now, CC wants to make N foods. The i-th food need a[i] unit meat, but CC find that there is only a whole meat and he need to consume energy to cut meat. Consumption value is equal to the size of the cut meat. For example, there is a 21 units meat, so CC will consume 21 energy to cut it. CC wants to cut meat into N small meat. He wants to know the minimum number of the energy he need.(The size of the meat bought is equal to the sum of N pieces of meat.)

INPUT

The first line is a number N (1 ≤ N ≤ 20000) identified the number of small meat.

Then next N lines, each line contains one number a[i](1 ≤ a[i] ≤ 50000) means a[i] units of the i-th meat.


Input

The first line is a number N (1 ≤ N ≤ 20000) identified the number of small meat.

Then next N lines, each line contains one number a[i](1 ≤ a[i] ≤ 50000) means a[i] units of the i-th meat.


Output

The minimum number of the energy.


Sample Input

3
8
5
8

Sample Output

34

    一开始看完这个题就当做是石子合并类的题了,先排个序然后直接往上加,结果就是收获了一堆wa→_→后来才发现这种做法不行,这题也是比完赛之后才明白的怎么做。直接贪心处理,把这些数进行排序,然后从最小的两个合并,再进行排序,再合并最小的两个,以此类推直到所有的合并在了一起。就相当于把切肉的这个过程给倒过来运行了。

    要注意的有两点,一开始我是每一遍循环都sort了一下,然后就TLE了,所以优化了一下,只sort一次,然后里面排序直接排合并的那个数就可以了。另外就是要用long long来处理这道题。

    下面是AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
long long a[20005];

int cmp(long long a,long long b)
{
    return a>b;
}

int main()
{
    int n;
    int i,j;
    long long ans;
    long long t;
    while(scanf("%d",&n)!=EOF)
    {
        ans=0;
        for(i=0;i<n;i++)
        {
            scanf("%lld",&a[i]);
        }
        sort(a,a+n,cmp);
        for(i=0;i<n-1;i++)
        {
            for(j=n-i-1;j>0;j--)
            {
                if(a[j]>a[j-1])
                {
                    t=a[j];
                    a[j]=a[j-1];
                    a[j-1]=t;
                }
                else
                    break;
            }
            a[n-i-2]=a[n-i-1]+a[n-i-2];
            ans+=a[n-i-2];
        }
        cout<<ans<<endl;
    }
    return 0;
}


### 关于 SDNU 山东师范大学操作系统课程期末考试复习资料 对于准备参加山东师范大学 (SDNU) 操作系统课程期末考试的学生来说,获取全面而有效的复习资源至关重要。通常情况下,学校会提供官方的教学大纲以及历年的试题作为主要参考资料[^1]。 #### 官方渠道获取复习材料 学生可以通过学校的官方网站或教务处公告栏来获得最新的教学计划和考试指南。这些文件往往包含了详细的考核范围说明、重点章节提示等内容,有助于考生有针对性地进行备考工作[^2]。 #### 教材与讲义的重要性 除了上述提到的正式出版物外,在课堂上使用的教材也是不可或缺的一部分。特别是由授课教师编写的讲义,其中可能涵盖了特定版本的操作系统知识点解析及其应用实例分析,这些都是非常宝贵的学习素材[^3]。 #### 利用在线平台辅助学习 随着互联网技术的发展,许多高校都建立了自己的网络学堂或是加入了MOOCs(大规模开放在线课程),这为广大学生提供了更多样化的自学途径。通过访问这类教育网站,可以找到其他院校相同专业的优质视频教程和其他形式的教学资源共享链接[^4]。 ```python # Python 示例代码用于展示如何在网络爬虫中抓取公开教育资源列表(仅作示意用途) import requests from bs4 import BeautifulSoup url = 'https://example.com/sdnu_os_material' # 假设这是目标网页地址 response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') materials = [] for item in soup.select('.material-item'): title = item.find('h2').get_text() link = item.find('a')['href'] material_info = { "title": title, "link": f"https://example.com{link}" } materials.append(material_info) else: print("Failed to fetch data") print(materials[:5]) # 打印前五个条目以供查看 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值