杭电ACM-LCY算法进阶培训班-专题训练(Hash及其应用)

  • 杭电ACM-LCY算法进阶培训班-专题训练(Hash及其应用)
    • Ignatius and the Princess II
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • 题意
      • 思路
    • sort
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • 思路
    • 解方程
      • Problem Description
      • Input
      • Output
      • Sample Input
      • Sample Output
      • 思路
      • 优化

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∗x12+b∗x22+c∗x32+d∗x42=0a*x_1^2 + b*x_2^2 + c*x_3^2 + d*x_4^2=0a∗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∗x12+b∗x22=−c∗x32−d∗x42a*x_1^2 + b*x_2^2 = -c*x_3^2 - d*x_4^2a∗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));}
}

优化

因为双重循环最多产生10410^4104个结果,开一个2×1062\times10^62×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方法为循环取余法,解决冲突的方法为线性探测法。
两次提交的结果如下,可以看到优化后效果还是挺明显的。

杭电ACM-LCY算法进阶培训班-专题训练(Hash及其应用)相关推荐

  1. 杭电ACM-LCY算法进阶培训班-专题训练15

    杭电ACM-LCY算法进阶培训班-专题训练(03-07-11-15) 1012 最短路 #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofa ...

  2. 杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)

    杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)[模板] 传送门 杭电ACM-LCY算法进阶培训班-专题训练(矩阵快速幂)[模板] 矩阵快速幂模板 Count Problem Descript ...

  3. 杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门)

    杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门) 传送门 杭电ACM-LCY算法进阶培训班-专题训练(计算几何入门) Shape of HDU Problem Description Inp ...

  4. 杭电ACM-LCY算法进阶培训班-专题训练(KMP)

    杭电ACM-LCY算法进阶培训班-专题训练(KMP) 杭电ACM-LCY算法进阶培训班-专题训练(KMP) 剪花布条 Problem Description Input Output Sample I ...

  5. 2021-06-18杭电ACM-LCY算法进阶培训班-专题训练16

    杭电ACM-LCY算法进阶培训班-专题训练(04-08-12-16) 1009 Intervals #pragma GCC optimize(2) #pragma GCC optimize(3,&qu ...

  6. 杭电ACM-LCY算法进阶培训班-专题训练09

    杭电ACM-LCY算法进阶培训班-专题训练09 1014 剪花布条 #pragma GCC optimize(2) #pragma GCC optimize(3,"Ofast",& ...

  7. 杭电ACM-LCY算法进阶培训班-专题训练(线段树)

    杭电ACM-LCY算法进阶培训班-专题训练(线段树) 传送门 目录 杭电ACM-LCY算法进阶培训班-专题训练(线段树) 张煊的金箍棒(2) Problem Description Input Out ...

  8. 杭电ACM-LCY算法进阶培训班-专题训练(强连通分量)

    点对统计 最大点权 点对统计 Problem Description 给定一个有向图,统计有多少点对u,v(1≤u<v≤n)u,v(1≤u<v≤n)u,v(1≤u<v≤n)满足uuu ...

  9. 【杭电ACM】1097 A hard puzzle

    [杭电ACM]1097  A hard puzzle http://acm.hdu.edu.cn/showproblem.php?pid=1097 先用int手写了算法结果竟然wrong answer ...

  10. 杭电ACM刷题(1):1002,A + B Problem II

    最近忙于考试复习,没有多少可供自己安排的时间,所以我利用复习之余的空闲时间去刷刷杭电acm的题目,也当对自己编程能力的锻炼吧. Problem Description I have a very si ...

最新文章

  1. 我程序中用到的第一个递归算法
  2. 深入解析 C# 的 String.Create 方法
  3. 农商银行招聘计算机人员考什么,农商银行招聘考试题都考什么?
  4. python层次聚类_python实现层次聚类
  5. python创建空元组_Python——元组的基本语法(创建、访问、修改、删除)
  6. keepalived配置高可用集群
  7. c/c++如何获取数组的长度
  8. sendmail发送html邮件,尝试使用sendmail发送/发送html电子邮件,但显示电子邮件的源代码...
  9. python求输入数字的平方、如果平方运算后小于50则退出_Python练习题(三)
  10. Vmware16一打开虚拟机就蓝屏
  11. 什么是python全栈开发_什么是python全栈
  12. img 标签的 src 属性
  13. linux oracle client 安装,Instant Client 下载 适用于 Linux x86-64(64 位)
  14. Sprint 敏捷开发
  15. 使用SDK Manager给TX2刷机且安装OpenCV3.4.0、CUDNN7.6.5、Pytorch、Miniforge(含百度云安装包)
  16. 如何阅读一本书?阅读的高效方法
  17. 【GCN-RS-Defence】GCN-Based User Representation Learning for Unifying Robust Recommendation and Frauds
  18. spring多参java注解_spring参数注解校验
  19. c语言的字符数组strlen的详细使用
  20. 微信小程序地址选择组件(仿拼多多App版地址选择)

热门文章

  1. Laravel文档整理
  2. VTN:视频Transformer网络
  3. OpenCV——证件照自动抠图
  4. 如何免费获取基于公网 IP 的 SSL 证书 (无需域名)
  5. win10桌面不见了如何找到
  6. 爬取全国12个热门城市奶茶店铺情况,看看你的城市哪个品牌最多
  7. 《我奋斗了18年才和你坐在一起喝咖啡》,而我奋斗了18年,不是为了和你一起喝咖啡(转载)...
  8. ISP PIPLINE Denoise 之 space domain denoise 空域降噪
  9. 数据去重-----VBA字典法
  10. SwiftUI 高级用法之ForEach如何使用 enumerated() UserDefaults.standard(教程含源码)