添加相邻的nos。获得一系列对称性 [英] Adding adjacent nos. To get an array of symmetry

查看:72
本文介绍了添加相邻的nos。获得一系列对称性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

桌子上有几堆牌从左到右排列。所有这些都不包含相同数量的卡。由于老板坚持对称性,Ramu被要求合并最小数量的相邻桩,这样如果每个堆中的卡数列表按从左到右的顺序给出,则列表读取相同的是否在从左到右的顺序或从右到左的顺序。



例如,如果成堆的卡最初是1 2 2 5 1 3 1 1,那么可以有三个合并(2,2),(5,1)和(3,1)给1 4 6 4 1.

注意合并3个数字,比如说(1,2,2)计算当2合并时,一个在(1 2)之间,下一个在得到的3和接下来的两个之间。因此另一个解决方案(1,2,2)(5,1)(3,1,1)导致5 6 5需要2 + 2 + 1或5合并,并且不是最小的。



另一个(平凡的)解决方案是将所有桩合并为16。在某些情况下,这可能是唯一的解决方案,并且表明所有初始集至少有一个(不一定是最小的)解决方案< br $>




输入



有两行。

第一行是桩的数量。

下一行包含空格分隔的正整数,表示从左到右的堆中的卡数。



输出



如果最小合并导致多个堆,输出有两行,第一行给出一个整数(数字合并)和第二个包含空格分隔的整数,按顺序给出最终堆序列中的卡数(从左到右)。

如果除了合并为一堆之外没有解决方案,输出将包含字母TRIVIAL MERGE的单行。



约束



初始数量桩数小于50,初始配置中每堆的卡数将少于1000.





< pre lang =text>示例1

输入:
8
1 4 3 6 1 2 1 5

输出:
3
5 3 7 3 5





我的尝试:



  #include   <   iostream  >  

使用 命名空间标准;

int main()
{
cout<< Hello World<< ENDL;

return 0 ;
}

解决方案

  #include   <   iostream  >  
使用 命名空间标准;
int main()
{
cout<< Hello World<< ENDL;
return 0 ;
}



我们不是白痴。你应该说你什么也没做,至少是诚实的。



用一张纸和一支铅笔试试你的HomeWork,手动解决问题。由于你没有使用魔法,你已经在脑中构建了一个方法,你的任务是在代码中翻译该方法。



我们不做你的HomeWork。

HomeWork不会测试你乞求别人做你的工作的技巧,它会让你思考并帮助你的老师检查你对你所学课程的理解,以及你应用它们时遇到的问题。

你的任何失败都会帮助你的老师发现你的弱点并设定补救措施。

你的任何失败都会帮助你了解什么有效,什么无效,被称为'试错'学习。

所以,试一试,重读课程并开始工作。如果您遇到特定问题,请显示您的代码并解释这个问题,我们可能会提供帮助。



作为程序员,您的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学会​​如何。而且越快越好。

当你要求解决方案时,就像试图通过培训其他人来学习开车一样。

创建算法基本上是找到数学并做出必要的调整以适应你的实际问题。



发展的概念就像这个词所暗示的那样:系统地使用科学和满足特定目标或要求的技术知识。 BusinessDictionary.com [ ^ ]

这与有一个不一样快速谷歌并放弃,如果我找不到正确的代码。


There are several piles of cards on the table arranged from left to right. All of them do not contain the same number of cards. As the boss insists on symmetry, Ramu is asked to merge the minimum number of adjacent piles so that if a list of the number of cards in each pile is given in left to right order, the list reads the same whether it is read in the left to right order or the right to left order.

For example, if the piles of cards initially were 1 2 2 5 1 3 1 1, there can be three merges (2,2), (5,1) and (3,1) to give 1 4 6 4 1.
Note that merging 3 numbers, say (1,2,2) is counted as 2 merges, one between (1 2) and the next between the resulting 3 and the next two. Hence another solution (1,2,2) (5,1) (3,1,1) resulting in 5 6 5 takes 2 + 2 + 1 or 5 merges, and is not minimal.

Another (trivial) solution is to merge all the piles to give 16. This may be the only solution in some cases, and shows that all initial sets have at least one (not necessarily minimal) solution


Input

There are two lines.
The first line is the number of piles.
The next line contains space separated positive integers, representing the numbers of cards in the piles from left to right.

Output

If the minimal merge results in more than one pile, the output has two lines, the first giving one integer (the number of merges) and the second containing space separated integers, giving the number of cards in the final pile sequence in order (left to right).
If there is no solution other than merging all into one pile, the output will be a single line containing the letters "TRIVIAL MERGE".

Constraints

The initial number of piles will be less than 50, and the number of cards in each pile in the initial configuration will be less than 1000.


Example 1

Input:
8
1 4 3 6 1 2 1 5

Output:
3
5 3 7 3 5



What I have tried:

#include <iostream>

using namespace std;

int main()
{
   cout << "Hello World" << endl; 
   
   return 0;
}

解决方案

#include <iostream>
using namespace std;
int main()
{
   cout << "Hello World" << endl; 
   return 0;
}


We are not idiots. You should as well says that you did nothing, at least is would be honest.

Give a try to your HomeWork with a sheet of paper and a pencil, solve manually the problem. Since you don't use magic, you have build a method in your head, your task is to translate that method in code.

We do not do your HomeWork.
HomeWork is not set to test your skills at begging other people to do your work, it is set to make you think and to help your teacher to check your understanding of the courses you have taken and also the problems you have at applying them.
Any failure of you will help your teacher spot your weaknesses and set remedial actions.
Any failure of you will help you to learn what works and what don't, it is called 'trial and error' learning.
So, give it a try, reread your lessons and start working. If you are stuck on a specific problem, show your code and explain this exact problem, we might help.

As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.
When you just ask for the solution, it is like trying to learn to drive a car by having someone else training.
Creating an algorithm is basically finding the maths and make necessary adaptation to fit your actual problem.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".


这篇关于添加相邻的nos。获得一系列对称性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆