TOJ 1601

简介: 1601.   Box of BricksTime Limit: 1.0 Seconds   Memory Limit: 65536KTotal Runs: 5007   Accepted Runs: 1950 Little Bob likes playing with his box of bricks.
1601.   Box of Bricks
Time Limit: 1.0 Seconds    Memory Limit: 65536K
Total Runs: 5007    Accepted Runs: 1950



Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. "Look, I've built a wall!", he tells his older sister Alice. "Nah, you should make all stacks the same height. Then you would have a real wall.", she retorts. After a little consideration, Bob sees that she is right. So he sets out to rearrange the bricks, one by one, such that all stacks are the same height afterwards. But since Bob is lazy he wants to do this with the minimum number of bricks moved. Can you help?

Input

The input consists of several data sets. Each set begins with a line containing the number n of stacks Bob has built. The next line contains n numbers, the heights hi of the n stacks. You may assume 1 ≤ n ≤ 50 and 1 ≤ hi ≤ 100.

The total number of bricks will be divisible by the number of stacks. Thus, it is always possible to rearrange the bricks such that all stacks have the same height.

The input is terminated by a set starting with n = 0. This set should not be processed.


Output

For each set, first print the number of the set, as shown in the sample output. Then print the line "The minimum number of moves is k.", where k is the minimum number of bricks that have to be moved in order to make all the stacks the same height.

Output a blank line after each set.


Sample Input

6
5 2 4 1 7 5
0


Sample Output

Set #1
The minimum number of moves is 5.
//大于平均值的部分加和 ,简单题目,学习vector 
//AC
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int i,j,k,T;
    int flag = 1;//必须开在while外,并且初始化0 
    while(cin>>T,T)
    {
        int sum = 0;
        vector <int > v;
        for(i=1;i<=T;i++)
        {
            int temp;
            cin>>temp;
            v.push_back(temp);
            sum += temp;
        }
        int aver = sum / v.size();
        int ans = 0;
        for(i=0;i<v.size();i++)
            if(v.at(i)>aver)
                ans += v.at(i) - aver;
        //if(flag>1)
            
        cout<<"Set #"<<flag<<endl;
        cout<<"The minimum number of moves is "<<ans<<"."<<endl;
        cout<<endl;
        flag++;
        v.clear();
    }
    //system("pause");
    return 0;
}
 
 
 //个人认为,应该
 #include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
    int i,j,k,T;
    int flag = 1;//必须开在while外,并且初始化0 
    while(cin>>T,T)
    {
        int sum = 0;
        vector <int > v;
        for(i=1;i<=T;i++)
        {
            int temp;
            cin>>temp;
            v.push_back(temp);
            sum += temp;
        }
        int aver = sum / v.size();
        int ans = 0;
        for(i=0;i<v.size();i++)
            if(v.at(i)>aver)
                ans += v.at(i) - aver;
        if(flag>1)
            cout<<endl;  
        cout<<"Set #"<<flag<<endl;
        cout<<"The minimum number of moves is "<<ans<<"."<<endl;
        flag++;
        v.clear();
    }
    //system("pause");
    return 0;
}
            
            
        

 

目录
相关文章
|
5月前
|
监控 物联网 区块链
新技术趋势与应用:新兴技术的发展前景与实践探索
【5月更文挑战第25天】在21世纪的科技浪潮中,新兴技术如区块链、物联网、虚拟现实等正以前所未有的速度发展,引领着全球的创新潮流。这些技术不仅改变了我们的生活方式,也正在重塑着社会的运行模式。本文将深入探讨这些新兴技术的发展趋势和应用场景,以及它们如何影响我们的生活和社会。
|
算法 索引 Python
python中的min和in用代码实现
min 在 Python 中 min 函数可以直接返回列表中的最小项。 现在用代码演示一下,怎么用代码实现在列表中检索一个最小项。 def fn(L): MinIndex = 0 CurrentInder = 1 while C...
1083 0
|
3天前
|
SQL 人工智能 安全
【灵码助力安全1】——利用通义灵码辅助快速代码审计的最佳实践
本文介绍了作者在数据安全比赛中遇到的一个开源框架的代码审计过程。作者使用了多种工具,特别是“通义灵码”,帮助发现了多个高危漏洞,包括路径遍历、文件上传、目录删除、SQL注入和XSS漏洞。文章详细描述了如何利用这些工具进行漏洞定位和验证,并分享了使用“通义灵码”的心得和体验。最后,作者总结了AI在代码审计中的优势和不足,并展望了未来的发展方向。
|
11天前
|
编解码 Java 程序员
写代码还有专业的编程显示器?
写代码已经十个年头了, 一直都是习惯直接用一台Mac电脑写代码 偶尔接一个显示器, 但是可能因为公司配的显示器不怎么样, 还要接转接头 搞得桌面杂乱无章,分辨率也低,感觉屏幕还是Mac自带的看着舒服
|
18天前
|
存储 人工智能 缓存
AI助理直击要害,从繁复中提炼精华——使用CDN加速访问OSS存储的图片
本案例介绍如何利用AI助理快速实现OSS存储的图片接入CDN,以加速图片访问。通过AI助理提炼关键操作步骤,避免在复杂文档中寻找解决方案。主要步骤包括开通CDN、添加加速域名、配置CNAME等。实测显示,接入CDN后图片加载时间显著缩短,验证了加速效果。此方法大幅提高了操作效率,降低了学习成本。
2799 8
|
13天前
|
存储 缓存 关系型数据库
MySQL事务日志-Redo Log工作原理分析
事务的隔离性和原子性分别通过锁和事务日志实现,而持久性则依赖于事务日志中的`Redo Log`。在MySQL中,`Redo Log`确保已提交事务的数据能持久保存,即使系统崩溃也能通过重做日志恢复数据。其工作原理是记录数据在内存中的更改,待事务提交时写入磁盘。此外,`Redo Log`采用简单的物理日志格式和高效的顺序IO,确保快速提交。通过不同的落盘策略,可在性能和安全性之间做出权衡。
1576 12
|
5天前
|
人工智能 关系型数据库 Serverless
1024,致开发者们——希望和你一起用技术人独有的方式,庆祝你的主场
阿里云开发者社区推出“1024·云上见”程序员节专题活动,包括云上实操、开发者测评和征文三个分会场,提供14个实操活动、3个解决方案、3 个产品方案的测评及征文比赛,旨在帮助开发者提升技能、分享经验,共筑技术梦想。
715 95
|
1月前
|
弹性计算 人工智能 架构师
阿里云携手Altair共拓云上工业仿真新机遇
2024年9月12日,「2024 Altair 技术大会杭州站」成功召开,阿里云弹性计算产品运营与生态负责人何川,与Altair中国技术总监赵阳在会上联合发布了最新的“云上CAE一体机”。
阿里云携手Altair共拓云上工业仿真新机遇
|
18天前
|
人工智能 Serverless API
AI助理精准匹配,为您推荐方案——如何快速在网站上增加一个AI助手
通过向AI助理提问的方式,生成一个技术方案:在网站上增加一个AI助手,提供7*24的全天候服务,即时回答用户的问题和解决他们可能遇到的问题,无需等待人工客服上班,显著提升用户体验。
1468 9