有test4就有test5好像没什么错。
你以为就没有test1,test2,test3吗?
就当做没有吧,可能之后会突然冒出来。
来来来,新鲜的题(误)
1.Math
题目描述
给定 2 个数组 a[] 和 b[] ,他们有相同的长度 n ,你可以任意对 a[] 和 b[] 进行重排列,我们定义函数
请问 x 最大可以取到多少,最小可以取到多少?
输入格式
第一行一个数字 n ,表示数组的长度;
第二行 n 个整数,表示数组 a ;
第三行 n 个整数,表示数组 b 。
输出格式
输出两个整数表示答案。
样例数据
输入
2
10 3
10 9
输出
127 120
备注
【样例说明】
Maximum:10 * 10 + 3 * 9 = 127
Minimum:10 * 3 + 10 * 9 = 120
【数据范围】
对 40% 的输入数据 :n≤10
对 100% 的输入数据 :n≤100000,1≤a[i], b[i]≤100000
这题巨坑。
你能想象用标算得了30分的憋屈吗。。。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
using namespace std;
int n;
int a[100010],b[100010];
long long ans1,ans2;
int main()
{
//freopen("math.in","r",stdin);
//freopen("math.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
cin>>b[i];
sort(b+1,b+n+1);
for(int i=1;i<=n;i++)
{
ans1+=(long long)a[i]*b[i];
ans2+=(long long)a[i]*b[n-i+1]; //就是这个long long,如果不转的话,乘法运算时会用int,直接炸
}
cout<<ans1<<" "<<ans2<<endl;
return 0;
}
2.Tree(祖孙关系)
题目描述
已知一棵 n 个节点的有根树。有 m 个询问。每个询问给出了一对节点的编号 x 和 y ,询问 x 与 y 的祖孙关系。
输入格式
输入第一行包括一个整数 n 表示节点个数。
接下来 n 行每行一对整数对 a 和 b 表示 a 和 b 之间有连边。如果 b 是 -1 ,那么 a 就是树的根。
第 n+2 行是一个整数 m 表示询问个数。
接下来 m 行,每行两个正整数 x 和 y。
输出格式
对于每一个询问,输出 1 如果 x 是 y 的祖先;输出 2 如果 y 是 x 的祖先;否则输出 0 。
样例数据
输入
10
234 -1
12 234
13 234
14 234
15 234
16 234
17 234
18 234
19 234
233 19
5
234 233
233 12
233 13
233 15
233 19
输出
1
0
0
0
2
备注
【样例说明】
234 是 233 的祖先,19 是 233 的祖先。
【数据范围】
对 30% 的输入数据 :n,m≤1000
对 100% 的输入数据 :n,m≤40000,每个节点的编号都不超过 40000 。
一道比较标准的LCA(最近公共祖先)。
啧,怎么就没想到呢,只水了几个点。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
using namespace std;
int n,l,m;
int f[40003][20]; //建树之后,向上走2^j
int first[40003],dis[40003],next[40003],where[40003],c[40003];
inline void ml(int x,int y)
{
where[++l]=y;
next[l]=first[x];
first[x]=l;
}
inline void md(int father)
{
memset(dis,0,sizeof(dis));
dis[father]=1;
c[1]=father;
for(int i=1,l=1;l<=i;l++)
{
int m=c[l];
for(int j=first[m];j;j=next[j])
{
dis[where[j]]=dis[m]+1;
c[++i]=where[j];
}
}
}
inline int zql(int x,int y)
{
if(dis[x]<dis[y])
{
int k=x;
x=y;
y=k;
}
int foot=dis[x]-dis[y];
for(int i=18;i>=0;i--)
if(foot>=(1<<i))
{
foot-=(1<<i);
x=f[x][i];
}
if(x==y)
return x;
for(int i=18;i>=0;i--)
if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
return f[x][0];
}
int main()
{
//freopen("tree.in","r",stdin);
//freopen("tree.out","w",stdout);
memset(f,0,sizeof(f));
memset(first,0,sizeof(first));
cin>>n;
l=0;
int father=0;
for(int i=1;i<=n;i++)
{
int x,y;
cin>>x>>y;
if(y!=-1)
{
f[x][0]=y;
ml(y,x);
}
else
father=x;
}
md(father);
for(int i=1;i<=18;i++)
for(int j=1;j<=40000;j++)
if(f[j][i-1])
f[j][i]=f[f[j][i-1]][i-1];
cin>>m;
for(int i=1;i<=m;i++)
{
int x,y;
cin>>x>>y;
int ans=zql(x,y);
if(x==y)
cout<<"0"<<endl;
else if(ans==x)
cout<<"1"<<endl;
else if(ans==y)
cout<<"2"<<endl;
else
cout<<"0"<<endl;
}
return 0;
}
3.Half
题目描述
给定 n 个数,求最大的数 m ,使得 m 是 n 个数中至少一半的数的约数。
注意:m 不一定在 n 个数中,只要满足要求即可。
输入格式
第一行一个整数 n ,表示数组大小。
第二行 n 个整数,表示数组的 n 个元素。
输出格式
输出一个整数,表示答案。
样例数据
输入
6
6 2 3 4 5 6
输出
3
备注
【样例说明】
3 是 6、3、6 的约数,达到了一半的要求;
【数据范围】
对 40% 的输入数据 : n≤100;
对 100% 的输入数据 :n≤100000;1≤数字的大小≤10^12 。
这次用了容错率极低的思想,然而并没有想到。
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<algorithm>
#include<cctype>
#include<iomanip>
#include<map>
using namespace std;
int n,cnt;
long long a[100010],c[100010],v[100010],f[100010];
map<long long,int>events;
inline long long gcd(long long a,long long b)
{
if(!b)
return a; //辗转相除法求最大公因数
return gcd(b,a%b);
}
inline int zql(int n,long long goal)
{
if(v[n]==goal)
return n;
int l=0,r=n,mid=(l+r)>>1;
for(;l+1<r;mid=(l+r)>>1)
if(v[mid]<goal)
l=mid;
else
r=mid;
return r;
}
int main()
{
//freopen("half.in","r",stdin);
//freopen("half.out","w",stdout);
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
srand(time(0));
random_shuffle(a+1,a+n+1); //随机排序
long long ans=0;
for(int i=1;i<=n&&i<=10;i++) //取十个数几乎无错,错误率1/1024
{
cnt=0;
sort(c+1,c+cnt+1);
int num=0;
for(long long j=1;j<=sqrt(a[i]);j++)
if(a[i]%j==0)
{
v[++num]=j;
f[num]=0;
if(j*j!=a[i])
{
v[++num]=a[i]/j;
f[num]=0;
}
}
sort(v+1,v+num+1);
for(int j=1;j<=n;j++)
++f[zql(num,gcd(a[i],a[j]))];
for(int j=1;j<=num;j++)
{
long long tt=0;
if(v[j]<=ans)
continue;
for(int k=j;k<=num;k++)
if(!(v[k]%v[j]))
tt+=f[k];
if(tt*2>=n)
ans=max(ans,v[j]);
}
}
cout<<ans<<endl;
return 0;
}
来自2017.7.13 test5
——我认为return 0,是一个时代的终结。