离散化 差分 欢乐赛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遍历的方式 自动来定序以及离散化//若普通离散化需要中间加一个间隔已保证数字存在

浙公网安备 33010602011771号