例题:
hdu 1425 sort
排序可能会超时
在输入的过程中排好序,最大复杂度
2
*
1000000
#
include
<cstdio>
#
include
<cstring>
#
include
<algorithm>
using
namespace
std;
int
a[
1000010
];
int
main()
{
int
n,m;
while
(scanf(
"%d%d"
,&n,&m)!=EOF)
{
int
x;
memset(a,
0
,sizeof(a));
int
maxn=
0
;
for
(
int
i=
0
;i<n;i++)
{
scanf(
"%d"
,&x);
x=x+
500000
;
maxn=max(maxn,x);
a[x]=
1
;
}
int
ans=
0
;
for
(
int
i=maxn;i>=
0
;i--)
{
if
(a[i])
{
ans++;
printf(
"%d"
,i-
500000
);
if
(ans!=m) printf(
" "
);
if
(ans==m)
break
;
}
}
printf(
"\n"
);
}
return
0
;
}
hdu 1496 Equations
利用hash表将O(n^3)的复杂度降为2*O(n^2)
#
include
<cstdio>
#
include
<cstring>
#
include
<algorithm>
#
include
<cmath>
using
namespace
std;
int
p[
102
];
int
hash[
2000010
];
int
main()
{
for
(
int
i=
1
;i<=
100
;i++)
p[i]=i*i;
int
a,b,c,d;
while
(scanf(
"%d%d%d%d"
,&a,&b,&c,&d)!=EOF)
{
if
(a<
0
&&b<
0
&&c<
0
&&d<
0
||a>
0
&&b>
0
&&c>
0
&&d>
0
)
{
printf(
"0\n"
);
continue
;
}
memset(hash,
0
,sizeof(hash));
for
(
int
i=
1
;i<=
100
;i++)
{
for
(
int
j=
1
;j<=
100
;j++)
hash[a*p[i]+b*p[j]+
1000000
]++;
}
int
cnt=
0
;
for
(
int
i=
1
;i<=
100
;i++)
{
for
(
int
j=
1
;j<=
100
;j++)
{
cnt+=hash[-(c*p[i]+d*p[j])+
1000000
];
}
}
printf(
"%d\n"
,
16
*cnt);
}
return
0
;
}
2,字符串hash
常见hash处理:
ELF算法模板:
inline int ELFhash(char *key)
{
unsigned long h = 0;
unsigned long g;
while( *key )
{
h =( h<< 4) + *key++;
g = h & 0xf0000000L;
if( g ) h ^= g >> 24;
h &= ~g;
}
return h;
}
其他hash算法见:http://acm.hdu.edu.cn/forum/read.php?tid=7329
例题:
hdu 1800 Flying to the Mars//对着模板写的。。。对这个算法不懂
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 7003
int hash[MAXN];
int count1[MAXN];
using namespace std;
int maxn;
int ELFhash(char *s)
{
unsigned long h=0;
unsigned long g;
while(*s)
{
h=(h<<4)+*s++;
g=h&0xf0000000L;
if(g) h^=g>>24;
h&=~g;
}
return h;
}
void getsolve(char *s)
{
while(*s=='0') s++;
int k=ELFhash(s);
int t=k%MAXN;
while(hash[t]!=k&&hash[t]!=-1)
t=(t+10)%MAXN;
if(hash[t]==-1) count1[t]=1,hash[t]=k;
else if(++count1[t]>maxn) maxn=count1[t];
}
int main()
{
int n;
char s[40];
while(scanf("%d",&n)!=EOF)
{
maxn=1;
memset(hash,-1,sizeof(hash));
memset(count1,0,sizeof(count1));
while(n--)
{
scanf("%s",s);
getsolve(s);
}
printf("%d\n",maxn);
}
return 0;
}