leetcode每日一题(4.29)+一些杂题

42 篇文章 2 订阅
35 篇文章 3 订阅

leetcode

427. 建立四叉树

给你一个 n * n 矩阵 grid ,矩阵由若干 0 和 1 组成。请你用四叉树表示该矩阵 grid 。
你需要返回能表示矩阵的 四叉树 的根结点。
注意,当 isLeaf 为 False 时,你可以把 True 或者 False 赋值给节点,两种值都会被判题机制 接受 。
四叉树数据结构中,每个内部节点只有四个子节点。此外,每个节点都有两个属性:
val:储存叶子结点所代表的区域的值。1 对应 True,0 对应 False;
isLeaf: 当这个节点是一个叶子结点时为 True,如果它有 4 个子节点则为 False 。
我们可以按以下步骤为二维区域构建四叉树:

如果当前网格的值相同(即,全为 0 或者全为 1),将 isLeaf 设为 True ,将 val 设为网格相应的值,并将四个子节点都设为 Null 然后停止。
如果当前网格的值不同,将 isLeaf 设为 False, 将 val 设为任意值,然后如下图所示,将当前网格划分为四个子网格。
使用适当的子网格递归每个子节点。

在这里插入图片描述

如果你想了解更多关于四叉树的内容,可以参考 wiki 。

四叉树格式:

输出为使用层序遍历后四叉树的序列化形式,其中 null 表示路径终止符,其下面不存在节点。

它与二叉树的序列化非常相似。唯一的区别是节点以列表形式表示 [isLeaf, val] 。

如果 isLeaf 或者 val 的值为 True ,则表示它在列表 [isLeaf, val] 中的值为 1 ;如果 isLeaf 或者 val 的值为 False ,则表示值为 0 。

示例 1:

在这里插入图片描述

输入:grid = [[0,1],[1,0]]
输出:[[0,1],[1,0],[1,1],[1,1],[1,0]]
解释:此示例的解释如下:
请注意,在下面四叉树的图示中,0 表示 false,1 表示 True 。

示例 2:
在这里插入图片描述
在这里插入图片描述

输入:grid = [[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,1,1,1,1],[1,1,1,1,1,1,1,1],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0],[1,1,1,1,0,0,0,0]]
输出:[[0,1],[1,1],[0,1],[1,1],[1,0],null,null,null,null,[1,0],[1,0],[1,1],[1,1]]
解释:网格中的所有值都不相同。我们将网格划分为四个子网格。
topLeft,bottomLeft 和 bottomRight 均具有相同的值。
topRight 具有不同的值,因此我们将其再分为 4 个子网格,这样每个子网格都具有相同的值。
解释如下图所示:

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/construct-quad-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

/*
// Definition for a QuadTree node.
class Node {
public:
    bool val;
    bool isLeaf;
    Node* topLeft;
    Node* topRight;
    Node* bottomLeft;
    Node* bottomRight;
    
    Node() {
        val = false;
        isLeaf = false;
        topLeft = NULL;
        topRight = NULL;
        bottomLeft = NULL;
        bottomRight = NULL;
    }
    
    Node(bool _val, bool _isLeaf) {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = NULL;
        topRight = NULL;
        bottomLeft = NULL;
        bottomRight = NULL;
    }
    
    Node(bool _val, bool _isLeaf, Node* _topLeft, Node* _topRight, Node* _bottomLeft, Node* _bottomRight) {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = _topLeft;
        topRight = _topRight;
        bottomLeft = _bottomLeft;
        bottomRight = _bottomRight;
    }
};
*/

class Solution {
public:
    Node* construct(vector<vector<int>>& grid) {
        int n=grid.size();
        function<Node*(int,int,int,int)>create=[&](int rowleft,int rowright,int colleft,int colright)->Node*{
            bool f=false;
            int mark=grid[rowleft][colleft];
            for(int i=rowleft;i<rowright;i++)
            {
                for(int j=colleft;j<colright;j++)
                {
                    if(mark!=grid[i][j])
                    {
                        f=true;
                        break;
                    }
                }
                if(f)break;
            }
            if(!f)return new Node(mark,true);
            int len=(rowright-rowleft)>>1;
            Node *root=new Node(true,false);
            root->topLeft=create(rowleft,rowleft+len,colleft,colleft+len);
            root->topRight=create(rowleft,rowleft+len,colleft+len,colright);
            root->bottomLeft=create(rowleft+len,rowright,colleft,colleft+len);
            root->bottomRight=create(rowleft+len,rowright,colleft+len,colright);
            return root;
        };
        return create(0,n,0,n);
    }
};

codeforces

A. GCD vs LCM

You are given a positive integer n. You have to find 4 positive integers a,b,c,d such that
a+b+c+d=n, and
gcd(a,b)=lcm(c,d).
If there are several possible answers you can output any of them. It is possible to show that the answer always exists.
In this problem gcd(a,b) denotes the greatest common divisor of a and b, and lcm(c,d) denotes the least common multiple of c and d.
Input
The input consists of multiple test cases. The first line contains a single integer t (1≤t≤104) — the number of test cases. Description of the test cases follows.

Each test case contains a single line with integer n (4≤n≤109) — the sum of a, b, c, and d.
Output
For each test case output 4 positive integers a, b, c, d such that a+b+c+d=n and gcd(a,b)=lcm(c,d).

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
void solve()
{
	cin>>n;
	int a=1,b=n-3,c=1,d=1;
	cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

A. Array Balancing

outputstandard output
You are given two arrays of length n: a1,a2,…,an and b1,b2,…,bn.
You can perform the following operation any number of times:
Choose integer index i (1≤i≤n);
Swap ai and bi.
What is the minimum possible sum |a1−a2|+|a2−a3|+⋯+|an−1−an| + |b1−b2|+|b2−b3|+⋯+|bn−1−bn| (in other words, ∑i=1n−1(|ai−ai+1|+|bi−bi+1|)) you can achieve after performing several (possibly, zero) operations?
Input
The first line contains a single integer t (1≤t≤4000) — the number of test cases. Then, t test cases follow.
The first line of each test case contains the single integer n (2≤n≤25) — the length of arrays a and b.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — the array a.
The third line of each test case contains n integers b1,b2,…,bn (1≤bi≤109) — the array b.
Output
For each test case, print one integer — the minimum possible sum ∑i=1n−1(|ai−ai+1|+|bi−bi+1|).

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
const int N=55;
int a[N],b[N];
void solve()
{
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	for(int i=1;i<=n;i++)cin>>b[i];;
	long long res=0;
	for(int i=1;i<n;i++)
	{
		res+=min(abs(a[i]-a[i+1])+abs(b[i]-b[i+1]),abs(a[i]-b[i+1])+abs(b[i]-a[i+1]));
	}
	cout<<res<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

B. Vlad and Candies

Not so long ago, Vlad had a birthday, for which he was presented with a package of candies. There were n types of candies, there are ai candies of the type i (1≤i≤n).
Vlad decided to eat exactly one candy every time, choosing any of the candies of a type that is currently the most frequent (if there are several such types, he can choose any of them). To get the maximum pleasure from eating, Vlad does not want to eat two candies of the same type in a row.
Help him figure out if he can eat all the candies without eating two identical candies in a row.
Input
The first line of input data contains an integer t (1≤t≤104) — the number of input test cases.
The following is a description of t test cases of input, two lines for each.
The first line of the case contains the single number n (1≤n≤2⋅105) — the number of types of candies in the package.
The second line of the case contains n integers ai (1≤ai≤109) — the number of candies of the type i.
It is guaranteed that the sum of n for all cases does not exceed 2⋅105.
Output
Output t lines, each of which contains the answer to the corresponding test case of input. As an answer, output “YES” if Vlad can eat candy as planned, and “NO” otherwise.
You can output the answer in any case (for example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer).

#include <bits/stdc++.h>
using namespace std;
int _;
int n;
const int N=2e5+10;
int a[N];
void solve()
{
	cin>>n;
	int maxv=0,sec=0;
	for(int i=0;i<n;i++)cin>>a[i];
	bool f=false;
	if(n==1)
	{
		if(a[0]>=2)
		{
			puts("NO");
			f=true;
		}
		else puts("YES");
	}
	else
	{
		for(int i=0;i<n;i++)
		{
			if(a[i]>maxv)
			{
				sec=maxv;
				maxv=a[i];
			}
			else if(a[i]>sec)sec=a[i];
		}
		if(maxv-sec>=2)
		{
			puts("NO");
		}
		else puts("YES");
	}
} 
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}

A. Vasya and Coins

Vasya decided to go to the grocery store. He found in his wallet a coins of 1 burle and b coins of 2 burles. He does not yet know the total cost of all goods, so help him find out s (s>0): the minimum positive integer amount of money he cannot pay without change or pay at all using only his coins.
For example, if a=1 and b=1 (he has one 1-burle coin and one 2-burle coin), then:
he can pay 1 burle without change, paying with one 1-burle coin,
he can pay 2 burle without change, paying with one 2-burle coin,
he can pay 3 burle without change by paying with one 1-burle coin and one 2-burle coin,
he cannot pay 4 burle without change (moreover, he cannot pay this amount at all).
So for a=1 and b=1 the answer is s=4.
Input
The first line of the input contains an integer t (1≤t≤104) — the number of test cases in the test.
The description of each test case consists of one line containing two integers ai and bi (0≤ai,bi≤108) — the number of 1-burle coins and 2-burles coins Vasya has respectively.
Output
For each test case, on a separate line print one integer s (s>0): the minimum positive integer amount of money that Vasya cannot pay without change or pay at all.

#include <bits/stdc++.h>
using namespace std;
int _;
int n,m;
void solve()
{
	int res=0;
	cin>>n>>m;
	if(n==0)res=1;
	else res=2*m+n+1;
	cout<<res<<endl;
}
int main()
{
	cin>>_;
	while(_--)solve();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

leimingzeOuO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值