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
样例
INPUT:
3
7
1
6
1
OUTPUT:
6 13
问题分析
这个题的题意是:若干个长椅,每个长椅上预先有部分人。此时又有几个人来到公园,他们可以随便坐在这些椅子上,那么在这些人的不同的坐法中,其中的长椅上的最大人数的最大值和最小值是多少。思路是:最大值很好求,所有人都坐在原本人最多的长椅上即可。最小值的计算方法是:首先计算长椅上原来的人数总和和新加入人数的和。比较其与最大值*长椅数的大小。如果发现大,那么最大人数的最小值为原来的人数总和和新加入人数的和/长椅数向上取整。否则,最大人数仍然为原来的最大值。
代码如下:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int main()
{
int x,y;
cin>>x>>y;
int max = 0;
int* num = new int[x+1];
for(int i = 1; i <= x; i++)
{
cin>>num[i];
if(num[i] >= max) max = num[i];
}
int left = 0;
for(int i = 1; i <= x; i++)
{
left += max - num[i];
}
//分配不满
if(y<left)
{
cout<<max<<" "<<max + y<<endl;
}
//分配满之后平均分配
else if(y>=left)
{
int a = y - left;
if(a%x == 0)
cout<<max + a/x<<" "<<max + y<<endl;
else
cout<<max + a/x +1<<" "<<max + y<<endl;
}
}
B - 东东学打牌:
问题描述
题目简述
最近,东东沉迷于打牌。所以他找到 HRZ、ZJM 等人和他一起打牌。由于人数众多,东东稍微修改了亿下游戏规则:
所有扑克牌只按数字来算大小,忽略花色。
每张扑克牌的大小由一个值表示。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。
每个玩家抽得 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 行,即这次全场人的排名。
INPUT:
3
DongDong AAA109
ZJM 678910
Hrz 678910
OUTPUT:
Hrz
ZJM
DongDong
解题思路:
这个题维护一个牌的类型,维护一个牌在该类型的大小数值即可,一直没有A的原因是知比较了首字母的字典序,没有比较后面的字典序,导致一直WA,更改之后就可以了。最主要的是,牌的大小应该用什么表示,我使用的是一个基数表示,比如三带二不同这种牌,二不同的最大和为K+Q = 25,所以基数用26,拍的大小可以表示为三张相同的大小乘26加两证不同牌的和。同样的其他的也可以。
代码如下
#include<iostream>
#include<cstring>
#include<iomanip>
#include<algorithm>
using namespace std;
struct node{
string name;
int type;
long long int num;
bool operator<(const node& a)const
{
if(type != a.type)
{
return type > a.type;
}
else if(num!=a.num){
return num > a.num;
}
else{
//前缀和,不一定只有第一个
return name < a.name;
}
}
}a[100010];
int pai[6];
int change(char card)
{
if(card>='1'&&card<='9')
return card - '0';
switch (card)
{
case 'A': return 1;
case 'J': return 11;
case 'Q': return 12;
case 'K': return 13;
}
}
void solve(int i)
{
//龙顺子
if(pai[1]==1&&pai[2]==10&&pai[3]==11&&pai[4]==12&&pai[5]==13)
{
a[i].type = 8;a[i].num =0;return;
}
//顺子
else if(pai[1]+1==pai[2]&&pai[2]+1==pai[3]&&pai[3]+1==pai[4]&&pai[4]+1==pai[5])
{
a[i].type = 7;a[i].num = pai[5];return;
}
//炸弹
else if(pai[1]==pai[2]&&pai[2]==pai[3]&&pai[3]==pai[4])
{
a[i].type = 6;a[i].num = pai[1]*100+pai[5]; return;
}
else if(pai[2]==pai[3]&&pai[3]==pai[4]&&pai[4]==pai[5])
{
a[i].type = 6;a[i].num = pai[4]*100+pai[1]; return;
}
//三代二
else if(pai[1]==pai[2]&&pai[2]==pai[3]&&pai[4]==pai[5])
{
a[i].type = 5;a[i].num = pai[1]*100+pai[4];return;
}
else if(pai[1]==pai[2]&&pai[3]==pai[4]&&pai[4]==pai[5])
{
a[i].type = 5;a[i].num = pai[1] + pai[3]*100;return;
}
//三带两个不同
else if(pai[1]==pai[2]&&pai[2]==pai[3])
{
//45 不同 最大的和为Q+K 为12+13 = 25 基数用26
a[i].type = 4;a[i].num = pai[1]*100+pai[4]+pai[5];return;
}
else if(pai[3]==pai[4]&&pai[4]==pai[5])
{
a[i].type = 4;a[i].num = pai[3]*100 + pai[1] + pai[2]; return;
}
else if(pai[2]==pai[3]&&pai[3]==pai[4])
{
a[i].type = 4;a[i].num = pai[2]*100 + pai[1] + pai[5];return;
}
else if(pai[1]==pai[2]&&pai[3]==pai[4])
{
a[i].type = 3;a[i].num = pai[3]*100*100 + pai[1]*100 +pai[5]; return;
}
else if(pai[1]==pai[2]&&pai[4]==pai[5])
{
a[i].type = 3;a[i].num = pai[4]*100*100 + pai[1]*100 +pai[3];return;
}
else if(pai[2]==pai[3]&&pai[4]==pai[5])
{
a[i].type = 3;a[i].num = pai[4]*100*100 + pai[2]*100 + pai[1];return;
}
//一对带三个不同 不同的最大值为JQK 11+12+13 = 36 基数用37
else if(pai[1]==pai[2])
{
a[i].type = 2;a[i].num = pai[1]*100 + pai[5]+pai[3]+pai[4];return;
}
else if(pai[2]==pai[3])
{
a[i].type = 2;a[i].num = pai[2]*100 + pai[1]+pai[4]+pai[5];return;
}
else if(pai[3]==pai[4])
{
a[i].type = 2;a[i].num = pai[3]*100 + pai[1]+pai[2]+pai[5];return;
}
else if(pai[4]==pai[5])
{
a[i].type = 2;a[i].num = pai[4]*100 + pai[1]+pai[2]+pai[3];return;
}
else{
a[i].type = 1;a[i].num = pai[1]+pai[2]+pai[3]+pai[4]+pai[5];return;
}
}
//void input(int i)
//{
// cin>>a[i].name;
// int pai[6];
// char card;
// string p;cin>>p;
// int now=0;
// for(int i = 1; i <= 5; i++)
// {
// if(p[now] == '1')
// {
// now++;now++;
// pai[i] = 10; continue;
// }
// pai[i] = change(p[now]);
// now++;
// }
// /*for(int i = 1; i<= 5; i++)
// cout<<pai[i]<<" ";*/
// solve(i,pai);
if(a[i].type ==1)cout<<a[i].type<<a[i].num<<endl;
if(a[i].type ==2)cout<<a[i].type<<" "<<a[i].num/37<<" "<<a[i].num%37<<endl;
if(a[i].type ==3)cout<<a[i].type<<" "<<a[i].num%14<<" "<<(a[i].num/14)%14<<" "<<((a[i].num/14)/14)%14<<endl;
if(a[i].type ==4)cout<<a[i].type<<" "<<a[i].num/26<<" "<<a[i].num%26<<endl;
if(a[i].type ==6||a[i].type ==8||a[i].type ==7||a[i].type==5)cout<<a[i].type<<" "<<a[i].num/14<<" "<<a[i].num%14<<endl;
//}
void input(int i)
{
int cnt=1;
string card;
cin>>a[i].name>>card; //读入姓名和牌
for(int j=0; j<card.length(); j++)
{ //遍历牌 输入
if(card[j]!='1')
{
pai[cnt] = change(card[j]);
cnt++;
continue;
}
pai[cnt] = 10;
cnt++;
j++;
}
sort(pai+1,pai+6);
solve(i); //获得判断各级条件
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
//memset(a,0,sizeof(a));
for(int i = 1; i <= n; i++)
input(i);
sort(a+1,a+n+1);
for(int i = 1;i <= n;i++)
cout<<a[i].name<<endl;
}
}
C- 东东的目录管理器:
问题描述
咕咕东的雪梨电脑的操作系统在上个月受到宇宙射线的影响,时不时发生故障,他受不了了,想要写一个高效易用零bug的操作系统 —— 这工程量太大了,所以他定了一个小目标,从实现一个目录管理器开始。前些日子,东东的电脑终于因为过度收到宇宙射线的影响而宕机,无法写代码。他的好友TT正忙着在B站看猫片,另一位好友瑞神正忙着打守望先锋。现在只有你能帮助东东!
初始时,咕咕东的硬盘是空的,命令行的当前目录为根目录 root。
目录管理器可以理解为要维护一棵有根树结构,每个目录的儿子必须保持字典序。
现在咕咕东可以在命令行下执行以下表格中描述的命令:
命令 类型 实现 说明
MKDIR s 操作 在当前目录下创建一个子目录 s,s 是一个字符串 创建成功输出 “OK”;若当前目录下已有该子目录则输出 “ERR”
RM s 操作 在当前目录下删除子目录 s,s 是一个字符串 删除成功输出 “OK”;若当前目录下该子目录不存在则输出 “ERR”
CD s 操作 进入一个子目录 s,s 是一个字符串(执行后,当前目录可能会改变) 进入成功输出 “OK”;若当前目录下该子目录不存在则输出 “ERR”
特殊地,若 s 等于 “…” 则表示返回上级目录,同理,返回成功输出 “OK”,返回失败(当前目录已是根目录没有上级目录)则输出 “ERR”
SZ 询问 输出当前目录的大小 也即输出 1+当前目录的子目录数
LS 询问 输出多行表示当前目录的 “直接子目录” 名 若没有子目录,则输出 “EMPTY”;若子目录数属于 [1,10] 则全部输出;若子目录数大于 10,则输出前 5 个,再输出一行 “…”,输出后 5 个。
TREE 询问 输出多行表示以当前目录为根的子树的前序遍历结果 若没有后代目录,则输出 “EMPTY”;若后代目录数+1(当前目录)属于 [1,10] 则全部输出;若后代目录数+1(当前目录)大于 10,则输出前 5 个,再输出一行 “…”,输出后 5 个。若目录结构如上图,当前目录为 “root” 执行结果如下,
UNDO 特殊 撤销操作 撤销最近一个 “成功执行” 的操作(即MKDIR或RM或CD)的影响,撤销成功输出 “OK” 失败或者没有操作用于撤销则输出 “ERR”
INPUT:
输入文件包含多组测试数据,第一行输入一个整数表示测试数据的组数 T (T <= 20);
每组测试数据的第一行输入一个整数表示该组测试数据的命令总数 Q (Q <= 1e5);每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。
每组测试数据的 2 ~ Q+1 行为具体的操作 (MKDIR、RM 操作总数不超过 5000);
面对数据范围你要思考的是他们代表的 “命令” 执行的最大可接受复杂度,只有这样你才能知道你需要设计的是怎样复杂度的系统。
output:
每组测试数据的输出结果间需要输出一行空行。注意大小写敏感。
INPUT:
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
OUTPUT:
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
代码如下:
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
const int Size=1e5+10;
const string op[]={"MKDIR","RM","CD","SZ","LS","TREE","UNDO"};
struct dir{
string name;
dir*parent;
int treesize;
vector<string> tenDes;
map<string,dir*> children;
bool update;
dir(string name)
{
this->name=name;
treesize=1;
parent=NULL;
}
void maintain(int num)
{
this->update=true;
treesize+=num;
if(parent!=NULL)
parent->maintain(num);
}
dir* mkdir(string s)
{
if(children.find(s)!=children.end())
return NULL;
dir*newdir=new dir(s);
newdir->parent=this;
children[s]=newdir;
maintain(1);
return newdir;
}
dir* rm(string s)
{
auto it=children.find(s);
if(it==children.end())
return NULL;
maintain(-1*it->second->treesize);
dir*temp=it->second;
children.erase(it);
return temp;
}
dir*cd(string s)
{
if(s=="..")
{
return parent;
}
else
{
auto it=children.find(s);
if(it==children.end())
return NULL;
else
return it->second;
}
}
void sz()
{
printf("%d\n",treesize);
}
void ls()
{
int sizeofch=children.size();
if(sizeofch==0)
printf("EMPTY\n");
else if(sizeofch<=10)
{
for(auto it=children.begin();it!=children.end();it++)
cout<<it->first<<endl;
}
else
{
auto it=children.begin();
for(int i=0;i<5;i++,it++)
cout<<it->first<<endl;
printf("...\n");
it=children.end();
for(int i=0;i<5;i++)it--;
for(int i=0;i<5;i++,it++)
cout<<it->first<<endl;
}
}
void treeAll(vector<string>&bar)
{
bar.push_back(name);
for(auto it=children.begin();it!=children.end();it++)
it->second->treeAll(bar);
}
void treeFirst(int num,vector<string>&bar)
{
bar.push_back(name);
if(--num==0)return;
int n=children.size();
auto it=children.begin();
while(n--)
{
int sts=it->second->treesize;
if(sts>=num)
{
it->second->treeFirst(num,bar);
return;
}
else
{
it->second->treeFirst(sts,bar);
num-=sts;
}
it++;
}
}
void treeLast(int num,vector<string>&bar)
{
int n=children.size();
auto it=children.end();
while(n--)
{
it--;
int sts=it->second->treesize;
if(sts>=num)
{
it->second->treeLast(num,bar);
return;
}
else
{
it->second->treeLast(sts,bar);
num-=sts;
}
}
bar.push_back(name);
}
void tree()
{
if(treesize==1)
printf("EMPTY\n");
else if(treesize<=10)
{
if(update)
{
tenDes.clear();
treeAll(tenDes);
update=false;
}
for(auto it=tenDes.begin();it!=tenDes.end();it++)
{
cout<<*it<<endl;
}
}
else
{
if(update)
{
tenDes.clear();
treeFirst(5,tenDes);
treeLast(5,tenDes);
update=false;
}
for(int i=0;i<5;i++)
cout<<tenDes[i]<<endl;
printf("...\n");
for(int i=9;i>=5;i--)
cout<<tenDes[i]<<endl;
}
}
bool addchild(dir*ch)
{
if(children.find(ch->name)!=children.end())
{
return false;
}
children[ch->name]=ch;
maintain(ch->treesize);
return true;
}
};
struct Command{
int type;
string arg;
dir*oldcmd;
void ini(string temp)
{
for(int i=0;i<7;i++)
{
if(temp==op[i])
{
type=i;
if(i<3)cin>>arg;
return;
}
}
}
}Com[Size];
int count;
vector<int> cmdlist;
dir*now=NULL;
string tmp;
int main() {
int T;
scanf("%d",&T);
while(T--)
{
count=-1;
cmdlist.clear();
now=new dir("root");
int n;
scanf("%d",&n);
while(n--)
{
cin>>tmp;
Com[++count].ini(tmp);
switch(Com[count].type)
{
case 0:
Com[count].oldcmd=now->mkdir(Com[count].arg);
if(Com[count].oldcmd==NULL)
printf("ERR\n");
else
printf("OK\n"),cmdlist.push_back(count);
break;
case 1:
Com[count].oldcmd=now->rm(Com[count].arg);
if(Com[count].oldcmd==NULL)
printf("ERR\n");
else
printf("OK\n"),cmdlist.push_back(count);
break;
case 2:
{
dir*ch=now->cd(Com[count].arg);
if(ch==NULL)printf("ERR\n");
else
{
printf("OK\n");
Com[count].oldcmd=now;
now=ch;
cmdlist.push_back(count);
}
break;
}
case 3:
now->sz();break;
case 4:now->ls();break;
case 5:now->tree();break;
case 6:
{
bool success=false;
while(!success&&!cmdlist.empty())
{
int n=cmdlist.back();cmdlist.pop_back();
switch(Com[n].type)
{
case 0:success=(now->rm(Com[n].arg)!=NULL);break;
case 1:success=now->addchild(Com[n].oldcmd);break;
case 2:now=Com[n].oldcmd;success=true;break;
}
}
if(success)
printf("OK\n");
else
printf("ERR\n");
}
}
}
}
return 0;
}