SRM 397 DIV2 [500]

本文介绍了一个名为“排序游戏”的算法挑战,目标是在限定条件下通过最少的步骤将一组乱序的整数序列排序。文章详细解释了使用双向队列、集合等数据结构实现排序过程的方法。
部署运行你感兴趣的模型镜像

 

#include<iostream>
#include
<queue>
#include
<vector>
#include
<set>
#include
<algorithm>
using namespace std;

vector
<int> swap(vector<int> a,int beg,int len)
{
    
int mid=len/2,end=beg+len-1;;
    
for (int i=0;i!=mid;++i){
        
int temp=a[beg+i];
        a[beg
+i]=a[end-i];
        a[end
-i]=temp;
    }

    
return a;
}


class SortingGame{
public:
    
int fewestMoves(vector<int> a,int num)
    
{
        
int len=a.size();
           vector
<int> des(a);
        sort(des.begin(),des.end());
        
if (des==a) return 0;
        
set<vector<int> > vis;
        vis.insert(a);
        queue
<vector<int> > qData;
        qData.push(a);
        queue
<int> qTime;
        qTime.push(
0);
        
while (!qData.empty()){
            
for (int i=0;i!=len-num+1;++i){
                vector
<int> temp(swap(qData.front(),i,num) );
                
if (temp==des)     return ++qTime.front();
                
if (!vis.count(temp)){
                    vis.insert(temp);
                    qTime.push(qTime.front()
+1);
                    qData.push(temp);
                }

            }

            qData.pop();
            qTime.pop();
        }

        
return -1;
    }

}
;

Problem Statement

     In The Sorting Game, you are given a sequence containing a permutation of the integers between 1 and n, inclusive. In one move, you can take any k consecutive elements of the sequence and reverse their order. The goal of the game is to sort the sequence in ascending order. You are given a vector <int> board describing the initial sequence. Return the fewest number of moves necessary to finish the game successfully, or -1 if it's impossible.

Definition

    
Class: SortingGame
Method: fewestMoves
Parameters: vector <int>, int
Returns: int
Method signature: int fewestMoves(vector <int> board, int k)
(be sure your method is public)
    
 

Constraints

- board will contain between 2 and 8 elements, inclusive.
- Each integer between 1 and the size of board, inclusive, will appear in board exactly once.
- k will be between 2 and the size of board, inclusive.

Examples

0)  
    
{1,2,3}
3
Returns: 0
The sequence is already sorted, so we don't need any moves.
1)  
    
{3,2,1}
3
Returns: 1
We can reverse the whole sequence with one move here.
2)  
    
{5,4,3,2,1}
2
Returns: 10
This one is more complex.
3)  
    
{3,2,4,1,5}
4
Returns: -1
 
4)  
    
{7,2,1,6,8,4,3,5}
4
Returns: 7
 

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

您可能感兴趣的与本文相关的镜像

ACE-Step

ACE-Step

音乐合成
ACE-Step

ACE-Step是由中国团队阶跃星辰(StepFun)与ACE Studio联手打造的开源音乐生成模型。 它拥有3.5B参数量,支持快速高质量生成、强可控性和易于拓展的特点。 最厉害的是,它可以生成多种语言的歌曲,包括但不限于中文、英文、日文等19种语言

### 关于 SACMA SRM01 的中文版资料 SACMA SRM01 是由复合材料学会(Society for the Advancement of Material and Process Engineering, SAMPE)发布的标准之一,主要用于描述短切玻璃纤维增强热固性模塑料的性能测试方法[^1]。该标准通常涉及材料特性、制备工艺以及质量控制等方面的内容。 对于寻找 SACMA SRM01 中文版文档的需求,可以通过以下途径获取: #### 1. 官方渠道 SAMPE 或其中国分会可能提供官方翻译版本的下载服务。建议访问 SAMPE China 的官方网站或其他授权机构网站查询是否有正式的中文译本[^2]。 #### 2. 图书馆资源 部分高校图书馆或行业技术中心会收藏此类国际标准的技术文件及其翻译件。可以联系所在地区的科技图书馆或者通过 Interlibrary Loan (ILL) 请求借阅相关文献[^3]。 #### 3. 商业平台购买 一些商业数据库如 CNKI(知网)、万方数据等可能会收录经过合法授权的标准翻译版本。如果这些平台上未找到具体条目,则需进一步确认是否已发布官方认可的中文版本[^4]。 以下是基于 Python 编写的简单脚本来演示如何自动化搜索某些在线学术资源库中的关键词匹配项: ```python import requests from bs4 import BeautifulSoup def search_sacma(keyword="SACMA SRM01"): url = f"https://example-academic-resource.com/search?q={keyword}" response = requests.get(url) soup = BeautifulSoup(response.text, 'html.parser') results = [] for item in soup.find_all('div', class_='result-item'): title = item.find('h3').text.strip() link = item.find('a')['href'] abstract = item.find('p', class_='abstract').text.strip()[:150]+'...' results.append({ "title": title, "link": link, "abstract": abstract }) return results if __name__ == "__main__": res = search_sacma("SACMA SRM01") for r in res: print(f"{r['title']}\n{r['link']}\nAbstract: {r['abstract']}\n---\n") ``` 请注意以上代码仅为示例用途,在实际应用前应调整目标网址并遵循各站点的服务条款与版权政策[^5]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值