ACwing第23场周赛

1.完全平方数

4003. 完全平方数 - AcWing题库

这个题有坑,但是考试的时候我避掉了,嘻嘻。

我们要知道数据范围是−10^6≤a_i≤10^6。

数据是包含负数的,这个需要特殊处理一下的。

Y总代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

int main()
{
    int n;
    cin >> n;

    int res = -1e8;
    while (n -- )
    {
        int x;
        cin >> x;
        if (x < 0)
        {
            res = max(res, x);
            continue;
        }
        int t = sqrt(x);
        if (t * t != x) res = max(res, x);
    }

    cout << res << endl;
    return 0;
}

我的代码:

#include<bits/stdc++.h>
using namespace std;

const int N = 100010;

typedef long long LL;

int n;

int a[N];
bool st[N];

int main()
{
	cin>>n;
	priority_queue<int> heap;
	for(int i =0;i<n;i++)cin>>a[i];
	
	for(int i =0;i<n;i++)
	{
		int t = sqrt(a[i]);
		if(t * t == a[i])st[i] = true;
	}
	
	for(int i =0;i<n;i++)
	{
		if(!st[i])
		{
			heap.push(a[i]);
		}
	}
	
	cout<<heap.top()<<endl;
	
	
	return 0;
} 

2.传送阵

4004. 传送阵 - AcWing题库

这个题在考试的我傻了,算法我已经猜到了,但是奈何我没得思路。

这个题其实就起点和终点都搜一下就好了,然后枚举起点可以到的点和终点可以到的点,取一个在两个点建阵最小的费用就行了。

然后我实现了DFS和BFS两种搜索,Y总直播的时候还说可以用并查集,然后我试了一下并查集,报了一个我看不懂的错误,哈哈哈,我放弃了。

DFS:

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 55;

int n;
char g[N][N];
int dx[4] = {-1,0,1,0} ,dy[4] = {0,1,0,-1};
bool st1[N][N], st2[N][N];

void dfs(int sx,int sy,bool st[][N])
{
	st[sx][sy] = true;
	for(int i =0;i<4;i++)
	{
		int x= sx + dx[i], y = sy + dy[i];
		
		if(x >= 0 && x < n && y >= 0 && y < n && !st[x][y] && g[x][y] == '0')
			dfs(x,y,st);
	} 
}


int main()
{
    cin >> n;
    int sx, sy, tx, ty;
    cin >> sx >> sy >> tx >> ty;
    sx --, sy --, tx --, ty -- ;
    for (int i = 0; i < n; i ++ ) cin >> g[i];
	
    dfs(sx, sy, st1);
    if (st1[tx][ty]) puts("0");
    else
    {
        dfs(tx, ty, st2);
        int res = 1e8;
        
        for(int i =0;i<n;i++)
        	for(int j =0;j<n;j++)
        	{
        		if(st1[i][j])
        		{
        			for(int x =0;x < n;x ++)
        			{
        				for(int y = 0;y < n;y ++)
        				{
        					if(st2[x][y])
        					{
        						res =  min(res,(x-i) * (x-i) + (y-j) * (y-j));
        					}
        				}
        			}
        		}
        	}
        
        cout<<res<<endl;
    }

    return 0;
}

BFS:

#include<bits/stdc++.h>
using namespace std;

const int N = 55;

typedef long long LL;
typedef pair<int,int> PII;

int n;

char g[N][N];
PII q[N * N];

bool st1[N][N],st2[N][N];

int r1,c1,r2,c2;
int res = 1e8;


void bfs(int r,int c,bool st[][N])
{
	int hh = 0,tt = 0;
	memset(q,0,sizeof(q));
	 
	q[0] = {r,c}; 
	st[r][c] = true;
	
	int dx[4] = {-1,0,1,0} , dy[4] = {0,1,0,-1};
	
	while(hh<=tt)
	{
		auto t = q[hh++];
		
		for(int i =0;i<4;i++)
		{
				int x = t.first + dx[i] ,y = t.second + dy[i];
				
				if(x >= 0 && x < n && y >= 0 && y < n && g[x][y] == '0' && !st[x][y])
				{
					st[x][y] = true;
					q[++tt] = {x,y};
				}
		}
	}
} 


int main()
{
	cin>>n;
	cin>>r1>>c1>>r2>>c2;
	r1 --,c1--,r2--,c2--;
	for(int i =0;i<n;i++)cin>>g[i];
	
	bfs(r1,c1,st1);
	
	if(st1[r2][c2])puts("0");
	else
	{
		bfs(r2,c2,st2);
		for(int i =0;i<n;i++)
			for(int j = 0;j<n;j++)
			{
				if(st1[i][j])
				{
					for(int x = 0;x<n;x++)
						for(int y = 0;y<n;y++)
							if(st2[x][y])
							{
								res = min(res,(x - i) * (x - i) + (y - j) * (y - j));
							} 
				} 
			}
		cout<<res<<endl;
	}
	
	return 0;
} 

比较来看,还是DFS好写一些,不用实现队列。

3.取石子游戏

第三题就比较恶心了,我一看就知道是博弈论,然后想了近半个小时,愣是没找到规律啊,难受。

感觉博弈论白学了,考试的时候sg函数在我脑子里一片空白,根本不记得了。

然后我只能给大家说一下具体思路吧:

我们首先要知道两个状态:(必胜态,必败态)简单博弈论_是饿梦啊的博客-CSDN博客_博弈论

然后我们要知道一个就是取的数量是每 k+1 一次循环。

具体的话,我们还是去看Y总的视频讲解吧。

AcWing 4005. 取石子游戏(AcWing杯 - 周赛) - AcWing

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

int main()
{
    int T;
    cin >> T;
    while (T -- )
    {
        int n, k;
        cin >> n >> k;
        if (k % 3)
        {
            if (n % 3) puts("Alice");
            else puts("Bob");
        }
        else
        {
            n %= k + 1;
            if (n == k || n % 3) puts("Alice");
            else puts("Bob");
        }
    }

    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是饿梦啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值