**
TT数鸭子
**
样例输入
6 5
123456789 9876543210 233 666 1 114514
样例输出
4
解题思路
每个鸭子对应的映射值输入以字符串输入,遍历字符串,利用vis数组标记计算字符串中不同字符的个数,与k进行比较。
原本我的思路是直接利用set的自动去重,把字符串中的字符存入set,就将set的size与k进行比较即可,但是这样一直报Time limit。
C++代码
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main()
{
int n=0,k=0;
scanf("%d %d",&n,&k);
int num=0;
for(int i=0;i<n;i++)
{
int vis[10]={0};
char str[20];
scanf("%s",str);
int count=0;
for(int j=0;j<strlen(str);j++)
{
if(vis[str[j]-'0']==0)
{
vis[str[j]-'0']=1;
count++;
}
}
if(count<k) num++;
}
printf("%d",num);
return 0;
}
**
ZJM要抵御宇宙射线
**
样例输入
5
0 0
0 1
1 0
0 -1
-1 0
样例输出
0.00 0.00
1.00
解题思路
定义结构体,存放点的坐标和到其他点距离的最小值。需要遍历所有的点计算两点之间的距离,将一点到其他各点之间的距离最小值的平方存入结构体中。将数组按照r的值从小到大排序,保留两位小数输出数组第一个元素的坐标和r。
注意是半径的平方,我直接求得距离,惨遭爆零。
C++代码
#include<iostream>
#include<cmath>
#include<algorithm>
#include<iomanip>
using namespace std;
const int MAX=1e3+1;
struct point
{
double x,y;
double r;
bool operator <(const point &t)const
{
if(r!=t.r) return r<t.r;
else if(x!=t.x) return x<t.x;
else return y<t.y;
}
};
point p[MAX];
int main()
{
int n=0;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>p[i].x>>p[i].y;
p[i].r=0;
}
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
double dis=pow(p[i].x-p[j].x,2)+pow(p[i].y-p[j].y,2);
p[i].r=max(p[i].r,dis);
p[j].r=max(p[j].r,dis);
}
}
sort(p,p+n);
cout<<fixed<<setprecision(2)<<p[0].x<<" "<<p[0].y<<endl;
cout<<p[0].r<<endl;
return 0;
}
**
宇宙狗的危机
**
无行末空格
样例输入1
1
6
3 6 9 18 36 108
样例输出1
Yes
样例输入2
2
2
7 17
9
4 8 10 12 15 18 33 44 81
样例输出2
No
Yes
解题思路
r[root+1][n]=1表示区间[root+1,n]区间内的点可以构建一颗二叉搜索数满足条件并可以作为a[root]的右子树;l[n][root-1]=1表示区间[n,root-1]区间内的点可以构建一颗二叉搜索数满足条件并可以作为a[root]的左子树。遍历每一个根root,如果左侧区间[l,root-1]和右侧区间[root+1,r]都可以构建满足条件的左子树和右子树,那么[l,r]区间上的点就可以构建一颗满足条件的子树,再判断这个区间左右两侧元素和根之间是否满足最大公约数超过1,如果满足,那么这个区间就可以作为该端点的左子树或右子树,继续进行判断。最后如果存在一个根root满足l[0][root-1]=1且r[root+1][n-1],那么就可以构造出来这样的树,否则不能。
C++代码
#include<iostream>
#include<cstring>
using namespace std;
const int MAX=710;
int judge[MAX][MAX];
int a[MAX];
int l[MAX][MAX];
int r[MAX][MAX];
int gcd(int a,int b){return b == 0 ? a : gcd(b,a%b);}
int main()
{
int t=0;
cin>>t;
for(int T=0;T<t;T++)
{
memset(judge,0,sizeof(judge));
memset(a,0,sizeof(a));
memset(l,0,sizeof(l));
memset(r,0,sizeof(r));
int n=0;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i];
for(int i=0;i<n-1;i++)
{
for(int j=i+1;j<n;j++)
{
if(gcd(a[i],a[j])>1)
{
judge[i][j]=1;
judge[j][i]=1;
}
}
}
for(int i=0;i<n-1;i++)
{
if(judge[i][i+1])
{
l[i][i]=1;
r[i+1][i+1]=1;
}
}
for(int k=0;k<n;k++)
{
for(int i=0;i<n-k;i++)
{
int j=i+k;
for(int root=i;root<=j;root++)
{
if(root==i)
{
if(r[root+1][j]&&judge[i-1][root]) r[i][j]=1;
if(r[root+1][j]&&judge[root][j+1]) l[i][j]=1;
}
else if(root==j)
{
if(l[i][root-1]&&judge[i-1][root]) r[i][j]=1;
if(l[i][root-1]&&judge[root][j+1]) l[i][j]=1;
}
else
{
if(l[i][root-1]&&r[root+1][j])
{
if(judge[i-1][root]) r[i][j]=1;
if(judge[root][j+1]) l[i][j]=1;
}
}
}
}
}
int judge1=0;
for(int i=0;i<n;i++)
{
if((i==0&&r[i+1][n-1])||(i==n-1&&l[0][i-1])) judge1=1;
else if(l[0][i-1]&&r[i+1][n-1]) judge1=1;
}
if(judge1==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}