PAT Advanced Level 1145

愿明天的题难一点 把我难倒 然后寒假我就可以刷三遍甲级了 这样榜上就会有权顺荣一刷 权顺荣二刷 权顺荣三刷 还有权顺荣

真是棒棒哒!!!深圳地铁prince,你听到了吗???(PS:犯什么呆萌?还不滚去刷题?)

1145 Hashing - Average Search Time(25 分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash table first. Then try to find another sequence of integer keys from the table and output the average search time (the number of comparisons made to find whether or not the key is in the table). The hash function is defined to be H(key)=key%TSize where TSize is the maximum size of the hash table. Quadratic probing (with positive increments only) is used to solve the collisions.

Note that the table size is better to be prime. If the maximum size given by the user is not prime, you must re-define the table size to be the smallest prime number which is larger than the size given by the user.

Input Specification:

Each input file contains one test case. For each case, the first line contains 3 positive numbers: MSize, N, and M, which are the user-defined table size, the number of input numbers, and the number of keys to be found, respectively. All the three numbers are no more than 10?4??. Then N distinct positive integers are given in the next line, followed by M positive integer keys in the next line. All the numbers in a line are separated by a space and are no more than 10?5??.

Output Specification:

For each test case, in case it is impossible to insert some number, print in a line X cannot be inserted. where X is the input number. Finally print in a line the average search time for all the M keys, accurate up to 1 decimal place.

Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8
/******************************
author:yomi
date:18.9.7
ps: 最让我伤心的当属这题 考试之前特意复习了一遍平方探测法 结果虽然过了样例 但是一分都没得 多亏了这题 70分结束战斗 、不然考95多不好意思啊
我猜想一定是我理解错了题意  T^T 给我四分我也不至于如此伤心啊 哇哇哇哇
通过运行柳神的Ac代码 分析结果 我就发现了 我逻辑错误啊 基本的哈希没问题吧是求平均查找时间那里出了问题
我完全跳进了自己给自己挖的坑 该 真该
这个平均查找时间 就是长查找时间 而我则贪图方便 在前面进行哈希的时候就计算好了 但是 首先我搞错插不进表的数据的比较次数 自然也搞错了表中没有
的数据的查找次数 其他表中能找到的数据的比较次数是没有问题的。所以只需做一点点调整 但是错了就是错了 25分就是一分没得。
想想70分都已经超过我的水平了吧 我觉得我是40分的水平 明天2200+考生在必定是一场恶战 我估计我能考20分 [滑稽脸]

******************************/
/**  柳婼的代码 ****/
#include <iostream>
#include <vector>
using namespace std;
bool isprime(int n) {
    for (int i = 2; i * i <= n; i++)
        if (n % i == 0) return false;
    return true;
}
int main() {
    int tsize, n, m, a;
    scanf("%d %d %d", &tsize, &n, &m);
    while(!isprime(tsize)) tsize++;
    vector<int> v(tsize);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a);
        int flag = 0;
        for (int j = 0; j < tsize; j++) {
            int pos = (a + j * j) % tsize;
            if (v[pos] == 0) {
                v[pos] = a;
                flag = 1;
                break;
            }
        }
        if (!flag) printf("%d cannot be inserted.\n", a);
    }
    int ans = 0;
    for (int i = 0; i < m; i++) {
        scanf("%d", &a);
        for (int j = 0; j <= tsize; j++) {
            ans++;
            int pos = (a + j * j) % tsize;
            if (v[pos] == a || v[pos] == 0) break;
        }

    }
    printf("%.lf\n", ans * 1.0 / m);
    return 0;
}

/**   按照正确思路改了不到五行 22分 弃了弃了 去看简洁的代码喽  ***/
//#include<iostream>
//#include <cmath>
//#include <cstdio>
//#include <cstring>
//using namespace std;
//const int maxn = 100010;
//bool isPrime(int n)
//{
//    if(n == 1){
//        return false;
//    }
//    if(n == 2){
//        return true;
//    }
//    for(int i=2; i<=sqrt(n); i++){
//        if(n%i == 0){
//            return false;
//        }
//    }
//    return true;
//}
//int hash_table[maxn], tim[maxn];
//int Msize;
//int Hash(int n)
//{
//    int t = 0;
//    if(hash_table[n%Msize] == 0){
//        hash_table[n%Msize] = n;
//        t++;
//    }
//    else{
//        int step = 1, flag = 0;
//        for(int i=0; i<Msize*2; i++){
//            int pos = (n+step*step)%Msize;
//            if(hash_table[pos] == 0){
//                hash_table[pos] = n;
//                flag = 1;
//                break;
//            }
//            step++;
//        }
//        if(flag == 0){
//            t = Msize+1;
//            cout << n << " cannot be inserted." << endl;
//        }
//        else{
//            t = step+1;
//        }
//    }
//
//    return t;
//}
//int main()
//{
//    int n, m;
//    memset(tim, 0, sizeof(tim));
//    memset(hash_table, 0, sizeof(hash_table));
//    cin >> Msize >> n >> m;
//    if(!isPrime(Msize)){
//        for(int i=Msize+1; i<=maxn; i++){
//            if(isPrime(i)){
//                Msize = i;
//                break;
//            }
//        }
//    }
//    int d, q;
//    for(int i=0; i<n; i++){
//        cin >> d;
//        tim[d] = Hash(d);
//    }
//    double ans = 0;
//    for(int i=0; i<m; i++){
//        cin >> q;
//        if(tim[q]!=0)
//            ans += tim[q]*1.0;
//        else{
//            tim[q] = Hash(q);
//            ans += tim[q];
//
//        }
//        //cout << ans << endl;
//    }
//    printf("%.1f\n", ans/m);
//    return 0;
//}
/**
Sample Input:

4 5 4
10 6 4 15 11
11 4 15 2

Sample Output:

15 cannot be inserted.
2.8

**/

原文地址:https://www.cnblogs.com/AbsolutelyPerfect/p/9606495.html

时间: 2024-10-04 06:08:32

PAT Advanced Level 1145的相关文章

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c

Pat(Advanced Level)Practice--1060(Are They Equal)

Pat1060代码 题目描述: If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123*105 with simple chopping. Now given the number of significant digits on a machine and two flo