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;
}

最新文章

  1. ubuntu16041,安装opencv3.1.0
  2. Beta版本冲刺———第六天
  3. Android7.0 Phone应用源码分析(二) phone来电流程分析
  4. C++字符串处理封装类String
  5. Python中处理时间 —— time模块
  6. JavaWeb学习笔记--2.3内置对象
  7. Swift String 一些常用方法
  8. XPath编写规则学习
  9. js实现定时器,时间倒计时为0后停止
  10. OpenApi开放平台架构实践
  11. redis 集群 遇坑1
  12. python与java的猜拳游戏
  13. &quot;远程服务器返回错误: (500) 内部服务器错误&quot;错误处理
  14. libevent 和 libev 提高网络应用性能
  15. RabbitMQ----整理
  16. How can I perform the likelihood ratio, Wald, and Lagrange multiplier (score) test in Stata?
  17. docker下安装tomcat
  18. serialize unserialize
  19. C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
  20. IOS开发 Missing submodule &#39;XXXX&#39; 警告

热门文章

  1. find 是区分大小写的。对于不区分大小写的写法(转载)
  2. 洛谷 - P2152 - SuperGCD - 高精度
  3. css margin边界叠加问题详谈
  4. python 模块和包的使用方法
  5. bzoj 3926: [Zjoi2015]诸神眷顾的幻想乡【SAM】
  6. websocket来回收发消息
  7. Qt样式表之三:实现按钮三态效果的三种方法
  8. DFS水题 URAL 1152 False Mirrors
  9. 1-8继承extends
  10. springMVC的架构与执行流程