ACwing第23场周赛

1.完全平方数

4003. 完全平方数 - AcWing题库

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

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

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

Y总代码:

#include 
#include 
#include 
#include 

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
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 heap;
	for(int i =0;i>a[i];
	
	for(int i =0;i

2.传送阵

4004. 传送阵 - AcWing题库

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

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

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

DFS:

#include 
#include 
#include 

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

BFS:

#include
using namespace std;

const int N = 55;

typedef long long LL;
typedef pair 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>g[i];
	
	bfs(r1,c1,st1);
	
	if(st1[r2][c2])puts("0");
	else
	{
		bfs(r2,c2,st2);
		for(int i =0;i

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

3.取石子游戏

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

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

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

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

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

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

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

#include 
#include 
#include 

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;
}

你可能感兴趣的:(c++,less,算法)