P1563 玩具谜题

结论:

map在一些情况有种“对象”的意味,在JSON中,对象可以用K-V格式存储;mybatis中参数是map或者对象都可以实现解析。。。k-v格式的数据存储和对象可以相互转换。

使用map进行模拟

耗时1300ms。。。。不会优化了。。。,代码没精简,凑合看吧,其中if判断可以简写两两在一起,这里不改了。也不是主要要说的。

	int rolenum,ordernum;
cin>>rolenum>>ordernum;
map<string,int>maping;
int direction;
string name;
int bushu;
vector<string> roles;
roles.resize(rolenum);
for (int i = 0; i < rolenum; ++i) {
cin>>direction>>name;
maping[name]=direction;//0表示朝向圈内,1表示朝向圈外
roles[i]=name;
}
int index = 0;//当前位置
for (int i = 0; i < ordernum; ++i) {
cin>>direction>>bushu;
if(direction==0){// 表示向左数s人
//判断当前人的朝向
if(maping[roles[index]]==0){//0表示朝向圈内
index-=bushu;
index<0? index=roles.size()-abs(index) : index ;
} else{//1表示朝向圈外
index+=bushu;
index<roles.size()? index: index = abs(index)-roles.size();
}
}else{//表示向右数s人
//判断当前人的朝向
if(maping[roles[index]]==0){//0表示朝向圈内
index+=bushu;
index<roles.size()? index: index = abs(index)-roles.size(); } else{//1表示朝向圈外
index-=bushu;
index<0? index=roles.size()-abs(index) : index ;
}
}
}
cout<<roles[index];

对象模拟

这里把由map存储的方向信息设置在对象中,取消了map。

耗时位200ms,emmmm???提升了5倍,这想不通啊。。。将vector替换位数组后,耗时也是200+,所以没有重新分配(因为先resize了)内存块的情况下,vector和int数组增查的时间复杂度都是O(1)的

C++ map的实现是treemap,TreeMap的增删改查和统计相关的操作的时间复杂度都为 O(logn),N*log(N)耗时1000ms么。。。差距这么直观的吗。。。。

#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <ctime>
using namespace std;
static const auto y = []() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
return 0;
}();
class Person{
public:
int direction;//0表示朝向圈内,1表示朝向圈外
string name;//姓名
Person(int direction, const string &name) : direction(direction), name(name) {}
Person() {}
};
vector<Person> roles;
void fun3(){
int rolenum,ordernum;
cin>>rolenum>>ordernum;
roles.resize(rolenum);
for (int i = 0; i < rolenum; ++i) {
cin>>roles[i].direction>>roles[i].name;
}
int direction,bushu;
int index = 0;
for (int i = 0; i < ordernum; ++i) {
cin>>direction>>bushu;
if(direction==0){// 表示向左数s人
//判断当前人的朝向
if(roles[index].direction==0){//0表示朝向圈内
index-=bushu;
index<0? index=roles.size()-abs(index) : index ;
} else{//1表示朝向圈外
index+=bushu;
index<roles.size()? index: index = abs(index)-roles.size();
}
}else{//表示向右数s人
//判断当前人的朝向
if(roles[index].direction==0){//0表示朝向圈内
index+=bushu;
index<roles.size()? index: index = abs(index)-roles.size(); } else{//1表示朝向圈外
index-=bushu;
index<0? index=roles.size()-abs(index) : index ;
}
}
}
cout<<roles[index].name;
}
int main(){
fun3();
return 0;
}

Map与对象关系的思考之P1563玩具谜题的更多相关文章

  1. P1563 玩具谜题

    P1563 玩具谜题 题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: ...

  2. 洛谷 P1563 玩具谜题(模拟)

    嗯... 题目链接:https://www.luogu.org/problem/P1563 这道题主要问题就是弄明白顺逆时针的问题,其实可以简化成一个异或的问题:当head与x异或值为零时,即为顺时针 ...

  3. luogu P1563 玩具谜题

    https://www.luogu.org/problemnew/show/1563 题目: 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩 ...

  4. P1563 玩具谜题(简单模拟)

    就是一个简单模拟 #include<iostream> #include<string> using namespace std; ; int in[maxn], x[maxn ...

  5. 洛谷 P1563 玩具谜题

    如果你想不耗费脑力做出这个题目,往下看: 本萌新看到这个题目,想到了乘法法则,题目中左右方向要判断两次,很耗脑力,和乘法中的正负号判断非常像. 抽象一点:这个人向内向外就是乘法中括号外的正负号,他的左 ...

  6. 洛谷 P1563 玩具谜题【模拟/环】

    题目描述 小南有一套可爱的玩具小人, 它们各有不同的职业. 有一天, 这些玩具小人把小南的眼镜藏了起来. 小南发现玩具小人们围成了一个圈,它们有的面朝圈内,有的面朝圈外.如下图: 这时singer告诉 ...

  7. 洛谷P1563 玩具谜题 简单模拟

    没意义,注意方向别判错. Code: #include<cstdio> #include<cstring> using namespace std; const int max ...

  8. 死去活来,而不变质:Domain Model(领域模型) 和 EntityFramework 如何正确进行对象关系映射?

    写在前面 阅读目录: 设计误区 数据库已死 枚举映射 关联映射 后记 在上一篇<一缕阳光:DDD(领域驱动设计)应对具体业务场景,如何聚焦 Domain Model(领域模型)?>博文中, ...

  9. [原创]java WEB学习笔记81:Hibernate学习之路--- 对象关系映射文件(.hbm.xml):hibernate-mapping 节点,class节点,id节点(主键生成策略),property节点,在hibernate 中 java类型 与sql类型之间的对应关系,Java 时间和日期类型的映射,Java 大对象类型 的 映射 (了解),映射组成关系

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

随机推荐

  1. Visual Studio 的Build Event 使用

    rmdir Configuration mkdir Configuration Copy "$(ProjectDir)Configuration\Spec.config" &quo ...

  2. 用AntDeploy如何更新Agent

    AntDeploy AntDeploy是一款开源的一键发布部署工具,目的是代替重复性的发布动作,提高部署效率 1.一键部署iis 2.一键部署windows服务 3.一键部署到Docker 4.支持增 ...

  3. mysql事务隔离级别实验

    一.实验数据: 建表语句: CREATE TABLE `isolation` ( `id` int(11) NOT NULL, `name` varchar(255) CHARACTER SET ut ...

  4. nginx memcache缓存

    1 基本 在一个lnmp架构中,nginx遇到动态资源,会反向代理,把请求发送到后端的php-fpm服务,php-fpm从mysql里读取数据,生产网页,然后返回给client. 如果流量大,php- ...

  5. python之使用request模块发送post和get请求

    import requestsimport json #发送get请求并得到结果# url = 'http://api.nnzhp.cn/api/user/stu_info?stu_name=小黑马 ...

  6. YII2修改backend模块报错An Error occurred while handling another error: exception 'yii\base\InvalidRouteException' with message 'Unable to resolve the request "site/error".' in E:\project\demo\vendor\yiisoft

    报错内容: 原因:没有修改common/config/bootstrap.php里的别名 修改后:

  7. PopupWindow(1)简介

    PopupWindow有点类似于Dialog,相同点在于都是弹出窗口,并且都可以对其进行自定义显示,并且里面的监听组件,进行相应的操作,但它与Dialog又有很大的区别,PopupWindow只是弹出 ...

  8. h5-15-svg格式图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. php服务端接收post的json数据

    最近用到ext与PHP交互,ext把json数据post给PHP,但在PHP里面$_post获取不到,$_REQUEST也获取不到,但是通过firedebug看到的请求信息确实是把JSON数据post ...

  10. 2019/05/13 JAVA虚拟机堆内存调优

    -Xms4000m 堆内存初始值 * -Xmx4000m 堆内存最大值 * -XX:+PrintGCDetails 打印GC信息 * -XX:+UseSerialGC 使用串行GC * -XX:+Pr ...