abyy a+poj1062

解决一个算法问题,目标是在遵循特定规则的情况下找到树木序列中美的最大总和。此问题涉及动态规划与数据结构操作。

A2. Oh Sweet Beaverette

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

— Oh my sweet Beaverette, would you fancy a walk along a wonderful woodland belt with me?

— Of course, my Smart Beaver! Let us enjoy the splendid view together. How about Friday night?

At this point the Smart Beaver got rushing. Everything should be perfect by Friday, so he needed to prepare the belt to the upcoming walk. He needed to cut down several trees.

Let's consider the woodland belt as a sequence of trees. Each tree i is described by the esthetic appeal ai — some trees are very esthetically pleasing, others are 'so-so', and some trees are positively ugly!

The Smart Beaver calculated that he needed the following effects to win the Beaverette's heart:

  • The first objective is to please the Beaverette: the sum of esthetic appeal of the remaining trees must be maximum possible;
  • the second objective is to surprise the Beaverette: the esthetic appeal of the first and the last trees in the resulting belt must be the same;
  • and of course, the walk should be successful: there must be at least two trees in the woodland belt left.

 

Now help the Smart Beaver! Which trees does he need to cut down to win the Beaverette's heart?

Input

The first line contains a single integer n — the initial number of trees in the woodland belt, 2 ≤ n. The second line contains space-separated integers ai — the esthetic appeals of each tree. All esthetic appeals do not exceed 109 in their absolute value.

 

  • to get 30 points, you need to solve the problem with constraints: n ≤ 100 (subproblem A1);
  • to get 100 points, you need to solve the problem with constraints: n ≤ 3·105 (subproblems A1+A2).

 

Output

In the first line print two integers — the total esthetic appeal of the woodland belt after the Smart Beaver's intervention and the number of the cut down trees k.

In the next line print k integers — the numbers of the trees the Beaver needs to cut down. Assume that the trees are numbered from 1 ton from left to right.

If there are multiple solutions, print any of them. It is guaranteed that at least two trees have equal esthetic appeal.

题意:有一排树,每个树有美丑值,让你砍出最大的一串,顺便要输出砍树的序号,砍树要求:

剩下的一串最左边和最右边相等,最少剩2棵树

其实这题跟最大子序列差不多,而且还不用按序列砍,随意砍的话我们用dp存到i为止最大能到多少,也就是dp[i]=dp[i-1]+a[i]>0?a[i]:0;

然后因为要头尾相等我们用map记录下每个新出的节点的序号,然后找对应的右端点与之对应,假如a[i]已经访问,就可以求他到左端的最大串,注意a[i]必须区,所以不能直接用dp[i]-dp[visit[a[i]-1],要是a[i]<0就跪了。

这题坑我的地方是,他0 0竟然不输出,我输出了0,就跪了,fuck

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
using namespace std;
struct node{
    int number;
    __int64 value;
};
int a[300010];
__int64 dp[300010];
int cut[300010];
node ans;
//__int64 ans[100010];
int main()
{
    map <int,int> visit;
    //__int64 sum;
    int i,j,m,n;
    //sum=0;
    int cnt=0;
    ans.value=-2000000010;
    memset(dp,0,sizeof(dp));
    cin>>n;
    for (i=1;i<=n;i++)
    cin>>a[i];
    for (i=1;i<=n;i++)
    {
            if (a[i]>=0)
            {
                dp[i]=dp[i-1]+a[i];
            }
            else {dp[i]=dp[i-1];}

        if (!visit[a[i]])      visit[a[i]]=i;
        else
        {
            if (ans.value<dp[i-1]-dp[visit[a[i]]]+2*a[i])
            {
                ans.value=dp[i-1]-dp[visit[a[i]]]+2*a[i];
                ans.number=i;
            }
        }
    }
        cout<<ans.value<<" ";
        for (i=1;i<visit[a[ans.number]];i++)
            cut[cnt++]=i;
        for (i=visit[a[ans.number]]+1;i<ans.number;i++)
            if (a[i]<0) cut[cnt++]=i;
        for (i=ans.number+1;i<=n;i++)
            cut[cnt++]=i;
        cout<<cnt<<endl;
        for (i=0;i<cnt-1;i++)
        cout<<cut[i]<<" ";
        if (cnt) cout<<cut[cnt-1]<<endl;
}


下面是poj1062,基本没什么好说的,模板题,我多加了分号,刚好空着一起写了

 

 

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#define inf 0x7fffffff
using namespace std;
bool visit[101];
int price[101][101],n;
int dist[102];
int djskl()
{
    int i,j,te;
    for (i=1;i<=n;i++)
    dist[i]=price[0][i];
    int node=0;
    for (j=1;j<=n;j++)
    {
        te=inf;
        for (i=1;i<=n;i++)
    {

        //cout<<dist[i]<<endl;
        if (te>dist[i]&&!visit[i])
        {
            te=dist[i];
            node=i;
            //cout<<te<<endl;
        }
    }
    if (node==0) break;
    //cout<<node<<endl;
    visit[node]=true;
    for (i=1;i<=n;i++)
    {
        if (!visit[i]&&price[node][i]>0&&dist[i]>dist[node]+price[node][i])
        dist[i]=dist[node]+price[node][i];
    }
    //cout<<node<<endl;
    }

    return dist[1];
}
int main()
{
    int level[101];
    int i,j,m,x,t,v;
    while (cin>>m>>n)
    {
        memset(price,0,sizeof(price));
        memset(visit,false,sizeof(visit));
        memset(level,0,sizeof(level));
        memset(dist,inf,sizeof(dist));
        for (i=1;i<=n;i++)
        {
            cin>>price[0][i]>>level[i]>>x;
            for (j=1;j<=x;j++)
            {
                cin>>t>>v;
                price[t][i]=v;
            }
        }
        //cout<<price[3][1]<<endl;
        v=inf;
        for (i=1;i<=n;i++)
        {
            for (j=1;j<=n;j++)
            {if (level[j]>level[i]||level[i]-level[j]>m)
            visit[j]=true;
            else visit[j]=false;}
            t=djskl();
            //cout<<dist[1]<<end
            //cout<<price[3][1]<<endl;
        if (t<v) v=t;

        }
        cout<<v<<endl;
        //cout<<price[4][1]<<endl;
        }
}


代码很差,没改的说。。。

 

 

基于STM32 F4的永磁同步电机无位置传感器控制策略研究内容概要:本文围绕基于STM32 F4的永磁同步电机(PMSM)无位置传感器控制策略展开研究,重点探讨在不依赖物理位置传感器的情况下,如何通过算法实现对电机转子位置和速度的精确估计与控制。文中结合嵌入式开发平台STM32 F4,采用如滑模观测器、扩展卡尔曼滤波或高频注入法等先进观测技术,实现对电机反电动势或磁链的估算,进而完成无传感器矢量控制(FOC)。同时,研究涵盖系统建模、控制算法设计、仿真验证(可能使用Simulink)以及在STM32硬件平台上的代码实现与调试,旨在提高电机控制系统的可靠性、降低成本并增强环境适应性。; 适合人群:具备一定电力电子、自动控制理论基础和嵌入式开发经验的电气工程、自动化及相关专业的研究生、科研人员及从事电机驱动开发的工程师。; 使用场景及目标:①掌握永磁同步电机无位置传感器控制的核心原理与实现方法;②学习如何在STM32平台上进行电机控制算法的移植与优化;③为开发高性能、低成本的电机驱动系统提供技术参考与实践指导。; 阅读建议:建议读者结合文中提到的控制理论、仿真模型与实际代码实现进行系统学习,有条件者应在实验平台上进行验证,重点关注观测器设计、参数整定及系统稳定性分析等关键环节。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值