Vova and Trophies

题意翻译

题目描述

你现在有 nn 枚奖牌,每枚奖牌为金牌或银牌。这些奖牌现在按顺序排成一排。现在你可以调换任意一对奖牌的位置,求金牌最长连续段。

输入格式

第一行一个整数 nn (2\le n\le 10^5)(2≤n≤105) .

第二行一行字符串,表示你的奖牌。 ( \texttt{G}G 表示金牌,\texttt{S}S 表示银牌)

输出格式

输出一个整数表示答案。

题目描述

Vova has won nn trophies in different competitions. Each trophy is either golden or silver. The trophies are arranged in a row.

The beauty of the arrangement is the length of the longest subsegment consisting of golden trophies. Vova wants to swap two trophies (not necessarily adjacent ones) to make the arrangement as beautiful as possible — that means, to maximize the length of the longest such subsegment.

Help Vova! Tell him the maximum possible beauty of the arrangement if he is allowed to do at most one swap.

输入输出格式

输入格式:

 

The first line contains one integer nn ( 2 \le n \le 10^52≤n≤105 ) — the number of trophies.

The second line contains nn characters, each of them is either G or S. If the ii -th character is G, then the ii -th trophy is a golden one, otherwise it's a silver trophy.

 

输出格式:

 

Print the maximum possible length of a subsegment of golden trophies, if Vova is allowed to do at most one swap.

 

输入输出样例

输入样例#1: 复制

10
GGGSGGGSGG

输出样例#1: 复制

7

输入样例#2: 复制

4
GGGG

输出样例#2: 复制

4

输入样例#3: 复制

3
SSS

输出样例#3: 复制

0

说明

In the first example Vova has to swap trophies with indices 44 and 1010 . Thus he will obtain the sequence "GGGGGGGSGS", the length of the longest subsegment of golden trophies is 77 .

In the second example Vova can make no swaps at all. The length of the longest subsegment of golden trophies in the sequence is 44 .

In the third example Vova cannot do anything to make the length of the longest subsegment of golden trophies in the sequence greater than 00 .

思路:记录字符串中每个s前面有多少个g,如果没有s,就只统计g的个数,然后保留到二维数组里,剩下的就是分情况讨论,1:如果某个s前后的g的个数总和小于总共g的个数,说明可以从其他地方拿一个,这样的话就可以将s前后的两个g串合起来然后长度+1;2:如果某个s前后的g的个数总和等于总共g的个数(比如ggggsggg)那么就是将一个串中拿出一个g替换s,这样长度就是两个g串长度的和;3,这个就比较简单了,说明没有s,只有g,直接打印

#include<iostream>
#include<queue>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long int LL;
const int inf = 0x7f7f7f7f;
const int N=2e5+5;
int num[N];
int main()
{
   int n;
   vector<int>ve;
   int cnt=0;
   cin>>n;
   string str;
   cin>>str;
   int sumg=0;
   int count=0;
   for(int i=0;i<n;i++){
    if(str[i]=='S'){//遇到s,就将之前的g的个数保存的数组里
        ve.push_back(count);
        count=0;
    }
    else{
        count++;//用来统计每个s前面g的个数
    }
   }
   for(int i=0;i<n;i++){//记录g的总共个数
    if(str[i]=='G')
        sumg++;
   }

   if(str[n-1]=='G'){//特判一下最后一个不是S
    ve.push_back(count);
   }
   int ans=0;
   if(ve.size()==1){//
    cout<<ve[0]<<endl;
    return 0;
   }
   else{
   for(int i=0;i<ve.size()-1;i++){
        if(ve[i]+ve[i+1]<sumg)//第一种情况
    ans=max(ans,ve[i]+ve[i+1]+1);
   else{
    ans=max(ans,ve[i]+ve[i+1]);//第二种情况
   }
   }
}
cout<<ans<<endl;

    return 0;

}

 

Vova STC32系列单片机上位机进行PID(比例积分微分)控制的调试,通常涉及到以下几个步骤: 1. **PID算法理解**: PID控制器由比例(P)、积分(I)和微分(D)三个部分组成,用于实时调整系统的输出,以稳定给定的输入。 2. **硬件连接**: 首先,确保你的STC32单片机和上位机(如PC或嵌入式开发板)之间有正确连接。可能需要串口通信(例如UART),并通过编程将PID参数传输到单片机。 3. **PID库或函数**: 使用STC32的C语言库或者自定义函数实现PID算法。这可能包括设置PID系数(Kp, Ki, Kd)、计算输出值、以及更新系统状态等。 4. **初始化和配置**: 初始化PID控制器,设定死区、积分限幅和微分滤波时间常数等参数,以避免积分发散和过快的微分响应。 5. **数据采集与反馈**: 单片机需要从传感器读取实际测量值,并将此值作为PID算法的输入。 6. **单片机编程**: 编写单片机代码,调用PID函数,根据反馈值计算出控制输出,并驱动执行器。 7. **调试过程**: - 在上位机软件中,你可以使用串口监视器观察通信数据,确认PID的输入输出是否正常。 - 调整PID参数,观察系统的动态响应是否改善,是否存在振荡或过度调整。 - 如果有问题,检查硬件连接、代码逻辑和参数设置。 8. **性能优化**: 可能需要通过实验或理论分析来调整PID参数,直到达到满意的控制精度和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值