CF46B T-shirts from Sponsor 题解
Content
有一家服装店,有 \(\texttt{S}\) 码的衣服 \(n_S\) 件、\(\texttt{M}\) 码的衣服 \(n_M\) 件,\(\texttt{L}\) 码的衣服 \(n_L\) 件,\(\texttt{XL}\) 码的衣服 \(n_{XL}\) 件,\(\texttt{XXL}\) 码的衣服 \(n_{XXL}\) 件。
有 \(k\) 个人依次进来买衣服,每个人都有自己期望的尺码。如果服装店里面刚好有这个尺码的衣服,TA 就会拿上这件衣服离开,否则 TA 会选择尽可能接近自己的期望尺码的衣服(如果在这样的情况下有多种选择,则选择尺码较大的)。求这 \(k\) 个人最后各拿了什么尺码的衣服。
数据范围:\(1\leqslant n_S,n_M,n_L,n_{XL},n_{XXL}\leqslant 1000,1\leqslant k\leqslant n_S+n_M+n_L+n_{XL}+n_{XXL}\leqslant 1000\)。
Solution
这题目模拟就好,就是代码稍微长了一些。
我们先看是否有每个人的期望尺码的衣服,如果有那就直接选择,否则分别向大尺码和小尺码依次遍历,大尺码先遍历到就选择大尺码,小尺码先遍历到就选择小尺码。因为数据的特殊性,无需判断是否有足够的衣服。
Code
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
int sze[7], k;
string str;
const string ans[6] = {"", "S", "M", "L", "XL", "XXL"};
int main() {
for(int i = 1; i <= 5; ++i) scanf("%d", &sze[i]);
scanf("%d", &k);
while(k--) {
cin >> str;
if(str == "S") {
if(sze[1]) {puts("S"); sze[1]--;}
else {
int cur = 1;
while(1) {
cur = min(5, cur + 1);
if(sze[cur]) {
cout << ans[cur] << endl;
sze[cur]--;
break;
}
}
}
} else if(str == "M") {
if(sze[2]) {puts("M"); sze[2]--;}
else {
int curl = 2, curr = 2;
while(1) {
curl = max(1, curl - 1), curr = min(5, curr + 1);
if(sze[curr]) {
cout << ans[curr] << endl;
sze[curr]--;
break;
} else if(sze[curl]) {
cout << ans[curl] << endl;
sze[curl]--;
break;
}
}
}
} else if(str == "L") {
if(sze[3]) {puts("L"); sze[3]--;}
else {
int curl = 3, curr = 3;
while(1) {
curl = max(1, curl - 1), curr = min(5, curr + 1);
if(sze[curr]) {
cout << ans[curr] << endl;
sze[curr]--;
break;
} else if(sze[curl]){
cout << ans[curl] << endl;
sze[curl]--;
break;
}
}
}
} else if(str == "XL") {
if(sze[4]) {puts("XL"); sze[4]--;}
else {
int curl = 4, curr = 4;
while(1) {
curl = max(1, curl - 1), curr = min(5, curr + 1);
if(sze[curr]) {
cout << ans[curr] << endl;
sze[curr]--;
break;
} else if(sze[curl]) {
cout << ans[curl] << endl;
sze[curl]--;
break;
}
}
}
} else if(str == "XXL") {
if(sze[5]) {puts("XXL"); sze[5]--;}
else {
int cur = 5;
while(1) {
cur = max(1, cur - 1);
if(sze[cur]) {
cout << ans[cur] << endl;
sze[cur]--;
break;
}
}
}
}
}
}
CF46B T-shirts from Sponsor 题解的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
随机推荐
- 还有这种好事!netty自带http2的编码解码器framecodec
目录 简介 Http2FrameCodec Http2Frame.Http2FrameStream和Http2StreamFrame Http2FrameCodec的构造 Stream的生命周期 流控 ...
- 干掉if-else的方法
策略模式+工厂方法消除if else 假设需求为,根据不同勋章类型,处理相对应的勋章服务,优化前有以下代码: String medalType = "guest"; if (&qu ...
- 解决texlive化学式转换镜像经常偶发性进程堆积导致卡顿问题
前言 之前在 使用Python定时清理运行超时的pdflatex僵尸进程 博文中我采用python脚本开启定时任务清理pdflatex僵尸进程,线上4u2G的k8s pod部署了3个,pdflatex ...
- 洛谷 P6383 -『MdOI R2』Resurrection(DP)
洛谷题面传送门 高速公路上正是补 blog 的时候,难道不是吗/doge,难不成逆在高速公路上写题/jy 首先形成的图显然是连通图并且有 \(n-1\) 条边.故形成的图是一棵树. 我们考虑什么样的树 ...
- 洛谷 P5071 - [Ynoi2015] 此时此刻的光辉(莫队)
洛谷题面传送门 一道其实算得上常规的题,写这篇题解是为了总结一些数论中轻微(?)优化复杂度的技巧. 首先感性理解可以发现该问题强于区间数颜色问题,无法用常用的 log 数据结构维护,因此考虑分块/莫队 ...
- Linux—crontab 定期执行程序的命令
crontab [ -u user ] { -l | -r | -e } 说明: crontab 是用来让使用者在固定时间或固定间隔执行程序之用,换句话说,也就是类似使用者的时程表. -u user ...
- 4.Reverse Words in a String-Leetcode
class Solution { public: void reverseWords(string &s) { vector<string> data; string word; ...
- 在Idea上用JDBC连接mysql数据库
一.前言 本次操作建立在idea中java环境已配置的基础上 二.操作步骤 1.建立Web项目后,添加驱动包 mysql-connector-java-5.0.8-bin.jar (1)下载mysql ...
- HTML 基本标签2
HTML标题通过<h1>-<h6>标签定义(<h1>定义最大的标题,<h6>定义最小的标题) <html>用于定义HTML文档 HTML段落 ...
- python写的多项式符号乘法
D:\>poly.py(x - 1) * (x^2 + x + 1) = x^3 - 1 1 import ply.lex as lex # pip install ply 2 import p ...