离散化 差分 欢乐赛2A
1. 链接:https://ac.nowcoder.com/acm/contest/16806/A
来源:牛客网
第一行包含一个正整数n,表示裁判的回答数(也是玩家的猜数次数)。
接下来n行,首先是猜的数,然后是一个空格,然后是一个符号。符号如果是“+”说明猜的数比答案大,“-”说明比答案小,“.”说明猜到了答案。
#include<bits/stdc++.h>
using
namespace
std;
map<
int
,
int
> mp;
int
main()
{
int
inf = 2147483647;
int
n;
scanf
(
"%d"
, &n);
for
(
int
i = 1; i <= n; i++)
{
int
x;
char
c;
scanf
(
"%d %c"
, &x, &c);
if
(c ==
'+'
) mp[0]++, mp[x]--;
else
if
(c ==
'-'
) mp[x+1]++, mp[inf]--;
else
mp[x]++, mp[x+1]--;
}
int
sum=0, maxn = 0;
for
(auto &it:mp)
{
sum += it.second;
maxn = max(maxn, sum);
}
printf
(
"%d"
, maxn);
return
0;
}
用map遍历的方式 自动来定序以及离散化//若普通离散化需要中间加一个间隔已保证数字存在
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步