【2019牛客暑期多校第三场】J题LRU management
题意
好吧,这道题我其实看都没看过,队友跟我说了说这道题是模拟题,卡时间。然后我就上了……
大致就是维护一个线性表,然后有两种操作:插入、查询
插入时,如果这个值(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的更多相关文章
- [题解] 2019牛客暑期多校第三场H题 Magic Line
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...
- 2019 牛客暑期多校 第三场 F Planting Trees (单调队列+尺取)
题目:https://ac.nowcoder.com/acm/contest/883/F 题意:求一个矩阵最大面积,这个矩阵的要求是矩阵内最小值与最大值差值<=m 思路:首先我们仔细观察范围,我 ...
- 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)
题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...
- 2019 牛客暑期多校 第八场 A All-one Matrices (单调栈+前缀和)
题目:https://ac.nowcoder.com/acm/contest/888/A 题意:找全1矩阵的个数,并且这个全1矩阵不被其他全1矩阵包含 思路:这里引用付队说的话 -> { 这类问 ...
- 2019牛客暑期多校第六场题解ABDJ
A.Garbage Classification 传送门 题意:给你两个串,第一个串s由小写字母组成,第二个串t由dwh组成,长度为26,分别表示字母a到z代表的字符.现在要你判断: 如果字符串中‘h ...
- 2019牛客暑期多校第五场题解ABGH
A.digits 2 传送门 题意:给你一个n,要求输出一个每一位数字之和能整除n且其本身也能整除n的数.n不超过100,要求的数不超过10000位数. 题解:直接将n输出n次. 代码: #inclu ...
- 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem
题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3 4 2 3 4 输出:0 0 1 题解: 认真想一 ...
- 2019牛客全国多校第八场A题 All-one Matrices(单调栈)
题意:让你找最大不可扩展全1子矩阵的数量: 题解:考虑枚举每一行为全1子矩阵的的底,然后从左到右枚举:up[i][j]:表示(i,j)这个位置向上可扩展多少,同时还有记录每个位置(i,j)向左最多可扩 ...
- 2020牛客暑期多校训练营 第二场 J Just Shuffle 置换 群论
LINK:Just Shuffle 比较怂群论 因为没怎么学过 置换也是刚理解. 这道题是 已知一个置换\(A\)求一个置换P 两个置换的关键为\(P^k=A\) 且k是一个大质数. 做法是李指导教我 ...
随机推荐
- Google是如何做测试的?
Google是如何做测试的?(一.二) 导读:本文译自 James Whittaker 在 Google 测试官方博客发表的文章<How Google TestsSoftware >. 在 ...
- nGrinder 介绍与安装
nGrinder是基于Grinder开源项目,但由NHN公司的nGrinder开发团队进行了重新设计和完善(所以叫做nGrinder). 它是由一个controller和连接它的多个agent组成,用 ...
- MySQL 的 RowNum 实现(排行榜计算用户排名)
1. 计算用户排名最高效的方法 例如:通过用户分享个数排名,那么自己的排名就是:比自己分享数多的用户个数 + 1 ' and `count` > '自己分享个数' 缺点:当多个用户分享个数相同的 ...
- sublime安装vue插件
1.打开sublime text 3按 Ctrl+Shift+P(相信你有单身的手速,同时按完这3个键) 2.选中上图中,框出来的内容,按下enter. 3.选择上图的第二个即:vue syntax ...
- nginx设置目录浏览及中文乱码问题解决
在Nginx下默认是不允许列出整个目录的.如需此功能, 先打开nginx.conf文件,在location server 或 http段中加入 autoindex on;另外两个参数最好也加上去: a ...
- py基础之数据类型及基本语法
'''python中有五种数据类型,分别是整数.浮点数.字符串.布尔值.空值'''a = 1b = 2.0c = 'hello,world'print (a,b,c)#a是整数,b是浮点数,c是字符串 ...
- HBuilder-X 关闭eslint-vue 插件语法检查
HBuilder-X 在写vue项目的时候发现,代码在保存的时候回自动检查eslint语法,会报一大堆的红色警告! 这时候就很烦人,看着不爽,看了下eslint 配置里面介绍了关闭语法检查的配置-- ...
- hadoop之完全分布式集群配置(centos7)
一.基础环境 现在我们有两台虚拟机了,再克隆两台: 克隆好之后需要做三件事:1.更改主机名称 2.修改ip地址 3.将ip地址和对应的主机号加入到/etc/hosts文件中 1.永久修改主机名 hos ...
- Eclipse与MyEclipse的联系和区别
Eclipse与MyEclipse的联系和区别 Eclipse 是一个IDE(Integrated Developing Environment),而这个IDE是允许安装第三方开发的插件来使自身的功 ...
- 单页面和多页面的网页差别比较(SPA)
单页面应用(singlePAge Web Application) 多页面应用MultiPage Applicaton,MPA) 组成 一个外壳页面和多个页面片段组成 多个完整的页面组成 资源公用 ...