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. html行内要素与块级要素

    行内要素:在一行里,不可设置width和height,不能上下外铺(margin) span 块状要素,标准的 div

  2. 洛谷 - P1987 - 摇钱树 - dp - 贪心

    https://www.luogu.org/problemnew/show/P1987 这道题,假如是n==k,也就是把所有的树都砍完,我就知道要贪心去做,因为树给的初始金币是固定的,每天掉金币,当然 ...

  3. on a null object reference 问题的解决办法

    FATAL EXCEPTION: …… java.lang.RuntimeException: Unable to start activity ComponentInfo…… ava.lang.Nu ...

  4. 瞎比比系列---1st

    A - 项目管理HDU4858 /* 题意: 这个项目有n个节点, 两个节点间可能有多条边,不过一条边的两端必然是不同的节点. 0的时候:接下来两个数u v表示给项目u的能量值加上v: 1的时候: 这 ...

  5. 如何让Android微博个人详情页滚动到顶部

    版权声明:本文为xing_star原创文章,转载请注明出处! 本文同步自http://javaexception.com/archives/103 个人详情页滑动到顶部 最近产品提了个新需求,需要实现 ...

  6. JS获取屏幕的大小

    <html><script>function a(){document.write("屏幕分辨率为:"+screen.width+"*" ...

  7. 项目上线后出现Bug,该如何处理?

    项目在上线之后又出现了Bug,这让很多测试人员和开发人员头痛.但很多时候线上Bug普遍地存在,不可避免. 任何项目都存在未发现 Bug  和 已发现 Bug  两种情况,不存在没有 Bug的情况. 即 ...

  8. EXBSGS

    http://210.33.19.103/problem/2183 参考:https://blog.csdn.net/frods/article/details/67639410(里面代码好像不太对) ...

  9. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  10. EJB Timer Service is not available. Timers for application with id 95795415990861824 will not be deleted

    delete follows:glassfish\domains\domain1\applications\ejb-timer-service-appglassfish\domains\domain1 ...