hdoj 哈希

队长给大一的拉了一些题目,自己闲着没事也做做,终于知道自己有多么水了。。。水的没话说啊。。。

不过有一个学妹超厉害,佩服,希望壮大我SYUCT。。。

1.、sort              题目描述

自己看到这个题目就兴奋了,说试着水一下,可是TLE了,知道自己的错误了。。。最后用了哈希。。。。。。。

可是看学妹的代码,人家是用快排写的,竟然过了,我暗暗的佩服qsort了好长时间。。。。。。。。。。

贴上自己抄袭的代码吧。。。。。。。

/* ***********************************************
Author        :Mosu
Created Time  :2015/3/27 18:18:55
File Name     :A.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int a[1000009];
int main()
{
	int n,m,max=-1;
	int temp;
	while(scanf("%d%d",&n,&m)==2)
	{
		int cnt=0;
		memset(a,0,sizeof(a[0])*1000001);
		for(int i=0;i<n;i++)
		{
			scanf("%d",&temp);
			if(temp>=max)
			{
				max=temp;
			}
			a[temp+500000]=1;
		}
		int j=max+500000;
		while(j>=0)
		{
			if(a[j]==1)
			{
				printf("%d",j-500000);
				cnt++;
				if(cnt==m)
				{
					printf("\n");
					break;
				}
				else
				{
					printf(" ");
				}
			}
			j--;
		}
	}
	return 0;
}


2、Equations  点击打开链接

一个简单的哈希题目,可以看成a*aa+b*bb+1000000=1000000-c*cc-d*dd。。。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;


const int Max=1000000;
int h[Max*2+10];


int main()                                                              //a*aa+b*bb+Max=Max-c*cc-d*dd
{
	int a,b,c,d,cnt;
	while(scanf("%d%d%d%d",&a,&b,&c,&d)==4)
	{

		if((a>0&&b>0&&c>0&&d>0)||(a<0&&b<0&&c<0&&d<0))  //最先的判断,否则超时
		{
			cout<<'0'<<endl;
			continue;
		}

		memset(h,0,sizeof(h));

		cnt=0;

		for(int i=1;i<=100;i++)
			for(int j=1;j<=100;j++)
			{
				//ans=a*i*i+b*j*j;
				h[a*i*i + b*j*j + Max]++;
			}
		for(int i=1;i<=100;i++)
			for(int j=1;j<=100;j++)
			{ 
				cnt+=h[Max-c*i*i-d*j*j];
			}


		cnt*=16;             // 对应不同的正负形,于是一共有 2^4 种可能


		printf("%d\n",cnt);
	}
	return 0;
}

3、Flying to the Mars   点击打开链接

题意是最大的问题,题目的意思是n个人,他们有不同的等级,等级高的给等级低的当师傅,但一个人只能有一个师傅和一个徒弟。。。

考虑到这里,我们要想到,对排好序的n个人,{{a1,a2,a3...an},{b1,b2,b3,...bn}..{c1,c2,c3...cn}},最大的需要则是取决于n的大小,即是最大的相等的分法。。。。。。

具体的看代码吧,只用一个简单的map即可完成,自己对于map的学习还是很弱的,这下需要加强了。。。。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int cmp(int a,int b)
{
	return a>b;
}

int main()
{
	int n;
	while(scanf("%d",&n)==1)
	{
		map<int,int>mp;
		int a;
		int Max=-1000000000;
		for(int i=0;i<n;i++)
		{
			scanf("%d",&a);
			mp[a]++;
			if(mp[a]>Max)
			{
				Max=mp[a];
			}
		}
		cout<<Max<<endl;
	}
	return 0;
}

4、A simple problem 点击打开链接


一个直接模拟的题目,其实自己还是不理解为什么要放到hash中,不过自己对于模拟处理的也不好。。。。。。。。。。

多做题吧。。。。。。。。。。

/* ***********************************************
Author        :Mosu
Created Time  :2015/3/27 18:18:55
File Name     :A.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int cmp(int a,int b)
{
	return a>b;
}
int s[5000000];
int main()
{
	int T,n;
	cin>>T;
	while(T--)
	{
		scanf("%d",&n);
		if(n<0)
		{
			printf("-");
			n=-n;
		}
		memset(s,0,(n+100)*sizeof(s[0]));
		int cnt=0;
		int uu=1,dd=n;
		if(n==1)
		{
			printf("1\n");
			continue;
		}
		printf("0.");
		while(uu!=0)
		{
			s[uu]=1;
			uu=uu*10;
			printf("%d",uu/dd);
			uu=uu%dd;
			if(s[uu])
				break;
		}
		printf("\n");
	}
	return 0;
}

好了,就这样了,其他的我也不会,在努力吧。。。。。。。。。。。。。。自己就是弱逼。。。。。。。。。。没办法啊。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值