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); 
	
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章