uva 11925——Generating Permutations

本文介绍了一种使用不超过2*n²次操作将任意1-n的排列转换为升序排列的方法。通过贪心策略,利用双端队列实现数据处理,具体包括交换前两个元素或将最后一个元素移至队首的操作。


题意:给定一个1-n的排列,用不超过2*n2的操作把他变成升序。每次操作只有两种,一种是交换前两个元素,另外一种是把最后一个元素放到最后一位。


思路:贪心。用双端队列来保存数据,每次当v[0]>v[1]&&v[0]!=n时用交换前两个的策略把当前排好,否则就放后面等待被排。


code:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const int INF=0x3fffffff;
const int inf=-INF;
const int N=180005;
const int M=2005;
const int mod=1000000007;
const double pi=acos(-1.0);

#define cls(x,c) memset(x,c,sizeof(x))
#define cpy(x,a) memcpy(x,a,sizeof(a))
#define fr(i,s,n) for (int i=s;i<=n;i++)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lrt  rt<<1
#define rrt  rt<<1|1
#define middle int m=(r+l)>>1
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define mk make_pair
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);

deque<int>v;
int s[N],len;

int check()
{
    fr (i,1,v.size()-1)
    if (v[i]<v[i-1]) return 0;
    return 1;
}
int main()
{
    int T,n,x;
    //scanf("%d",&T);
    while (~scanf("%d",&n)&&n)
    {
        v.clear();len=0;
        fr (i,1,n) scanf("%d",&x),v.push_back(x);
        while (!check())
        {
            if (v[0]>v[1]&&v[0]!=n)
            {
                swap(v[0],v[1]);
                s[len++]=1;
                //s='1'+s;
                //cout<<"bug1"<<endl;
            }
            else
            {
                int t=v.back();v.pop_back();
                //cout<<t<<endl;
                v.push_front(t);
                s[len++]=2;
                //s='2'+s;
            }
        }
       for (int i=len-1;i>=0;i--) printf("%d",s[i]);
       puts("");
        //cout<<s<<endl;
    }
}


(Kriging_NSGA2)克里金模型结合多目标遗传算法求最优因变量及对应的最佳自变量组合研究(Matlab代码实现)内容概要:本文介绍了克里金模型(Kriging)与多目标遗传算法NSGA-II相结合的方法,用于求解最优因变量及其对应的最佳自变量组合,并提供了完整的Matlab代码实现。该方法首先利用克里金模型构建高精度的代理模型,逼近复杂的非线性系统响应,减少计算成本;随后结合NSGA-II算法进行多目标优化,搜索帕累托前沿解集,从而获得多个最优折衷方案。文中详细阐述了代理模型构建、算法集成流程及参数设置,适用于工程设计、参数反演等复杂优化问。此外,文档还展示了该方法在SCI一区论文中的复现应用,体现了其科学性与实用性。; 适合人群:具备一定Matlab编程基础,熟悉优化算法和数值建模的研究生、科研人员及工程技术人员,尤其适合从事仿真优化、实验设计、代理模型研究的相关领域工作者。; 使用场景及目标:①解决高计算成本的多目标优化问,通过代理模型降低仿真次数;②在无法解析求导或函数高度非线性的情况下寻找最优变量组合;③复现SCI高水平论文中的优化方法,提升科研可信度与效率;④应用于工程设计、能源系统调度、智能制造等需参数优化的实际场景。; 阅读建议:建议读者结合提供的Matlab代码逐段理解算法实现过程,重点关注克里金模型的构建步骤与NSGA-II的集成方式,建议自行调整测试函数或实际案例验证算法性能,并配合YALMIP等工具包扩展优化求解能力。
### UVA 12000: A Programming and Algorithm Perspective UVA 12000, commonly referred to in competitive programming circles, is a problem that involves algorithmic thinking and efficient implementation. While no direct description of UVA 12000 is provided in the references, we can infer potential strategies based on common patterns seen in similar problems like UVA 1363 and other Josephus problem variants. The problem likely involves a sequence or array manipulation, possibly with modular arithmetic or cyclic behavior. Based on the pattern of other UVA problems involving recursive or iterative logic, UVA 12000 may require a solution that leverages mathematical observations to reduce complexity. For instance, if UVA 12000 involves a similar structure to the Josephus problem, the solution could be expressed using a loop that iteratively computes the position of the survivor or some other derived value. The following code snippet demonstrates a general approach that could be adapted depending on the exact requirements of the problem: ```python def solve(n, k): r = 0 for i in range(1, n + 1): r = (r + k) % i return r ``` This function computes a result using an iterative approach where `k` is added to the current result `r` and then the modulo operation is applied with the current index `i`. This is a common technique in problems involving circular elimination or selection processes. In the case of UVA 12000, the problem may require additional constraints or a variation of this logic. For example, it could involve handling large input sizes efficiently, which would necessitate optimizing the algorithm to avoid unnecessary computations. Techniques such as memoization or dynamic programming could play a role in achieving this efficiency. When implementing a solution, it is crucial to consider edge cases, such as when `n` or `k` is very small or when `k` exceeds `n`. Handling these cases correctly ensures robustness in the implementation. For example, if `k` is larger than `i` during the loop iteration, the modulo operation naturally reduces it to an equivalent smaller value. If the problem involves prime numbers or paths, such as in UVA 12101, then a breadth-first search (BFS) or similar graph traversal technique might be required. In such cases, the solution would involve generating valid transitions between states and efficiently exploring the search space. ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值