浙江大学计算机与软件学院2021年考研复试上机

7-4 Load Balancing

分数 30

全屏浏览题目

切换布局

作者 陈越单位 浙江大学

Load balancing (负载均衡) refers to efficiently distributing incoming network traffic across a group of backend servers. A load balancing algorithm distributes loads in a specific way.

If we can estimate the maximum incoming traffic load, here is an algorithm that works according to the following rule:

  • The incoming traffic load of size S will first be partitioned into two parts, and each part may be again partitioned into two parts, and so on.

  • Only one partition is made at a time.

  • At any time, the size of the smallest load must be strictly greater than half of the size of the largest load.

  • All the sizes are positive integers.

  • This partition process goes on until it is impossible to make any further partition.

For example, if S=7, then we can break it into 3+4 first, then continue as 4=2+2. The process stops at requiring three servers, holding loads 3, 2, and 2.

Your job is to decide the maximum number of backend servers required by this algorithm. Since such kind of partitions may not be unique, find the best solution -- that is, the difference between the largest and the smallest sizes is minimized.

Input Specification:

Each input file contains one test case, which gives a positive integer S (2≤N≤200), the size of the incoming traffic load.

Output Specification:

For each case, print two numbers in a line, namely, M, the maximum number of backend servers required, and D, the minimum of the difference between the largest and the smallest sizes in a partition with M servers. The numbers in a line must be separated by one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

22

Sample Output:

4 1

Hint:

There are more than one way to partition the load. For example:

22
= 8 + 14
= 8 + 7 + 7
= 4 + 4 + 7 + 7

or

22
= 10 + 12
= 10 + 6 + 6
= 4 + 6 + 6 + 6

or

22
= 10 + 12
= 10 + 6 + 6
= 5 + 5 + 6 + 6

All requires 4 servers. The last partition has the smallest difference 6−5=1, hence 1 is printed out.

本题暴力解法的思想是,想象成一根木棍,然后进行截断。枚举每一种截断方法,若满足下一步的条件,就进行深搜递归遍历,在每次递归前都判断最大数量以及最大最小值之差,故要记录下最大值最小值以及数量

优化的思想是对每次遍历之前进行循环判定,只有最长的木棍max截断后短的那根记为l1,要求max/3+1<=l1<=max/2 ,由此可以得出另一根的长度,同时找到第二短的木棍max2,再在递归之前判定截断最长木棍后是否满足i*2>max2,满足条件再进行递归。

启发来自于浙江大学计算机与软件学院2021年考研复试上机 - 雪上空留马行处 - 博客园 (cnblogs.com)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;
const int maxn = 210;
int n,maxnum=-1,mindis=maxn,length[maxn]={0};
//最长木棍,最短,数量
//遍历的目的是让它一直掰下去,然后筛选出满足条件的解 
void dfs(int min_l,int max_l,int count){
    if(count>maxnum){
        maxnum=count;
        mindis = max_l-min_l;
    }
    else if(count==maxnum && mindis>(max_l-min_l)){
        mindis = max_l-min_l;
    }
    //选出第二长的木棍,方便到时候判断截断的两根是否合规
    int max2=max_l;
    length[max_l]--;//取出最长木棍 
    if(length[max_l]==0){
        for(max2=max_l-1;max2>=2;max2--){
            if(length[max2]!=0) break;
        }
    }
    //选择的是较长木棍截断后较短的那根 
    for(int i=max_l/2;i>=max_l/3+1;i--){
        if(i*2<=max2) break;
        int j=max_l-i;
        length[i]++;
        length[j]++;
        dfs(i,max(j,max2),count+1);
        length[i]--;
        length[j]--;
    }
    length[max_l]++;
}
int main()
{
    scanf("%d",&n);
    length[n]=1;
    dfs(n,n,1);
    printf("%d %d\n",maxnum,mindis);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【优质项目推荐】 1、项目代码均经过严格本地测试,运行OK,确保功能稳定后才上传平台。可放心下载并立即投入使用,若遇到任何使用问题,随时欢迎私信反馈与沟通,博主会第一时间回复。 2、项目适用于计算机相关专业(如计科、信息安全、数据科学、人工智能、通信、物联网、自动化、电子信息等)的在校学生、专业教师,或企业员工,小白入门等都适用。 3、该项目不仅具有很高的学习借鉴价值,对于初学者来说,也是入门进阶的绝佳选择;当然也可以直接用于 毕设、课设、期末大作业或项目初期立项演示等。 3、开放创新:如果您有一定基础,且热爱探索钻研,可以在此代码基础上二次开发,进行修改、扩展,创造出属于自己的独特应用。 欢迎下载使用优质资源!欢迎借鉴使用,并欢迎学习交流,共同探索编程的无穷魅力! 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip 基于业务逻辑生成特征变量python实现源码+数据集+超详细注释.zip
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。
江苏科技大学计算机科学与技术专业,考研复试科目包括以下几个方面: 一、英语复试科目 1.阅读理解 Directions: In this section there are four passages followed by questions or unfinished statements, each with four suggested answers marked A, B, C and D. Choose the one that you think is the correct answer. Passage One Questions 1 to 5 are based on the following passage. A recent survey of more than 400 business firms indicates that economic growth in the United States may slow down in the next few years. The survey was conducted by the National Association of Business Economists, which also found that the rate of inflation will increase. The survey found that the average economic growth rate for the next three years will be 2.7 percent, compared to 3.3 percent for the previous three years. Inflation is expected to rise from 3 percent to 4 percent during the same period. The survey also found that employment will increase by only 1.7 percent annually over the next three years, compared to 2.4 percent for the previous three years. 1. What is the main topic of the passage? A. Economic growth in the United States. B. Inflation in the United States. C. A survey of business firms. D. Employment in the United States. 2. According to the survey, the average economic growth rate for the next three years will be ________. A. 2.4 percent B. 2.7 percent C. 3.0 percent D. 3.3 percent 3. What is the expected increase in inflation for the next three years? A. 1 percent B. 2 percent C. 3 percent D. 4 percent 4. According to the survey, employment will increase by ________ annually over the next three years. A. 1.7 percent B. 2.4 percent C. 3.0 percent D. 3.3 percent 5. Which of the following statements is true according to the passage? A. Economic growth will increase over the next three years. B. Inflation is expected to remain the same over the next three years. C. Employment will increase at a faster rate over the next three years. D. The survey was conducted by the National Association of Business Economists. 2.翻译 Directions: Translate the following passage into Chinese. The Internet has made it possible for people all over the world to communicate with each other instantly and inexpensively. E-mail, chat rooms, and instant messaging are just a few of the ways people can communicate online. Because of this, the Internet has become an important tool for businesses, governments, and individuals. People can buy and sell goods and services online, share information, and even participate in virtual meetings. However, the Internet can also be a dangerous place. Hackers can steal personal and financial information, and viruses can damage computer systems. Therefore, it is important to use caution when using the Internet and to protect your computer with antivirus software and firewalls. 3.口试 Directions: In this section, you will be asked to express your opinions on a given topic. You will have one minute to prepare your answer, and then you will have two minutes to speak. You should speak for at least one minute, and you may use the notes you make during the preparation time. Topic: Should college students be required to take physical education classes? Possible answer: In my opinion, college students should be required to take physical education classes. First of all, physical education classes can help students maintain a healthy lifestyle. Many college students spend a lot of time sitting in classrooms or studying in libraries, and they may not have time to engage in physical activities. Physical education classes can provide students with opportunities to exercise and improve their physical fitness. Secondly, physical education classes can also help students develop teamwork and leadership skills. Many physical education classes involve team sports, such as basketball or volleyball, and students need to work together to achieve their goals. This can help them learn how to cooperate with others and develop leadership skills. Finally, physical education classes can also be a good way for students to relieve stress. College students often face a lot of pressure from academic studies and other responsibilities, and physical activities can be a good way to relieve stress and improve mental health. Therefore, I believe that college students should be required to take physical education classes.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值