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 ...
随机推荐
- 深入struts2(二) ---stuts2长处和主要包、类功能
1.1 Struts2 上节已讲.struts2在webwork基础发展起来的mvc框架.MVC框架相信一般码农都比較了解,这里不再重说. 在这里只对于一下struts1,struts2做了哪 ...
- java 多线程——并发编程模型 学习笔记
并发编程模型 ...
- uefi bios安装ubuntu16.04 (win10和ubuntu双系统)
哎呀,没事闲的装双系统,按照晚上的教程装半天也没成功,后来才知道是自己电脑的问题,当然也有那些过时的博客的问题! ultraiso制作ubuntu u盘启动盘 http://www.cr173.co ...
- 【转】Intent传递数据时,可以传递哪些类型数据?
在Android应用的开发中,如果我们需要在不同的模块(比如不同的Activity之间)之间传递数据,通常有以下两种方法:1. 利用Intent对象携带数据通过查询Intent/Bundle的API文 ...
- Dice (HDU 4652)
题面: m 面骰子,求1. 出现n个连续相同的停止 ;2. 出现n个连续不同的停止的期望次数.(n, m ≤ 10^6 ) 解析: 当然要先列式子啦. 用f[i](g[i])表示出现i个连续相同(不相 ...
- 36.面板Ext.Panel使用
转自:https://www.cnblogs.com/linjiqin/archive/2011/06/22/2086620.html 面板Ext.Panel使用 概要 1.Ext.Panel概述 2 ...
- Linux进程状态查询
进程状态详细说明 Linux进程状态详细解析 ps 的参数说明 ps 提供了很多的选项参数,常用的有以下几个: l 长格式输出: u 按用户名和启动时间的顺序来显示进程: ...
- [Apple开发者帐户帮助]八、管理档案(2)创建临时配置文件(iOS,tvOS,watchOS)
创建临时配置文件以在设备上运行您的应用程序而无需Xcode.在开始之前,您需要一个App ID,一个分发证书和多个注册设备. 有关完整的临时配置文件工作流程,请转到Xcode帮助中的分发到已注册设备( ...
- $CF1141A Game 23$
这题很简单啊 可以用\(DFS\)来打 毕竟是 \(2^x*3^y=m 输出x+y啊\) 这是最简单的做法 #include <bits/stdc++.h> using namespace ...
- Map,Filter 和 Reduce
Map会将一个函数映射到一个输入列表的所有元素上 map(function_to_apply, list_of_inputs) items = [1, 2, 3, 4, 5] squared = li ...