当前位置: 首页 > 工具软件 > Status HUD > 使用案例 >

HUD 2648 解题报告

谢哲瀚
2023-12-01

E - 1005
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Every girl likes shopping,so does dandelion.Now she finds the shop is increasing the price every day because the Spring Festival is coming .She is fond of a shop which is called "memory". Now she wants to know the rank of this shop's price after the change of everyday.
 

Input

One line contians a number n ( n<=10000),stands for the number of shops. 
Then n lines ,each line contains a string (the length is short than 31 and only contains lowercase letters and capital letters.)stands for the name of the shop. 
Then a line contians a number m (1<=m<=50),stands for the days . 
Then m parts , every parts contians n lines , each line contians a number s and a string p ,stands for this day ,the shop p 's price has increased s. 
 

Output

Contains m lines ,In the ith line print a number of the shop "memory" 's rank after the ith day. We define the rank as :If there are t shops' price is higher than the "memory" , than its rank is t+1.
 

Sample Input

3 memory kfc wind 2 49 memory 49 kfc 48 wind 80 kfc 85 wind 83 memory
 

Sample Output

1 2

HDU 2648

本题也可以用map做,但是会显示TL,先发一遍map的代码

#include<bits/stdc++.h>
using namespace std;
map<string,int> a;
int node[10005];
int main(){
    int n,k;
    while(~scanf("%d",&n)){
        memset(node,0,sizeof(node));
        a.clear();
        for(int i = 1; i <= n; i++){
            string name;
            cin >> name;
            if(name == "memory")
                k = i;
            a[name] = i;
            node[i] = 0;
        }
        int m;
        cin >> m;
        while(m--){
            int nn = n;
            while(nn--){
                double price;
                string name;
                cin >> price >> name;
                node[a[name]] += price;
            }
            int ran = 1;
            for(int i = 1; i <= n; i++){
                if(node[i] > node[k])
                    ran++;
            }
            cout << ran << endl;
        }
    }
    return 0;
}

下面是哈希的代码,貌似用了好长的时间。。。




#include <iostream>
#include <malloc.h>
#include <cstring>
#include <cstdio>
using namespace std;
#define szHASH 50000  /*HASH表总长度*/
#define HASH_ROOT 50007 /*用于计算哈希地址的随机数*/

struct THash
{
	int key;  /*钥匙码*/
	char shop_name[35];
	int depth;
};

int ran,hhh;
int elem[szHASH];



/*HASH地址查找*/
int GetHashAddress(int key,int root)
{
	return key%root;
}

/*冲突地址计算,如果发现地址冲突,则用当前地址和钥匙码、哈希根重新生成一个新地址*/
/*int GetConflictAddress(int key, int address, int root)
{
	int addr=address+key%5+1;
	return addr%root;
}*/

/*查找key*/
unsigned int ELFHash(char* str)
{
	unsigned int hash = 0;
	unsigned int x = 0;
	while(*str)
	{
		hash = (hash << 4) + (*str++);
		if((x = hash & 0xF0000000L) != 0)
		{
			hash ^= (x >> 24);
			hash &= ~x;
		}
	}
	return hash & 0x7FFFFFFF;
}

/*根据商店数、长度和哈希根构造哈希表*/
struct THash * CreateNames(int size, int root, int number)
{
	int i =0, key = 0, addr = 0, depth = 0; char name[35];
	struct THash * h = 0, *hash = 0;

	/*哈希根和长度不能太小*/
//	if(size < root || root < 2) return 0;

	/*根据哈希表长度构造一个空的哈希表*/
	hash = (struct THash *)malloc(sizeof(struct THash) * size);

	/*将整个表清空*/
	memset(hash, 0, sizeof(struct THash) * size);

	for(i = 0; i < number; i++)
	{
		/*首先产生一个随机的店名,并根据店名计算哈希钥匙码,再根据钥匙码计算地址*/
		scanf("%s",name);
		key=ELFHash(name);
		addr=GetHashAddress(key,root);
		h = hash + addr;
		if (h->depth == 0)
		{ /*如果当前哈希地址没有被占用,则存入数据*/
			h->key = key;
			strcpy(h->shop_name,name);
			h->depth ++;
			continue;
		}
		/*如果哈希地址已经被占用了,就是说有冲突,则寻找一个新地址,直到没有被占用*/
		depth = 0;
		while(h->depth)
		{
			addr = (addr+1)%HASH_ROOT;
			h = hash + addr;
			depth ++;
		}
		/*按照新地址存放数据,同时记录检索深度*/
		h->key = key;
		strcpy(h->shop_name , name);
		h->depth = depth + 1;
	}
	return hash;
}

/*在哈希表中以特定哈希根查找一个商店的记录*/
struct THash * Lookup(struct THash * hash, char * name, int root)
{
	int key = 0, addr = 0;
	struct THash * h = 0;
	/*不接受空表和空名称*/
	if(!name || !hash) return 0;
	key = ELFHash(name);
	addr = GetHashAddress(key, root);
	h = hash + addr;
	/*如果结果不正确表示按照冲突规则继续寻找*/
	while(strcmp(h->shop_name , name))
	{
		addr = (addr+1)%HASH_ROOT;
		h = hash + addr;
		if(h->key == 0) return 0;
	}
	return hash + addr;
}



int main()
{
	struct THash *hash=0,*h=0;
	int n,m,i,val;
	char name[35];
	while(scanf("%d",&n)!=EOF)
	{
		memset(elem,0,sizeof(elem));
		hash=CreateNames(szHASH, HASH_ROOT,n);
		h=Lookup(hash,"memory",HASH_ROOT);
		hhh=h-hash;
		scanf("%d",&m);
		while(m--)
		{
			ran=0;
			for(i=0;i<n;i++)
			{
				scanf("%d%s",&val,name);
				h=Lookup(hash,name,HASH_ROOT);
				elem[h-hash]+=val;
			}
			for(i=0;i<szHASH;i++)
			{
				if(elem[i]>elem[hhh])
					ran++;
			}
			printf("%d\n",ran+1);
		}
	}
	return 0;
}


 类似资料: