E. Easy Assembly

本文介绍了一个关于塔合并的问题,目标是将多个塔通过特定规则合并成一个递增序列的塔。文章详细阐述了解决方案,包括如何计算所需的操作次数,并提供了一段C++代码实现。

传送门

题意:给你一个数n,代表有n座塔,每一行的数不一样,每一行的第一个元素形成一个塔的塔顶,第二个元素在塔顶的正下方...(有n个塔)

然后你可以进行两个操作:

1.从上往下取任意长度的元素形成一个新塔。

2.把两个塔合成一个新塔。

要求最后只能形成一个塔,并且这个塔的的元素从高到低必须是递增的。

问你两个操作数的次数分别是多少?

思路:因为塔的元素必须是递增的,那么塔的情况只有一个,那么我们就把塔的最终的情况先算出来,然后存储每个元素在塔的位置的下标。然后对于某一个塔,如果当前元素大于下一个元素或者下一个元素不是比他大的最小的元素的话,就要用2操作,然后多产生一个塔。然后全部操作完成之后,因为有n个塔要进行组合的操作,那么直接就是n-1的组合操作就可以了。

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃  	    ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

#include<cstdio>
#include <iostream>
#include 
/** * NOTE: This file has been copied and slightly modified from {com.alibaba.csp.sentinel.datasource.redis}. * <p> * <h2>Redis Datasource Task Elements Operation</h2> * * This class is a concrete implementation of a remote configuration data source, * built on top of Redis's high-performance read/write capabilities and Pub/Sub real-time * messaging mechanism. It enables dynamic loading, listening, and publishing of rule or * configuration data with low latency and strong consistency. * * <p>The primary design objectives are:</p> * * <ul> * <li><b>Real-time Updates</b>: Subscribes to a designated Redis Pub/Sub channel to detect * configuration changes immediately and trigger hot-reload events.</li> * <li><b>Deployment Flexibility</b>: Supports multiple Redis deployment modes including * standalone, Sentinel (for high availability), and Redis Cluster (for horizontal scaling).</li> * <li><b>Security Support</b>: Provides full SSL/TLS encryption support with compatibility for * both JKS and PEM certificate formats, meeting production security requirements.</li> * <li><b>Extensibility</b>: Extends {@link RemoteDatasourceTaskElementsOperation}, adhering to a unified * datasource abstraction contract. Can be seamlessly integrated into systems requiring * runtime reconfiguration, such as rule engines, policy managers, or distributed gateways.</li> * </ul> * * <h3>Working Principle</h3> * <p>During initialization, the class automatically selects the appropriate client based on * the provided {@link RedisConnectionConfig}: * <ul> * <li>If cluster nodes are configured, it creates a {@link RedisClusterClient}.</li> * <li>Otherwise, it uses a {@link RedisClient} for standalone or Sentinel mode connections.</li> * </ul> * * Additionally, it sets up a "lazy-loaded" subscription via {@link #setLazyListener(java.util.function.Supplier)} — * meaning the actual Pub/Sub connection and subscription occur only when an external system * explicitly requests listening (e.g., during startup of a configuration watcher). * This lazy initialization strategy reduces resource consumption and improves application startup time. * * <h3>Key Method Overview</h3> * <table border="1" cell-padding="8"> * <tr><th>Method</th><th>Purpose</th></tr> * <tr> * <td>{@link #getRemoteConfigInfo()}</td> * <td>Retrieves the current configuration value associated with {@code ruleKey} from Redis (via GET)</td> * </tr> * <tr> * <td>{@link #publishConfig(String)}</td> * <td>Publishes new configuration data to Redis (via SET), typically used to broadcast updates</td> * </tr> * <tr> * <td>{@link #subscribeFromChannel(String)}</td> * <td>Establishes a Pub/Sub connection and subscribes to a specific channel; incoming messages * are dispatched through {@link DelegatingRedisPubSubListener}</td> * </tr> * <tr> * <td>{@link #close()}</td> * <td>Gracefully shuts down the Redis client and releases all underlying resources</td> * </tr> * </table> * * <h3>Typical Use Cases</h3> * <ul> * <li>Dynamic rule loading in rule engines (e.g., Drools, Easy Rules)</li> * <li>Hot-reloading of distributed configurations in microservices</li> * <li>Real-time policy or permission updates in access control systems</li> * <li>Centralized rate-limiting or circuit-breaker configuration management</li> * </ul> * <h3>Example Usage</h3> * <pre>{@code * RedisConnectionConfig config = new RedisConnectionConfig(); * config.setHost("localhost"); * config.setPort(6379); * config.setTimeout(5000); * * RedisDatasourceTaskElementsOperation redisSource = * new RedisDatasourceTaskElementsOperation(config, "rule:flow", "channel:rule-update", ConfigFormat.JSON); * * // Retrieve current configuration * String currentConfig = redisSource.getRemoteConfigInfo(); * * // Start listening (triggers lazy subscription) * redisSource.startListen(); * * // Publish new config (other instances will receive notification) * redisSource.publishConfig("{\"rateLimit\": 1000}"); * * // Clean up resources * redisSource.close(); * }</pre> * * @author <a href="mailto:929160069@qq.com">zhangpengfei</a> * @since 3.0.2 */ 编译报错 [ERROR] /Users/zhangpengfei/workspace/assembly/assembly-simplified-cron/cron-redis-datasource-driven-scheduled/src/main/java/top/osjf/cron/datasource/driven/scheduled/redis/RedisDatasourceTaskElementsOperation.java:97: 错误: 此处不允许使用标记: <tr> [ERROR] * <tr>
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值