题目链接

题意

好吧,这道题我其实看都没看过,队友跟我说了说这道题是模拟题,卡时间。然后我就上了……

大致就是维护一个线性表,然后有两种操作:插入、查询

插入时,如果这个值(string)之前出现过,则把之前那个值(string)放到线性表的表尾(删去原来那个),但是保存的值(int)仍是之前那个值(int)。如果没有出现过,则把它插入到表尾。如果插入后发现线性表长度超过 m ,则弹出表头的元素。

查询时,如果有这个值(string),然后根据要求查询这个值(string)的上一个或者下一个,再返回它的值(int),如果没有(没有上一个或者下一个也是)则输出:Invalid

分析

一开始觉得这个……应该就是拿STL可以暴力过的(当然不能太暴力)我选择了 unordered_map + list

听说用 map 会 T,没试过……

unordered_map 是哈希表,而 map 是红黑树,相对而言, map 的查询、插入、删除的时间比较稳定,都是 O(logN),而 unordered_map 的时间不确定性比较大,运气好就是 O(1) 的查询,运气差就是 O(N)

复杂度

平均为常数,最坏情况与容器大小成线性。、

摘自cppreference

unordered_map 用 string 作为索引,保存了 list 的迭代器

list 保存了值的顺序情况,包括了 string 和 int 两个变量

但是我第一发居然T了,然后加了快读就AC了,感觉就是被卡常了……

AC代码

#include <bits/stdc++.h>

using namespace std;

typedef list<pair<int, string>>::iterator pl;
unordered_map<string, pl> ump;
list<pair<int, string>> lists;
char catchmessage[100]; struct ioss
{
#define endl '\n'
static const int LEN = 20000000;
char obuf[LEN], *oh = obuf;
std::streambuf *fb;
ioss()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
fb = cout.rdbuf();
}
inline char gc()
{ static char buf[LEN], *s, *t, buf2[LEN];
return (s == t) && (t = (s = buf) + fread(buf, 1, LEN, stdin)), s == t ? -1 : *s++;
}
inline ioss &operator>>(long long &x)
{
static char ch, sgn, *p;
ch = gc(), sgn = 0;
for (; !isdigit(ch); ch = gc())
{
if (ch == -1)
return *this;
sgn |= ch == '-';
}
for (x = 0; isdigit(ch); ch = gc())
x = x * 10 + (ch ^ '0');
sgn && (x = -x);
return *this;
}
inline ioss &operator>>(int &x)
{
static char ch, sgn, *p;
ch = gc(), sgn = 0;
for (; !isdigit(ch); ch = gc())
{
if (ch == -1)
return *this;
sgn |= ch == '-';
}
for (x = 0; isdigit(ch); ch = gc())
x = x * 10 + (ch ^ '0');
sgn && (x = -x);
return *this;
}
inline ioss &operator>>(char &x)
{
static char ch;
for (; !isalpha(ch); ch = gc())
{
if (ch == -1)
return *this;
}
x = ch;
return *this;
}
inline ioss &operator>>(string &x)
{
static char ch, *p, buf2[LEN];
for (; !isalpha(ch) && !isdigit(ch); ch = gc())
if (ch == -1)
return *this;
p = buf2;
for (; isalpha(ch) || isdigit(ch); ch = gc())
*p = ch, p++;
*p = '\0';
x = buf2;
return *this;
}
inline ioss &operator<<(string &c)
{
for (auto &p : c)
this->operator<<(p);
return *this;
}
inline ioss &operator<<(const char *c)
{
while (*c != '\0')
{
this->operator<<(*c);
c++;
}
return *this;
}
inline ioss &operator<<(const char &c)
{
oh == obuf + LEN ? (fb->sputn(obuf, LEN), oh = obuf) : 0;
*oh++ = c;
return *this;
}
inline ioss &operator<<(int x)
{
static int buf[30], cnt;
if (x < 0)
this->operator<<('-'), x = -x;
if (x == 0)
this->operator<<('0');
for (cnt = 0; x; x /= 10)
buf[++cnt] = x % 10 | 48;
while (cnt)
this->operator<<((char)buf[cnt--]);
return *this;
}
inline ioss &operator<<(long long x)
{
static int buf[30], cnt;
if (x < 0)
this->operator<<('-'), x = -x;
if (x == 0)
this->operator<<('0');
for (cnt = 0; x; x /= 10)
buf[++cnt] = x % 10 | 48;
while (cnt)
this->operator<<((char)buf[cnt--]);
return *this;
}
~ioss()
{
fb->sputn(obuf, oh - obuf);
}
} io; int main()
{
#ifdef ACM_LOCAL
freopen("./in.txt", "r", stdin);
freopen("./out.txt", "w", stdout);
#endif
ios::sync_with_stdio(false);
int t;
io >> t;
while (t--)
{
ump.clear();
lists.clear();
int q, m;
io >> q >> m;
string s;
int op, val;
for (int i = 0; i < q; i++)
{
pl cur;
io >> op >> s >> val;
if (op)
{
if (!ump.count(s))
{
cout << "Invalid" << endl;
continue;
}
cur = ump[s];
if (val == 1)
{
cur++;
if (cur == lists.end())
{
cout << "Invalid" << endl;
continue;
}
}
else if (val == -1)
{
if (cur == lists.begin())
{
cout << "Invalid" << endl;
continue;
}
cur--;
}
cout << (*cur).first << endl;
}
else
{
if (!ump.count(s))
{
pair<int, string> newnode = make_pair(val, s);
lists.push_back(newnode);
pl tmp = lists.end();
tmp--;
ump.insert(make_pair(s, tmp));
if (lists.size() > m)
{
ump.erase(lists.front().second);
lists.pop_front();
}
cout << val << endl;
continue;
}
cur = ump[s];
pair<int, string> newnode = make_pair((*cur).first, s);
lists.push_back(newnode);
pl tmp = lists.end();
tmp--;
ump[s] = tmp;
lists.erase(cur);
cout << newnode.first << endl;
}
}
}
return 0;
}

【2019牛客暑期多校第三场】J题LRU management的更多相关文章

  1. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

  2. 2019 牛客暑期多校 第三场 F Planting Trees (单调队列+尺取)

    题目:https://ac.nowcoder.com/acm/contest/883/F 题意:求一个矩阵最大面积,这个矩阵的要求是矩阵内最小值与最大值差值<=m 思路:首先我们仔细观察范围,我 ...

  3. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  4. 2019 牛客暑期多校 第八场 A All-one Matrices (单调栈+前缀和)

    题目:https://ac.nowcoder.com/acm/contest/888/A 题意:找全1矩阵的个数,并且这个全1矩阵不被其他全1矩阵包含 思路:这里引用付队说的话 -> { 这类问 ...

  5. 2019牛客暑期多校第六场题解ABDJ

    A.Garbage Classification 传送门 题意:给你两个串,第一个串s由小写字母组成,第二个串t由dwh组成,长度为26,分别表示字母a到z代表的字符.现在要你判断: 如果字符串中‘h ...

  6. 2019牛客暑期多校第五场题解ABGH

    A.digits 2 传送门 题意:给你一个n,要求输出一个每一位数字之和能整除n且其本身也能整除n的数.n不超过100,要求的数不超过10000位数. 题解:直接将n输出n次. 代码: #inclu ...

  7. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  8. 2019牛客全国多校第八场A题 All-one Matrices(单调栈)

    题意:让你找最大不可扩展全1子矩阵的数量: 题解:考虑枚举每一行为全1子矩阵的的底,然后从左到右枚举:up[i][j]:表示(i,j)这个位置向上可扩展多少,同时还有记录每个位置(i,j)向左最多可扩 ...

  9. 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论

    LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...

随机推荐

  1. 使用Win10自带的虚拟机安装Linux

    Win10自带了一款虚拟机,感觉不错,可以试用一下: 安装:http://news.mydrivers.com/1/650/650018.htm 主要思路就是在BIOS里面开启CPU虚拟化,然后在Wi ...

  2. Selenium&Pytesseract模拟登录+验证码识别

    验证码是爬虫需要解决的问题,因为很多网站的数据是需要登录成功后才可以获取的. 验证码识别,即图片识别,很多人都有误区,觉得这是爬虫方面的知识,其实是不对的. 验证码识别涉及到的知识:人工智能,模式识别 ...

  3. Shell之Here Document

    EOF本意是 End Of File,表明到了文件末尾. 使用格式基本是这样的: 命令 << EOF 内容段EOF将“内容段”整个作为命令的输入.你的代码里就是用cat命令读入整段字符串并 ...

  4. C++走向远洋——60(项目四、立体类族共有的抽象类)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  5. Vmware Workstation 15 Pro安装Arch Linux并配置Docker

    主机配置: CPU:Intel Core i7-7700HQ 2.8GHz 2.8GHz 内存:16GB 操作系统:Windows 10 Home 64bit 双显卡:Intel HD Graphic ...

  6. java网络编程——socket实现简单的CS会话

    还记得当年学计网课时用python实现一个简单的CS会话功能,这也是学习socket网络编程的第一步,现改用java重新实现,以此记录. 客户端 import java.io.*; import ja ...

  7. PHPRAP 1.0.2 发布,修复安装失败 Bug 和优化细节

    PHPRAP,是一个PHP轻量级开源API接口文档管理系统,致力于减少前后端沟通成本,提高团队协作开发效率,打造PHP版的RAP. 更新记录 [修复]修复在MySQL5.5版本下安装数据初始化sql文 ...

  8. proxyTable的配置

    在dev环境下面: proxyTable: { '/api': { target: 'http://api.douban.com/v2', //主域名,以前我都写192.168.2.57:80,这里跨 ...

  9. 编程史话第四期-饱受争议的前端之王JavaScript的血泪成长史

    写在前面 这篇博文主要介绍javaScript的发展史,根据作者在B站发布的同名视频的文案整理修改而成,对视频感兴趣的博友可访问https://www.bilibili.com/video/av945 ...

  10. python框架Django实战商城项目之工程搭建

    项目说明 该电商项目类似于京东商城,主要模块有验证.用户.第三方登录.首页广告.商品.购物车.订单.支付以及后台管理系统. 项目开发模式采用前后端不分离的模式,为了提高搜索引擎排名,页面整体刷新采用j ...