week16-csp模测4

在这里插入图片描述

思路:

利用大小为10数组来记录0-9是否被标记,利用string存储数字,然后用每个字符-'0’转化为ascII码来标记并计数,每位数比较完后如果标记的个数如果小于K,则符合要求。
注意:这道题要使用ios::sync_with_stdio(false),否则会超时。

#include<iostream>
#include<algorithm>
#include<cstring>	
#include<vector>
#include<cmath>
#include<string>
#include<cstdio>
using namespace std;
int n, k;
int v[10];
int main()
{
	ios::sync_with_stdio(false);
	int ans = 0;
	cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		int cnt = 0;
		string a;
		cin >> a;
		memset(v, 0, sizeof(v));
		for (int j = 0; j < a.size(); j++)
		{
			if (v[(a[j] - '0')] == 0)
			{
				v[(a[j] - '0')] = 1;
				cnt++;
			}
		}
		if (cnt < k)
		{
			ans++;
		}
		
	}
	cout << ans << endl;
	return 0;
}

在这里插入图片描述

思路:

这道题可以暴力搜索,用struct存储每个点,然后按照x从小到大进行排序。然后利用两层循环,相同的点跳过。对于不相同的点,对于每层遍历,都记录(x2-x1)^2 + (y2-y1)^2并记录这层遍历的最大值,并记录半径平方和原点,再对上层遍历找出最小值并记录半径平方和原点,最后输出即可。

#include<iostream>
#include<algorithm>
#include<cstring>	
#include<vector>
#include<cmath>
#include<string>
#include<cstdio>
#include<queue>
#include<stack>
#include<map>
#include<iomanip>

using namespace std;
double minh = 100000000000000;
int n;
int a, b, ans;
double  maxr;
struct node
{
	double x;
	double y;
	node() {}
	node(double x1, double y1)
	{
		x = x1;
		y = y1;
	}
	bool operator <(const node& n1)const
	{
		if (x == n1.x)
			return y < n1.y;
		return x < n1.x;
	}
}no[1000000];
int main()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a >> b;
		no[i]=node(a, b);
	}
	sort(no, no + n);
	for (int i = 0; i < n; i++)
	{
		double maxr2=0;
		for (int j = 0; j < n; j++)
		{
			if (j == i)
				continue;
			else
			{
				double r2 = pow(no[j].x - no[i].x, 2) + pow(no[j].y - no[i].y, 2);
				maxr2 = max(maxr2,r2);
			}
		}
		if (maxr2 < minh)
		{
			minh=maxr2;
			ans = i;
		}

	}
	cout << setiosflags(ios::fixed) << setprecision(2);
	cout << no[ans].x <<" "<< no[ans].y << endl;
	cout << minh <<endl;
	
}

请添加图片描述
请添加图片描述

思路:

二叉搜搜索树(Binary Search Tree),它或者是一棵空树,或者是具有下列性质的二叉树: 若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值; 若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。
dp思想:
c[i][j]表示区间[i,j-1]作为j的左孩子是否合法
d[i][j]表示区间[i+1,j]作为j的右孩子是否合法
设k为区间[l,r]的根,
如果c[l][k]=1且d[k][r]=1,则该状态合法,可以转移;
同时l1且rn则说明存在一颗以K为根的bst。
如果k可以与l-1相连,说明以l-1为根,[l,r]为右孩子的状态合法。
如果k可以与r+1相连,说明以r+1为根,[l,r]为左孩子的状态合法。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<iomanip>
#include<cstdio>
using namespace std;
const int maxn = 1000;
int gcd(int a, int b) 
{
	return b == 0 ? a : gcd(b, a % b);
}
int T, n;
int a[maxn];
int b[maxn][maxn], c[maxn][maxn], d[maxn][maxn], e[maxn][maxn];
int main()
{
	ios::sync_with_stdio(false);
	cin >> T;
	for (int k = 0; k < T; k++)
	{
		cin >> n;
		memset(b, 0, sizeof(b));
		memset(d, 0, sizeof(d));
		memset(e, 0, sizeof(e));
		for (int i = 1; i <= n; i++)
		{
			cin >> a[i];
			b[i][i] = 1;
			d[i][i] = e[i][i] = 1;
		}
		for (int i = 1; i <= n; i++)
		{
			for (int j = i + 1; j <= n; j++)
			{
				c[i][j] = gcd(a[i], a[j]) > 1;
			}
		}
		for (int i = 1; i <= n; i++)
		{
			for (int l = 1, r = l + i - 1; r <= n; l++, r++)
			{
				for (int j = l; j <= r; j++)
				{
					if (d[l][j] && e[j][r])
					{
						b[l][r] = 1;
						if (c[l - 1][j])
							d[l - 1][r] = 1;
						if (c[j][r + 1])
							e[l][r + 1] = 1;
					}
				}
			}
		}
		if (b[1][n])
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值