杭电ACM-LCY算法进阶培训班-专题训练(Hash及其应用)
Ignatius and the Princess II
Problem Description
Now our hero finds the door to the BEelzebub feng5166. He opens the door and finds feng5166 is about to kill our pretty Princess. But now the BEelzebub has to beat our hero first. feng5166 says, “I have three question for you, if you can work them out, I will release the Princess, or you will be my dinner, too.” Ignatius says confidently, “OK, at last, I will save the Princess.”
“Now I will show you the first problem.” feng5166 says, “Given a sequence of number 1 to N, we define that 1,2,3…N-1,N is the smallest sequence among all the sequence which can be composed with number 1 to N(each number can be and should be use only once in this problem). So it’s easy to see the second smallest sequence is 1,2,3…N,N-1. Now I will give you two numbers, N and M. You should tell me the Mth smallest sequence which is composed with number 1 to N. It’s easy, isn’t is? Hahahahaha…”
Can you help Ignatius to solve this problem?
Input
The input contains several test cases. Each test case consists of two numbers, N and M(1<=N<=1000, 1<=M<=10000). You may assume that there is always a sequence satisfied the BEelzebub’s demand. The input is terminated by the end of file.
Output
For each test case, you only have to output the sequence satisfied the BEelzebub’s demand. When output a sequence, you should print a space between two numbers, but do not output any spaces after the last number.
Sample Input
6 4
11 8
Sample Output
1 2 3 5 6 4
1 2 3 4 5 6 7 9 8 11 10
题意
输入n与m,求1、2、3…n这个序列的第m小排列。
思路
上次做这题用了C++STL中的next_permutation。这个问题还可以用逆康托展开来解决。
#include<cstdio>
#include<vector>
using namespace std;
int f[1005]={1},n,m,ans[1005],tot;
vector<int> vec;
void init(){
for(int i=1;i<=1000;i++) //预处理阶乘
f[i]=f[i-1]>10000?f[i-1]:i*f[i-1];
}
int main(){
init();
while(~scanf("%d%d",&n,&m)){
vec.clear(); tot=0; m--;
for(int i=0;i<n;i++) vec.push_back(i+1);
for(int i=1;i<=n;i++){
int t=m/f[n-i];
m%=f[n-i];
ans[tot++]=vec[t];
vec.erase(vec.begin()+t);
}
for(int i=0;i<tot;i++) printf("%d%c",ans[i],i==tot-1?'\n':' ');
}
}
sort
Problem Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3
3 -35 92 213 -644
Sample Output
213 92 3
思路
这题时间限制为750ms,应该是要卡快速排序。因为数据值的范围在[-500000,500000]这个不算太大的范围,直接按值来查找就可以了。
#include<cstdio>
#include<cstring>
#define H(x) (x+P)
#define R(x) (x-P)
using namespace std;
const int maxn=1e6+5;
const int P=maxn/2;
int n,m,a[maxn],b;
int main(){
while(~scanf("%d%d",&n,&m)){
memset(a,0,sizeof(a));
for(int i=0;i<n;i++) scanf("%d",&b),a[H(b)]=1;
for(int i=maxn-1;i>=0&&m;i--)
if(a[i]) printf("%d%c",R(i),m==1?'\n':' '),m--;
}
}
解方程
Problem Description
给定一方程如下:
a
∗
x
1
2
+
b
∗
x
2
2
+
c
∗
x
3
2
+
d
∗
x
4
2
=
0
a*x_1^2 + b*x_2^2 + c*x_3^2 + d*x_4^2=0
a∗x12+b∗x22+c∗x32+d∗x42=0
其中:
a, b, c, d在整数区间[-50,50]内取值,并且都不等于0.
求方程在区间[-100,100] 内的非零整数解的个数。
Input
输入包含多组测试数据。
每组数据占一行,包含4个整数a b c d。
Output
请输出每组数据方程解的个数。
Sample Input
1 2 3 -4
1 1 1 1
Sample Output
39088
0
思路
每个x可以取200个不同的数字,直接四重循环暴力肯定不行的。折半枚举,将式子改写为 a ∗ x 1 2 + b ∗ x 2 2 = − c ∗ x 3 2 − d ∗ x 4 2 a*x_1^2 + b*x_2^2 = -c*x_3^2 - d*x_4^2 a∗x12+b∗x22=−c∗x32−d∗x42,双重循环记录一下等号左边的值,再双重循环累加答案即可。
#include<cstdio>
#include<cstring>
#define H(x) (x+P)
using namespace std;
const int maxn=2e6+5,P=maxn/2;
int h[maxn],a,b,c,d,f[105];
int main(){
for(int i=1;i<=100;i++) f[i]=i*i;
while(~scanf("%d%d%d%d",&a,&b,&c,&d)){
int ans=0;
if(a<0&&b<0&&c<0&&d<0||a>0&&b>0&&c>0&&d>0){
printf("0\n"); continue;
}
for(int i=1;i<=100;i++)
for(int j=1;j<=100;j++)
h[H(a*f[i]+b*f[j])]++;
for(int i=1;i<=100;i++)
for(int j=1;j<=100;j++)
ans+=h[H(-c*f[i]-d*f[j])];
printf("%d\n",16*ans);
memset(h,0,sizeof(h));
}
}
优化
因为双重循环最多产生 1 0 4 10^4 104个结果,开一个 2 × 1 0 6 2\times10^6 2×106的数组,在清空数组的时候会花费很多额外的时间。可以开个小数组,换个Hash方法,加上冲突处理,这样更加高效。
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=5e4+21;
int f[maxn],g[maxn],a,b,c,d;
int H(int k){
int t=k%maxn;
if(t<0) t+=maxn;
while(f[t]!=0&&g[t]!=k) t=(t+1)%maxn;
return t;
}
int main(){
int t[105];
for(int i=1;i<=100;i++) t[i]=i*i;
while(~scanf("%d%d%d%d",&a,&b,&c,&d)){
if(a<0&&b<0&&c<0&&d<0||a>0&&b>0&&c>0&&d>0){
printf("0\n"); continue;
}
memset(g,0,sizeof(g));
memset(f,0,sizeof(f));
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int s=a*t[i]+b*t[j],p=H(s);
g[p]=s,f[p]++;
}
}
int ans=0;
for(int i=1;i<=100;i++){
for(int j=1;j<=100;j++){
int s=-c*t[i]-d*t[j],p=H(s);
ans+=f[p];
}
}
printf("%d\n",ans<<4);
}
}
这里的Hash方法为循环取余法,解决冲突的方法为线性探测法。
两次提交的结果如下,可以看到优化后效果还是挺明显的。