【CodeForces 618A】Slime Combining(水题)

本文探讨了一种独特的游戏机制,玩家通过合成相同数值的元素以增加其价值,从初始的单一元素逐步构建复合状态,直至所有元素被整合。详细解释了实现方法,包括使用栈数据结构来简化元素的合成过程,并提供了代码示例以直观展示整个流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.

You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n - 1 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v + 1.

You would like to see what the final state of the row is after you’ve added all n slimes. Please print the values of the slimes in the row from left to right.

Input

The first line of the input will contain a single integer, n (1 ≤ n ≤ 100 000).

Output

Output a single line with k integers, where k is the number of slimes in the row after you’ve finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.

Sample Input

Input

1

Output

1

Input

2

Output

2

Input

3

Output

2 1

Input

8

Output

4

题目大意

给你一个整数n表示有n个1,每两个相同的数a可以合成a+1,比如n=3,表示有3个1,其中两个1可以合成2,剩下一个1,所以输出2 1。

思路

实现方法很多,我是用栈来实现的,每次都将当前准备进栈的数同栈顶元素比较,相等则栈顶元素出栈并+1,再重复上述步骤知道当前进栈元素和栈顶元素不相同时,进栈。最后用数组存栈中的数据,逆序输出就好了(因为栈是先进后出)

代码

#include <iostream>
 #include <cstdio>
 #include <cstring>
 #include <algorithm>
 #include <stack>

 using namespace std;

 const int maxn=1e5+5;

 int ans[maxn];

 int main()
 {
    int n;
    while(~scanf("%d",&n))
    {
        stack<int> q;
        int temp=-1,tem=-1;
        for(int i=1;i<=n;i++)
        {
            if(!q.empty()) temp=q.top();
            else temp==-1;
            q.push(1);
            while(q.top()==temp)
            {
                tem=q.top()+1;
                q.pop();q.pop();
                if(!q.empty()) temp=q.top();
                else temp==-1;
                q.push(tem);
            }
        }
        int k=q.size();
        for(int i=0;i<k;i++)
        {
            ans[i]=q.top();
            q.pop();
        }
        for(int i=k-1;i>=0;i--)
        {
            if(i==k-1) printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
 }
### 如何在 Codeforces 上找到数学相关的Codeforces 是一个非常受欢迎的在线编程竞赛平台,其中包含了大量涉及不同算法和数据结构类型的目。为了更高效地查找特定主(如数学类)的相关目,可以采用以下方法: #### 方法一:通过标签筛选 Codeforces 的每道目都会被打上相应的标签,这些标签可以帮助用户快速定位到感兴趣的领域。对于数学类目,可以通过访问 Codeforces 目列表页面并利用过滤功能来实现目标[^1]。 具体步骤如下: 1. 打开 Codeforces 官网 (https://codeforces.com/) 并进入 **Problems** 页面。 2. 使用左侧栏中的 “Tags” 过滤器选择与数学相关的标签,例如 `math` 或者 `number theory`。 3. 应用过滤条件后即可看到符合条件的所有目。 这种方法的优点在于简单直观,适合初学者或者希望浏览广泛范围内的数学目的选手。 #### 方法二:借助第三方工具或脚本自动化检索 如果想要更加精确地获取某些难度级别下的数学问,则可能需要编写一些辅助程序来进行批量处理。比如使用 Python 编写的网络爬虫可以从网站提取所需信息,并按照自定义标准分类整理[^3]。 下面展示一段用于抓取指定类别下样本输入输出的小型示例代码片段: ```python from bs4 import BeautifulSoup as soup import requests def fetch_codeforces_problem(tag='math', count=5): url = f'https://codeforces.com/problemset?tags={tag}' response = requests.get(url) page_soup = soup(response.text, "html.parser") problems = [] problem_entries = page_soup.select('.problems tr[data-index]') for entry in problem_entries[:count]: title = entry.find('a').text.strip() link = 'https://codeforces.com' + entry.find('a')['href'] # Fetch sample input/output if necessary. prob_response = requests.get(link) prob_page = soup(prob_response.text, "html.parser") inputs = prob_page.find_all(name="div", attrs={"class": "input"}) outputs = prob_page.find_all(name="div", attrs={"class": "output"}) samples = [ ("Input", "\n".join([str(pre.contents[0]) for pre in inp.find_all("pre")])), ("Output", "\n".join([str(pre.contents[0]) for pre in outp.find_all("pre")])) ] for inp,outp in zip(inputs,outputs)] problems.append({ 'Title':title, 'Link':link, 'Samples':samples }) return problems # Example usage: print(fetch_codeforces_problem()) ``` 此函数会返回前几个带有给定标签的问及其基本详情,包括链接以及部分测试案例的数据表示形式。 #### 方法三:参与专训练营或比赛 除了单独练习之外,还可以参加专注于某一技能提升的比赛活动,像 CF Round 中经常会有专门针对某种技巧设计的一系列挑战项目,在实战环境中锻炼自己的能力往往事半功倍[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值