首页 > 数据结构 > Hash表 > hdu 2648 Shopping-Hash表-[解题报告]c-sharp
2014
02-12

hdu 2648 Shopping-Hash表-[解题报告]c-sharp

Shopping

问题描述 :

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.

输入:

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.

输出:

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.

样例输入:

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

样例输出:

1
2

#include <iostream>
#include <map>
#include <string>
using namespace std;
#define N 10005

map<string,int> elem;
int node[N];

int main()
{
	int n,m,i,val,j,rank;
	string str;
	while(cin>>n)
	{
		int n1=n;
		for(i=1;i<=n1;i++)
		{
			cin>>str;
			if(str=="memory")
			{
				str[0]='0';
				elem[str]=0;
				i--;
				n1--;
			}
			else
				elem[str]=i;
		}
		memset(node,0,sizeof(node));
		scanf("%d",&m);
		while(m--)
		{
			for(i=1;i<=n;i++)
			{
				cin>>val>>str;
				if(str=="memory")
					str[0]='0';
				node[elem[str]]+=val;
			}
			int cnt=0;
			for(i=1;i<n;i++)
			{
				if(node[i]>node[0])
					cnt++;
			}
			printf("%d/n",cnt+1);
		}
	}
	return 0;
}

解题转自:http://blog.csdn.net/xiaotaoqibao/article/details/5765669


  1. 一开始就规定不相邻节点颜色相同,可能得不到最优解。我想个类似的算法,也不确定是否总能得到最优解:先着一个点,随机挑一个相邻点,着第二色,继续随机选一个点,但必须至少有一个边和已着点相邻,着上不同色,当然尽量不增加新色,直到完成。我还找不到反例验证他的错误。。希望LZ也帮想想, 有想法欢迎来邮件。谢谢

  2. 漂亮。佩服。
    P.S. unsigned 应该去掉。换行符是n 不是/n
    还可以稍微优化一下,
    int main() {
    int m,n,ai,aj,bi,bj,ak,bk;
    while (scanf("%d%d",&m,&n)!=EOF) {
    ai = sqrt(m-1);
    bi = sqrt(n-1);
    aj = (m-ai*ai-1)>>1;
    bj = (n-bi*bi-1)>>1;
    ak = ((ai+1)*(ai+1)-m)>>1;
    bk = ((bi+1)*(bi+1)-n)>>1;
    printf("%dn",abs(ai-bi)+abs(aj-bj)+abs(ak-bk));
    }
    }

  3. 第二种想法,我想来好久,为啥需要一个newhead,发现是把最后一个节点一直返回到嘴上面这层函数。厉害,这道题之前没样子想过。

  4. 约瑟夫也用说这么长……很成熟的一个问题了,分治的方法解起来o(n)就可以了,有兴趣可以看看具体数学的第一章,关于约瑟夫问题推导出了一系列的结论,很漂亮