Day01 算法problem A and B

A-Keanu Reeves

问题描述

After playing Neo in the legendary “Matrix” trilogy, Keanu Reeves started doubting himself: maybe we really live in virtual reality? To find if this is true, he needs to solve the following problem.

Let’s call a string consisting of only zeroes and ones good if it contains different numbers of zeroes and ones. For example, 1, 101, 0000 are good, while 01, 1001, and 111000 are not good.

We are given a string s of length n consisting of only zeroes and ones. We need to cut s into minimal possible number of substrings s1,s2,…,sks1,s2,…,sk such that all of them are good. More formally, we have to find minimal by number of strings sequence of good strings s1,s2,…,sks1,s2,…,sk such that their concatenation (joining) equals s, i.e. s1+s2+⋯+sk=ss1+s2+⋯+sk=s.

For example, cuttings 110010 into 110 and 010 or into 11 and 0010 are valid, as 110, 010, 11, 0010 are all good, and we can’t cut 110010 to the smaller number of substrings as 110010 isn’t good itself. At the same time, cutting of 110010 into 1100 and 10 isn’t valid as both strings aren’t good. Also, cutting of 110010 into 1, 1, 0010 isn’t valid, as it isn’t minimal, even though all 33 strings are good.

Can you help Keanu? We can show that the solution always exists. If there are multiple optimal answers, print any.

Input

The first line of the input contains a single integer nn (1≤n≤1001≤n≤100) — the length of the string ss.
The second line contains the string ss of length nn consisting only from zeros and ones.

Output

In the first line, output a single integer kk (1≤k1≤k) — a minimal number of strings you have cut ss into.
In the second line, output kk strings s1,s2,…,sks1,s2,…,sk separated with spaces. The length of each string has to be positive. Their concatenation has to be equal to ss and all of them have to be good.

If there are multiple answers, print any.

Input   1
		1
output  1
		1
	
Input   2
		10
output  2
		1 | 0


Input   6
		100011
output  2
		100 | 011

这道题的思路:

首先是定义一个长度为n的字符串且仅包含数字0或1,如果0或1的个数相等的话,则要对其进行分割,分割为两个0和1数目不相等的子串,且尽可能少的分配子串,我们这里可以把分割字串一的长度为1,则剩下的字串长度为n-1,且这样保证了0或1在任意情况下都不会相等。

#include<iostream>
#include<string>
using namespace std;
int main()
	{
	int n;
	cin >> n;
	string str;
	cin >> str;
	int a = 0, b = 0;
	for (int i = 0; i < str.size(); i++)
	{
		if (str[i] == '0')
		{
			a += 1;
		}
		else
			b += 1;

	}
	if (a != b)
	{
		cout << "1" << endl;
		cout << str << endl;


	}
	else{
	
		cout << "2" << endl;
		for (int i = 0; i < 1; i++)
		{
			cout << str[i];//保证格式先输入第一个然后再分割开来

		}
		cout << " ";
		for (int i = 1; i < str.size(); i++)
		{
			cout << str[i];


		}


		cout << endl;
	}


	return 0;

}


  • 运行结果如图所示:
    在这里插入图片描述

B-Number Circle

You are given nn numbers a1,a2,…,ana1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?

For example, for the array [1,4,5,6,7,8][1,4,5,6,7,8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+15≥4+1 and 8>1+68>1+6.
在这里插入图片描述

Input

The first line contains a single integer nn(3≤n≤10^5) — the number of numbers.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).

Output

If there is no solution, output “NO” in the first line.
If there is a solution, output “YES” in the first line. In the second line output n numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.

Input   3
		2 4 3
output  YES
       	4 2 3
	
Input   5
		1 2 3 4 4 
output  YES
		4 4 2 1 3 


Input   3
		13 8 5
output  
		NO

问题描述:

第一行输入一个整数n(为小于1e5的整数),表示第二行有多少个数据,如果每个数据比其相邻的两个数据的和小则满足题目条件,反之则不满足,输出任何一个满足条件的数组。

解题思路:

对于这道题我们应寻找它的最优解,就是在什么情况下都能满足的数组序列,我们先对输入的无序数组进行从小到大的排列,排序后如果数组的最后一个元素小于倒数第一个元素和倒数第二个元素相加,则必满足题述条件,这里可以定义两个数组(1是存储无序数组,2是存储有序数组)则可以依次将1数组的最大的元素放入2数组的首位置再将第二大的放在末位置,以此类推。

  • 例如:1 2 3 4
  • 存放顺序为:4 2 1 3

- 代码如下所示:

#include<iostream>
#include<algorithm>
using namespace std;
int a[1000010];
int b[1000010];
int main()
{
	int x = 0;
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin >> a[i];


	}
	sort(a, a+n);

	if (a[n - 1] < a[n - 2] + a[n - 3])
	{
		cout << "YES" << endl;
		if (n % 2 == 0)
		{
			for (int i = n - 1; i >= 0; i -= 2)
			{
				b[x++] = a[i];

			}

			for (int i = 0; i < n; i += 2)
			{
				b[x++] = a[i];

			}




		}
		else{
			for (int i = n - 1; i >= 0; i -= 2)
			{
				b[x++] = a[i];
				

			}
			for (int i = 1; i < n; i += 2)
			{
				b[x++] = a[i];

			}
		
		
		
		}
		for (int i = 0; i < n - 1; i++)
		{
			cout << b[i] << " ";
		}
		cout << b[n - 1];
		cout << endl;
	}

	else{
		cout << "NO" << endl;
	
	
	}


}

运行结果如图所示:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值