Week9 作业

A - 咕咕东的目录管理器

咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。
目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
在这里插入图片描述
现在咕咕东可以在命令行下执行以下表格中描述的命令:
在这里插入图片描述
在这里插入图片描述

输入

输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);
每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);
每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);
面对数据范围你要思考的是他们代表的 “命令” 执行的最大可接受复杂度,只有这样你才能知道你需要设计的是怎样复杂度的系统。

输出

每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。

样例输入

1
22
MKDIR dira
CD dirb
CD dira
MKDIR a
MKDIR b
MKDIR c
CD ..
MKDIR dirb
CD dirb
MKDIR x
CD ..
MKDIR dirc
CD dirc
MKDIR y
CD ..
SZ
LS
TREE
RM dira
TREE
UNDO
TREE

样例输出

OK
ERR
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
OK
9
dira
dirb
dirc
root
dira
a
b
c
dirb
x
dirc
y
OK
root
dirb
x
dirc
y
OK
root
dira
a
b
c
dirb
x
dirc
y

思路

综述

这道题给的最深的印象就是复杂。需要处理的事情有点多。可以利用面向对象编程的思想:对于特殊对象的复杂操作,可在一定程度上采用面向对象的封装思想,以简化思路分析。
以前学习C的时候,觉得面向过程编程容易,而且代码量也少。但是面向大模拟,特别是最近几周做了一些大模拟题,越发的发现面向对象编程的思想其实是有利于解决这样的题目的。而且入股面向对象编程也有利于后期的调试。

懒更新

懒更新就是当用到某部分的时候才会加载,看到这个词,最早想到的是WEB里面,也有个懒更新的应用,就是网页一般很长,当滚动条滚动到下方的时候才会加载下面的页面,这样提高了加载速度,缓解了网络压力。

变量

结构体:目录
包含名字,孩子数组,父节点指针,大小
update是懒更新的时候使用,用于判断是否需要更新
在这里插入图片描述
操作命令数组,主要用于UNDO操作
在这里插入图片描述

操作

MKDIR

需要完成以下操作

  • 判断是否存在该目录
  if (children.find(name) != children.end())
  {
      return NULL;
  }
  • 添加到父目录孩子节点数组内
children[name] = ch;
  • 更新从该目录到根目录一条链的size变量
maintain(1);
RM

需要完成以下操作

  • 判断是否存在该目录
        map<string, Directory *>::iterator it = children.find(name);
        if (it == children.end())
        {
            return nullptr;
        }
  • 删除父目录孩子节点数组内
children.erase(it);
  • 更新从该目录到根目录一条链的size变量
maintain(-1 * it->second->size);
CD
  • ‘..’的时候
     if (".." == name)
      {
          return this->parent;
      }
  • 其他
return getChild(name);
//getchild
//取子目录并且返回,不存在返回空指针;
map<string, Directory *>::iterator it = children.find(name);
if (it == children.end())
    return NULL;
return it->second;
SZ

由于目录结构体维护了size变量,故:

printf("%d\n",size);
LS

当分两种情况:
该目录中size<=10的时候
全部输出

            for(map<string, Directory *>::iterator it = children.begin();it!=children.end();it++){
                printf("%s\n",it->first.c_str());
            }
  • 否则,输出前五和后五,前五正常操作,后五用迭代器指向最后,然后再向前五个,这样复杂度是常数级的。
            map<string, Directory *>::iterator it = children.begin();
            for(int i=0;i<5;i++,it++)printf("%s\n",it->first.c_str());
            printf("...\n");
            it = children.end();
            for(int i=0;i<5;i++)it--;
            for(int i=0;i<5;i++,it++)printf("%s\n",it->first.c_str());
TREE

输出以该节点为子树的所有节点的前5个和后5个
在目录结构体内维护了tenChild数组,也就是维护着该节点前五和后五的孩子节点,这样直接调用输出即可,这里使用到了懒更新,即如果该节点没有被更改过,也即直接输出即可。

  • 少于10个的情况
	        	tenChild.clear();
				treeALL(&tenChild);
	            this->updated = false;	
			}
            for(int i=0;i<size;i++)printf("%s\n",tenChild.at(i).c_str());  
  • 多于10个
    先判断是否被更新过,也即判断是否需要更新tenChild孩子数组
       if(this->updated){
           tenChild.clear();
           treeFirst(5,&tenChild);
           treeBack(5,&tenChild);
           this->updated = false;
       }
       for(int i=0;i<5;i++)printf("%s\n",tenChild.at(i).c_str());
       printf("...\n");
       for(int i=9;i>=5;i--)printf("%s\n",tenChild.at(i).c_str());
UNDO

这里只能对MKDIR、RM、CD三种操作进行UNDO
将每次成功操作的三种命令记录到cmdlist中;
进行一次UNDO就从数组最后pop出一个cmd = cmdlist.back();
分为下面三种情况:
0:前一个操作是MKDIR,所以直接删除刚刚新建的目录即可
1:前一个操作是RM,所以再新建上,因为删除操作并没有将删除的空间全部delete释放掉,所以直接加入到孩子数组即可
2:前一个操作是CD操作,所以这里直接返回即可;

  case 0:
      flag = now->rm(cmd->tmpDir->name) != NULL;
      break;
  case 1:
      flag = now->addChild(cmd->tmpDir) != NULL;
      break;
  case 2:
      now = cmd->tmpDir;flag=true;
      break; /*ok=true;*/
  }

代码

#include <iostream>
#include <cstdio>
#include <map>
#include <vector>
#define _OK printf("OK\n")
#define _ERR printf("ERR\n")
#define nullptr NULL
using namespace std;
int t;
char tmps[20];
const string cmd_names[7] = {"MKDIR", "RM", "CD", "SZ", "LS", "TREE", "UNDO"};

struct Directory
{
    string name;
    map<string, Directory *> children;
    Directory *parent;
    int size;

    bool updated;//懒更新
    vector<string> tenChild;

    Directory(string name, Directory *parent) : name(name), parent(parent)
    {
        size = 1;
        updated = 1;
    }
    
	Directory *getChild(string name)
    {
        //取子目录并且返回,不存在返回空指针;
        map<string, Directory *>::iterator it = children.find(name);
        if (it == children.end())
            return NULL;
        return it->second;
    }
   
    Directory *mkdir(string name)
    {
    	
        if (children.find(name) != children.end())
        {
            return NULL;
        }
        Directory *ch = new Directory(name, this);
        children[name] = ch;
        maintain(1);
        return ch;
    }
   
    Directory *rm(string name)
    {
        map<string, Directory *>::iterator it = children.find(name);
        if (it == children.end())
        {
            return nullptr;
        }
        maintain(-1 * it->second->size);
        children.erase(it);
        return it->second;
    }
   
    Directory *cd(string name)
    {
        if (".." == name)
        {
            return this->parent;
        }
        return getChild(name);
    }

    bool addChild(Directory *ch)
    {
        //加入子目录并返回成功与否
        if (children.find(ch->name) != children.end())
            return false;
        children[ch->name] = ch;
        maintain(ch->size);
        return true;
    }
   
    void maintain(int delta)
    { //向上维护子树
        updated = true;
        size+=delta;
        if(parent!=NULL){
            parent->maintain(delta);
        }
    }
   
    void sz(){
        printf("%d\n",size);
    }
   
    void ls(){
        int sz = children.size();
        if(sz==0)printf("EMPTY\n");
        else if(sz<=10){
            for(map<string, Directory *>::iterator it = children.begin();it!=children.end();it++){
                printf("%s\n",it->first.c_str());
            }
        }else{
            map<string, Directory *>::iterator it = children.begin();
            for(int i=0;i<5;i++,it++)printf("%s\n",it->first.c_str());
            printf("...\n");
            it = children.end();
            for(int i=0;i<5;i++)it--;
            for(int i=0;i<5;i++,it++)printf("%s\n",it->first.c_str());
        }
    }
   
    void tree(){
        if(size==1)printf("EMPTY\n");
        else if(size <=10){
        	if(this->updated){
//        		cout<<"here11"<<endl;
	        	tenChild.clear();
//	            cout<<"here22"<<endl;
				treeALL(&tenChild);
	            this->updated = false;	
			}
            for(int i=0;i<size;i++)printf("%s\n",tenChild.at(i).c_str());  
        }else{
            if(this->updated){
                tenChild.clear();
                treeFirst(5,&tenChild);
                treeBack(5,&tenChild);
                this->updated = false;
            }
            for(int i=0;i<5;i++)printf("%s\n",tenChild.at(i).c_str());
            printf("...\n");
            for(int i=9;i>=5;i--)printf("%s\n",tenChild.at(i).c_str());

        }
    
    }

private:
    void treeFirst(int num,vector<string>*bar){
        bar->push_back(name);
        if(--num==0)return;

        int n = children.size();
        map<string, Directory *>::iterator it = children.begin();
        while(n--){
            int sts = it->second->size;
            if(sts>=num){
                it->second->treeFirst(num,bar);
                return;
            }else{
                it->second->treeFirst(sts,bar);
                num-=sts;
            }
            it++;
        }
    }
  
    void treeBack(int num,vector<string>*bar){
        int n = children.size();
        map<string, Directory *>::iterator it = children.end();
        while(n--){
            it--;
            int sts = it->second->size;
            if(sts>=num){
                it->second->treeBack(num,bar);
                return;
            }else{
                it->second->treeBack(sts,bar);
                num-=sts;
            }
        }
        bar->push_back(name);
    }
 
    void treeALL(vector<string>*bar){
        bar->push_back(name);
        for(map<string, Directory *>::iterator it = children.begin();it!=children.end();it++){
//            cout<<"hello?treeALL"<<endl;
			it->second->treeALL(bar);
        }
    }
};

struct Command
{
    int type;
    string arg;
    Directory *tmpDir;
    Command(string s)
    {
        for (int i = 0; i < 7; i++)
        {
            if (cmd_names[i] == s)
            {
                type = i;
                if (i < 3)
                {
                    scanf("%s", tmps), arg = tmps;
                }
                return;
            }
        }
    }
};

void work()
{
    int n;
    scanf("%d", &n);
    Directory *now = new Directory("root", NULL);
    vector<Command *> cmdlist;
    while (n--)
    {
        scanf("%s", tmps);
        Command *cmd = new Command(tmps);
        switch (cmd->type)
        {
        case 0:
            cmd->tmpDir=now->mkdir(cmd->arg);
            if (cmd->tmpDir == NULL)
               _ERR;
//				printf("aloiha\n"); 
            else
            {
                _OK;
                cmdlist.push_back(cmd);
            }
            break;
        case 1:{
			
            cmd->tmpDir=now->rm(cmd->arg);
            if (cmd->tmpDir == NULL)
                _ERR;
            else
            {
                _OK;
                cmdlist.push_back(cmd);
            }
            break;
		}
        case 2:{
        	Directory *ch = now->cd(cmd->arg);
            if (ch == NULL)
                _ERR;
            else
            {
                _OK;
                cmd->tmpDir = now;
                now = ch;
                cmdlist.push_back(cmd);
            }
            break;
			
		}
            
        case 3:
            now->sz();
            break;
        case 4:
            now->ls();
            break;
        case 5:
            now->tree();
            break;
        case 6:
            bool flag = false;
            while (!flag && !cmdlist.empty())
            {
                cmd = cmdlist.back();
                cmdlist.pop_back();
                switch (cmd->type)
                {
                case 0:
                    flag = now->rm(cmd->tmpDir->name) != NULL;
                    break;
                case 1:
                	//???
                    flag = now->addChild(cmd->tmpDir) != NULL;
                    break;
                case 2:
                    now = cmd->tmpDir;flag=true;
                    break; /*ok=true;*/
                }
            }
            if (flag)  
                _OK;
            else
                _ERR;
            break;
        }
    }
}

int main()
{
    scanf("%d", &t);
    while (t--)
    {
        work();
    }
    return 0;
}

B - 东东学打牌

最近,东东沉迷于打牌。所以他找到 HRZ、ZJM 等人和他一起打牌。由于人数众多,东东稍微修改了亿下游戏规则:
1、所有扑克牌只按数字来算大小,忽略花色。
2、每张扑克牌的大小由一个值表示。A, 2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K 分别指代 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13。
3、每个玩家抽得 5 张扑克牌,组成一手牌!(每种扑克牌的张数是无限的,你不用担心,东东家里有无数副扑克牌)
理所当然地,一手牌是有不同类型,并且有大小之分的。
举个栗子,现在东东的 “一手牌”(记为 α),瑞神的 “一手牌”(记为 β),要么 α > β,要么 α < β,要么 α = β。
那么这两个 “一手牌”,如何进行比较大小呢?首先对于不同类型的一手牌,其值的大小即下面的标号;对于同类型的一手牌,根据组成这手牌的 5 张牌不同,其值不同。下面依次列举了这手牌的形成规则:
1、大牌:这手牌不符合下面任一个形成规则。如果 α 和 β 都是大牌,那么定义它们的大小为组成这手牌的 5 张牌的大小总和。
2、对子:5 张牌中有 2 张牌的值相等。如果 α 和 β 都是对子,比较这个 “对子” 的大小,如果 α 和 β 的 “对子” 大小相等,那么比较剩下 3 张牌的总和。
3、两对:5 张牌中有两个不同的对子。如果 α 和 β 都是两对,先比较双方较大的那个对子,如果相等,再比较双方较小的那个对子,如果还相等,只能比较 5 张牌中的最后那张牌组不成对子的牌。
4、三个:5 张牌中有 3 张牌的值相等。如果 α 和 β 都是 “三个”,比较这个 “三个” 的大小,如果 α 和 β 的 “三个” 大小相等,那么比较剩下 2 张牌的总和。
5、三带二:5 张牌中有 3 张牌的值相等,另外 2 张牌值也相等。如果 α 和 β 都是 “三带二”,先比较它们的 “三个” 的大小,如果相等,再比较 “对子” 的大小。
6、炸弹:5 张牌中有 4 张牌的值相等。如果 α 和 β 都是 “炸弹”,比较 “炸弹” 的大小,如果相等,比较剩下那张牌的大小。
7、顺子:5 张牌中形成 x, x+1, x+2, x+3, x+4。如果 α 和 β 都是 “顺子”,直接比较两个顺子的最大值。
8、龙顺:5 张牌分别为 10、J、Q、K、A。
作为一个称职的魔法师,东东得知了全场人手里 5 张牌的情况。他现在要输出一个排行榜。排行榜按照选手们的 “一手牌” 大小进行排序,如果两个选手的牌相等,那么人名字典序小的排在前面。
不料,此时一束宇宙射线扫过,为了躲避宇宙射线,东东慌乱中清空了他脑中的 Cache。请你告诉东东,全场人的排名

输入

输入包含多组数据。每组输入开头一个整数 n (1 <= n <= 1e5),表明全场共多少人。
随后是 n 行,每行一个字符串 s1 和 s2 (1 <= |s1|,|s2| <= 10), s1 是对应人的名字,s2 是他手里的牌情况。

输出

对于每组测试数据,输出 n 行,即这次全场人的排名。

样例输入

3
DongDong AAA109
ZJM 678910
Hrz 678910

样例输出

Hrz
ZJM
DongDong

思路

综述

之前做过一个类似的模拟题,当时有花色,这里没有花色区分了,反而简单了一些;
判断一手牌的大小是多关键字排序:
1、判断是什么类型的牌;
2、某类型里面也有大小顺序;

接收

因为存在JQK字符,所以采用字符串接收该牌;
需要注意10这个数字是两个字符;

判断一手牌是什么类型的

比较简单,采用暴力的思想,枚举了所有情况
会发现,如果将一手牌排序之后,再枚举情况会简单很多
关键点是第一行的sort函数

	sort(a,a+5);
	//判断是不是龙顺
	if(a[0]==1 && a[1]==10 && a[2]==11 && a[3]==12 && a[4]==13 ){
		node.value1 = 8;
		node.value2 = 1;
	}//顺子 
	else if(a[0]==a[1]-1 &&a[1]==a[2]-1 &&a[2]==a[3]-1 &&a[3]==a[4]-1){
		node.value1 = 7;
		node.value2 = a[4];
	}//炸弹 
	else if(a[0]==a[3] || a[1]==a[4]){
		node.value1 = 6;
		if(a[0]==a[3])node.value2 = a[2]*ONE + a[4];
		else node.value2 = a[2]*ONE + a[0];
	}//三带二 
	else if((a[0]==a[1] && a[2]==a[4])||(a[0]==a[2] && a[3]==a[4])){
		node.value1 = 5;
		if((a[0]==a[1] && a[2]==a[4])){
			node.value2 = a[4]*ONE + a[0]*TWO;
		}else if(a[0]==a[2] && a[3]==a[4]){
			node.value2 = a[0]*ONE + a[4]*TWO;
		}
	}//三个 
	else if(a[0]==a[2] || a[2]==a[4] || a[1] == a[3]){
		node.value1 = 4;
		if(a[0]==a[2]) node.value2 = a[0]*ONE + a[3] + a[4];
		else if(a[2]==a[4])node.value2 = a[2]*ONE + a[0] + a[1];
		else node.value2 = a[1]*ONE + a[0] + a[4];
	}// 两对
	else if((a[0]==a[1] && a[2]==a[3])||(a[0]==a[1] && a[3]==a[4])||(a[1]==a[2] && a[3]==a[4])){
		node.value1 = 3;
		if(a[0]==a[1] && a[2]==a[3])node.value2 = a[2]*ONE + a[1]*TWO + a[4];
		else if(a[0]==a[1] && a[3]==a[4])node.value2 = a[3]*ONE + a[1]*TWO + a[2];
		else if(a[1]==a[2] && a[3]==a[4])node.value2 = a[3]*ONE + a[2]*TWO + a[0];
	}//对子
	else if((a[0]==a[1])||(a[1]==a[2])||(a[2]==a[3])||(a[3]==a[4])){
		node.value1 = 2;
		if(a[0]==a[1])node.value2 = a[0]*ONE+a[2]+a[3]+a[4];
		else if(a[1]==a[2])node.value2 = a[1]*ONE+a[0]+a[3]+a[4];
		else if(a[2]==a[3])node.value2 = a[2]*ONE+a[0]+a[1]+a[4];
		else if(a[3]==a[4])node.value2 =a[3]*ONE+a[0]+a[1]+a[2]; 
	}//大牌 
	else{
		node.value1 = 1;
		node.value2 = a[0]+a[1]+a[2]+a[3]+a[4];
	}

注意

如果两个人的牌完全一样,则按照名字的字典序排序,字典序小的排在前面;
所以对per排序的时候,需要增加多关键字排序的一项,名字;

struct per{
	string name;
	string cards;
	int value1;
	int value2;
	bool operator < (const per &P)const{
		if(value1 != P.value1)return value1 < P.value1;
		else if(value2 != P.value2)return value2 < P.value2;
		else return name > P.name;
	}
};

代码

#include <iostream>
#include <string>
#include <queue>
#include <map>
#include <algorithm>
#define ONE 1000000
#define TWO 10000
using namespace std;
struct per{
	string name;
	string cards;
	int value1;
	int value2;
	bool operator < (const per &P)const{
		if(value1 != P.value1)return value1 < P.value1;
		else if(value2 != P.value2)return value2 < P.value2;
		else return name > P.name;
//		else return true;
	}
};
int tonum(char ch){
	if(ch=='A')return 1;
	if(ch=='J')return 11;
	if(ch=='Q')return 12;
	if(ch=='K')return 13;
	if(ch=='1')return 10;
	else{
		int num = ch - '0';
		return num;
	}
}
void calc(struct per &node ){
	int a[5];
	string s;
	s = node.cards;
	int j=0;
	for(int i=0;i<s.size();i++ ){
		a[j] = tonum(s[i]);
		if(s[i]=='1')i++;
		j++;
	}
	sort(a,a+5);
	//判断是不是龙顺
	if(a[0]==1 && a[1]==10 && a[2]==11 && a[3]==12 && a[4]==13 ){
		node.value1 = 8;
		node.value2 = 1;
	}//顺子 
	else if(a[0]==a[1]-1 &&a[1]==a[2]-1 &&a[2]==a[3]-1 &&a[3]==a[4]-1){
		node.value1 = 7;
		node.value2 = a[4];
	}//炸弹 
	else if(a[0]==a[3] || a[1]==a[4]){
		node.value1 = 6;
		if(a[0]==a[3])node.value2 = a[2]*ONE + a[4];
		else node.value2 = a[2]*ONE + a[0];
	}//三带二 
	else if((a[0]==a[1] && a[2]==a[4])||(a[0]==a[2] && a[3]==a[4])){
		node.value1 = 5;
		if((a[0]==a[1] && a[2]==a[4])){
			node.value2 = a[4]*ONE + a[0]*TWO;
		}else if(a[0]==a[2] && a[3]==a[4]){
			node.value2 = a[0]*ONE + a[4]*TWO;
		}
	}//三个 
	else if(a[0]==a[2] || a[2]==a[4] || a[1] == a[3]){
		node.value1 = 4;
		if(a[0]==a[2]) node.value2 = a[0]*ONE + a[3] + a[4];
		else if(a[2]==a[4])node.value2 = a[2]*ONE + a[0] + a[1];
		else node.value2 = a[1]*ONE + a[0] + a[4];
	}// 两对
	else if((a[0]==a[1] && a[2]==a[3])||(a[0]==a[1] && a[3]==a[4])||(a[1]==a[2] && a[3]==a[4])){
		node.value1 = 3;
		if(a[0]==a[1] && a[2]==a[3])node.value2 = a[2]*ONE + a[1]*TWO + a[4];
		else if(a[0]==a[1] && a[3]==a[4])node.value2 = a[3]*ONE + a[1]*TWO + a[2];
		else if(a[1]==a[2] && a[3]==a[4])node.value2 = a[3]*ONE + a[2]*TWO + a[0];
	}//对子
	else if((a[0]==a[1])||(a[1]==a[2])||(a[2]==a[3])||(a[3]==a[4])){
		node.value1 = 2;
		if(a[0]==a[1])node.value2 = a[0]*ONE+a[2]+a[3]+a[4];
		else if(a[1]==a[2])node.value2 = a[1]*ONE+a[0]+a[3]+a[4];
		else if(a[2]==a[3])node.value2 = a[2]*ONE+a[0]+a[1]+a[4];
		else if(a[3]==a[4])node.value2 =a[3]*ONE+a[0]+a[1]+a[2]; 
	}//大牌 
	else{
		node.value1 = 1;
		node.value2 = a[0]+a[1]+a[2]+a[3]+a[4];
	}
}
int n;
int main(){
	string s1,s2;
	per per1;
	while(cin>>n){
		priority_queue<per> qq;
		for(int i=0;i<n;i++){
			cin>>s1>>s2;
			per1.name = s1;
			per1.cards = s2;
			calc(per1);
			qq.push(per1);
		}
		while(!qq.empty()){
			cout<<qq.top().name<<endl;
			qq.pop();
		}
	}
	return 0;
}

C - 签到题,独立思考哈

SDUQD 旁边的滨海公园有 x 条长凳。第 i 个长凳上坐着 a_i 个人。这时候又有 y 个人将来到公园,他们将选择坐在某些公园中的长凳上,那么当这 y 个人坐下后,记k = 所有椅子上的人数的最大值,那么k可能的最大值mx和最小值mn分别是多少。

输入

第一行包含一个整数 x (1 <= x <= 100) 表示公园中长椅的数目
第二行包含一个整数 y (1 <= y <= 1000) 表示有 y 个人来到公园
接下来 x 个整数 a_i (1<=a_i<=100),表示初始时公园长椅上坐着的人数

输出

输出 mn 和 mx

样例输入

3
7
1
6
1

样例输出

6 13

思路

综述

简单的思维题:
K的最大值比较简单,在输入所有a_i的时候顺便记录一下里面的最大值,加上y即为mx;
最小值的话,可以让y个人先分配到非最大值的那个凳子,在其他的凳子需要均匀的分配,如果其他的凳子分配完成之后和最大值的凳子上人数一样之后,如果y还有人数的话,就将y均匀分配到所有的凳子。

代码

#include <iostream>
using namespace std;
int mn,mx;
int x,y;
int minx,maxx;
int main(){
	int a[100];
	cin>>x>>y;
	int temp;
	maxx = -1e5;
	for(int i=0;i<x;i++){
		cin>>a[i];
		if(a[i] > maxx){
			maxx = a[i];	
		}
	}
//	cout<<"maxx:"<<maxx<<endl;
	int maxxx = maxx + y; 
	for(int i=0;i<x;i++){
		int num = maxx - a[i];
		y -= num;
	}
	if(y<=0)minx=maxx;
	else{
		float num1;
		num1 = (float)y/(float)x;
		minx = maxx + num1;
		if(num1>(int)num1){
			minx++;
		}
	}
	
	cout<<(minx)<<" "<<(maxxx); 
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章