std::map(无脑维护)

操作序列

http://www.nowcoder.com/questionTerminal/8509ba5ce0184ceb82c9f9b5b5d978b6

这题灵活运用了std::map

- 给出一个长度无限的数列,初始全部为零,有三种操作:

- 增加操作:给下标为 t 的数加 c 。特别注意,如果在下标 [t-30,t+30] 内有不为零的数,增加操作无效。

- 削减操作:让数列中下标最小的不为零数变为零。

- 查询操作:查询数列中下标为 tt 的数字是多少。

Face

std::map

tutorial:无脑维护

#include <bits/stdc++.h>
#include <bits/extc++.h>

using namespace std;
#define _rep(n, a, b) for (ll n = (a); n <= (b); ++n)
#define _rev(n, a, b) for (ll n = (a); n >= (b); --n)
#define _for(n, a, b) for (ll n = (a); n < (b); ++n)
#define _rof(n, a, b) for (ll n = (a); n > (b); --n)
#define oo 0x3f3f3f3f3f3f
#define ll long long
#define db double
#define eps 1e-6
#define bin(x) cout << bitset<10>(x) << endl;
#define what_is(x) cerr << #x << " is " << x << endl
#define met(a, b) memset(a, b, sizeof(a))
#define mp(a, b) make_pair(a, b)
#define all(x) x.begin(), x.end()
#define pii pair<ll, ll>
#define pdd pair<db, db>
#define pi acos(-1.0)
const ll maxn = 3e5 + 10;
const ll mod = 1e9 + 7;
map<ll, ll> q;
signed main()
{
    ll n;
    cin >> n;
    _rep(i, 1, n)
    {
        ll fir, sec = oo;
        cin >> fir;
        char ch = getchar();
        if (ch != '\n')
        { //两个数
            cin >> sec;
        }
        if (fir < 0)
        {
            if (q.empty())
                cout << "skipped" << endl;
            else
            {
                cout << q.begin()->second << endl;
                q.erase(q.begin());
            }
        }
        else if (sec == oo)
        {
            if(q.find(fir) != q.end())cout << q[fir] << endl;
            else cout << 0 << endl;
        }
        else if (sec != oo)
        {
            bool add = 1;
            _rep(i, fir - 30, fir + 30)
            {
                if (q.find(i) != q.end()){
                    add = 0;
                    break;
                }
            }
            if(add){
                q[fir] = sec;
            }
        }
    }
}
全部评论
迎评
1 回复 分享
发布于 2020-02-23 00:18
气死我类
1 回复 分享
发布于 2020-02-23 13:26
只要map中所有元素都为0,empty的返回值就是true吗
1 回复 分享
发布于 2020-02-24 17:06

相关推荐

01-02 00:50
三峡大学 Java
程序员牛肉:这简历一出手就离失业不远了。 作为一家公司来讲,我如果要招日常实习生,那我对实习生最基本的要求就是要能干活,毕竟你就待三四个月,谁会留心培养你? 那么除了院校之外,最重要的就是项目和实习了。没有实习的话项目就好好搞。 但是你说你这个项目吧:课程作业管理系统和TMS运输管理系统。这两个基本就和闹着玩差不多。 你作为一个想要应聘Java开发实习生的人,对后端的理解还仅仅停留在:“使用mapper和sql映射”,“使用SQL进行多表调用”,“基于MySQL简历表结构”,“基于Spring boot完成CURD操作”这种玩具上......... 找不到后端实习的
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务