有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,是一個時代的終結。