7-13 航空公司VIP客户查询 (25 分)
题意:

思路:
读完题目之后的第一思路就是用map将客户的id(string类型)与里程road(int类型)形成映射,然后直接用id查找添加里程或输出里程。但是400ms的限制妥妥的超时了。然后意识到要用哈希做,但是用哈希就有一点不好解决,每个客户的里程怎么保存,考虑了很长时间无果,搜了一下博客,发现可以用结构体来保存,平常用数组模拟链表的时候都是直接开的一维数组,所以当每个客户的信息多了后就没有结构体来的理解方便了。
第一次尝试:这个题N的上限是1e5,所以将每个客户的id转换成long long类型,然后对1e5+7取余,结果作为下标对应到数组里边存里程,完美过样例,提交果断WA。。。。。。。
第二次尝试:又看了一遍题目,思考了一下,哈希会出现冲突,需要处理冲突。所以数组模拟链表处理冲突,提交AC。
代码:
#include <iostream>
#include <queue>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
#include <map>
#include <vector>
#define INF 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin) using namespace std;
typedef long long ll;
typedef pair<int,string> P;
const int maxn = 1e5+;
int head[maxn];
struct Peo{
char name[];
int road;
int next;
}p[maxn];
int cnt = ; int GetId(char* str) {
ll res = ;
for(int i = ; i<strlen(str); i++) {
if(isdigit(str[i])) {
res = res* + str[i]-'';
} else {
res = res* + ;
}
}
return (int)(res%maxn);
} void Add_Node(char* str,int id,int temp){
bool ok = true;
for(int i = head[id]; ~i; i = p[i].next){
if(strcmp(str,p[i].name) == ){
p[i].road += temp;
ok = false;
}
}
if(ok){
int i = ;
p[cnt].road += temp;
for(i = ; str[i]; i++){
p[cnt].name[i] = str[i];
}
p[cnt].name[i] = '\0';
p[cnt].next = head[id];
head[id] = cnt++;
}
return;
} bool ToFind(char* str,int id){
for(int i = head[id]; i!=-; i = p[i].next){
if(strcmp(str, p[i].name) == ){
cout<<p[i].road<<endl;
return true;
}
}
return false;
} int main() {
char str[];
int n,k,temp;
cin>>n>>k;
memset(head,-,sizeof(head));
for(int i = ; i<n; i++) {
cin>>str>>temp;
if(temp<k) temp = k;
int index = GetId(str);
Add_Node(str,index,temp);
}
int m;
cin>>m;
for(int i = ; i<m; i++) {
cin>>str;
int index = GetId(str);
if(!ToFind(str,index)){
printf("No Info\n");
}
}
return ;
}
7-13 航空公司VIP客户查询 (25 分)的更多相关文章
- 5-45 航空公司VIP客户查询 (25分) HASH
不少航空公司都会提供优惠的会员服务,当某顾客飞行里程累积达到一定数量后,可以使用里程积分直接兑换奖励机票或奖励升舱等服务.现给定某航空公司全体会员的飞行记录,要求实现根据身份证号码快速查询会员里程积分 ...
- 12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁
12. The Biggest Safety Threat Facing Airlines 航空公司面临的最大安全威胁 (1) The biggest safety threat facing air ...
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- L2-001 紧急救援 (25 分)
L2-001 紧急救援 (25 分) 作为一个城市的应急救援队伍的负责人,你有一张特殊的全国地图.在地图上显示有多个分散的城市和一些连接城市的快速道路.每个城市的救援队数量和每一条连接两个城市的快 ...
- 1125 Chain the Ropes (25 分)
1125 Chain the Ropes (25 分) Given some segments of rope, you are supposed to chain them into one rop ...
- 1122 Hamiltonian Cycle (25 分)
1122 Hamiltonian Cycle (25 分) The "Hamilton cycle problem" is to find a simple cycle that ...
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- 1044 Shopping in Mars (25 分)
1044 Shopping in Mars (25 分) Shopping in Mars is quite a different experience. The Mars people pay b ...
- 1012 The Best Rank (25 分)
1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we cons ...
随机推荐
- School Personal Contest #1 (Codeforces Beta Round #38)---A. Army
Army time limit per test 2 seconds memory limit per test 256 megabytes input standard input output s ...
- IDEA Spark Streaming Kafka数据源-Consumer
import org.apache.spark.SparkConf import org.apache.spark.streaming.kafka.KafkaUtils import org.apac ...
- fprintf与stderr、stdout的使用
#include <stdio.h> void main() { fprintf(stderr,"soyo8888!"); fprintf(stdout,"s ...
- 43. ExtJs控件属性配置详细
转自:https://www.cnblogs.com/mannixiang/p/6558225.html 序言: 1.本文摘自网络,看控件命名像是4.0以前的版本,但控件属性配置仍然可以借鉴(不 ...
- Java - HashTable、HashMap和LinkedHashMap的区别
一般情况下,我们用的最多的是HashMap,在Map 中插入.删除和定位元素,HashMap 是最好的选择.但如果您要按自然顺序或自定义顺序遍历键,那么TreeMap会更好.如果需要输出的顺序和输入的 ...
- In 7-bit
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3713 题意:给定一个字符串,首先输出这个字符串的长度(以两位的十六进制的形 ...
- [Swift通天遁地]二、表格表单-(11)创建星期选项表单和拥有浮动标签的文本框
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- Akka源码分析-Remote-Actor创建
在之前的博客中,我们分析过local模式下Actor的创建过程,最终还是调用了provider的actorOf的函数创建了Actor,在remote模式下provider就是RemoteActorRe ...
- Codeforces 771C
我的树形dp果然是渣... 题意:给一棵树,共n(0<n<=15e4)个节点,可在树上进行跳跃,每次跳的最大距离为k(0<k<=5),定义f(s,t)为(dis(s,t)+k) ...
- Zabbix Server参数文件详解
Zabbix的配置文件一般有三种: zabbix_server.conf:zabbix server的配置文件 zabbix_proxy.conf:zabbix proxy的配置文件 zabbix_a ...