Functions again

Something happened in Uzhlyandia again… There are riots on the streets… Famous Uzhlyandian superheroes Shean the Sheep and Stas the Giraffe were called in order to save the situation. Upon the arriving, they found that citizens are worried about maximum values of the Main Uzhlyandian Function f, which is defined as follows:

在这里插入图片描述
The first line contains single integer n (2 ≤ n ≤ 105) — the size of the array a.

The second line contains n integers a1, a2, …, an (-109 ≤ ai ≤ 109) — the array elements.

Output
Print the only integer — the maximum value of f.

Examples
Input
5
1 4 2 3 1
Output
3
Input
4
1 5 4 7
Output
6
Note
In the first sample case, the optimal value of f is reached on intervals [1, 2] and [2, 5].

In the second case maximal value of f is reachable only on the whole array.

题目描述;先让奇数取反,偶数不变,求出最大连续子序列,在让奇数取反,求出最大连续子序列,最后求二者的最大值

解题思路:问题挺简单,先取反,然后正常去求就可以了,下次求的时候,在上次的基础上再次去反就可以了,这样奇数就变回来,偶数也取反了

#include <iostream>
#include <cmath>
using namespace std;
int a[100015];
int main()
{
    int x;
    cin>>x;
    for(long long i=1;i<=x;i++)
    {
        cin>>a[i];

    }
    for(long long  i=1; i<x; i++)
        a[i]=abs(a[i]-a[i+1]);
    long long max1=0,max2=0,maxn;
    for(long long i=1;i<x;i++)
    {
        if(i%2==1)
            a[i]=-a[i];
    }
    max1=maxn=a[1];
    for(long long i=2;i<x;i++)
    {
        if(maxn<=0)
            maxn=a[i];
        else
            maxn+=a[i];
        max1=max(max1,maxn);
    }
    for(long long i=1;i<x;i++)
    {
            a[i]=-a[i];
    }
    max2=maxn=a[1];
    for(long long i=2;i<x;i++)
    {
        if(maxn<=0)
        
            maxn=a[i];
        else
        
            maxn+=a[i];
        max2=max(max2,maxn);
        
    }
    long long maxnx=max(max1,max2);
    cout<<maxnx<<endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
设计一个多功能数字时钟 verilog ,具有计时,秒表,时钟三个功能的,同时使用6个7段数码管进行显示,有三个按键输入,三个LED显示当前模式,可以对时钟模式进行的数字进行修改这是怎么进行修改的说明First we will finish the clock we started working on in assignment #1. Here is the complete specification. The time is to be displayed on the 7-segment displays (hours, minutes and seconds, in 24-hour format). The buttons perform the following functions. KEY2 Set the time KEY1 Up KEY0 Down Specifically, if KEY2 is pressed for one second or longer, the seconds digits will flash at a rate of 2 Hz with a duty cycle of 80%, and the time stops advancing. Another press (however short) of KEY2 will cause only the minutes digits to flash, and yet another press will cause only the hours digits to flash, and one more press will cause the clock to return to normal, with the time starting to advance again. If some digits are flashing then the Up and Down keys (KEY1 and KEY0) can be used to increment and decrement their combined value. If one of these keys is pressed for less than half a second, the value should increment or decrement by unity. If pressed for 1 longer than half a second then the value should change rapidly, at a rate of ten numbers per second (in other words, changing by one unit once per 1/10 of a second). (The IFAdvance module from assignment #1 can be used to achieve this behaviour.),以下是部分模块的开头module Clock ( input clk , mode , inc , dec , output [4:0] hours , output [5:0] mins , secs , output [2:0] blank ); // ... endmodule module StopWatch ( input clk , reset , startStop , output [5:0] mins , secs , output [6:0] hundredths ); // ... endmodule module CountdownTimer ( input clk , reset , inc , startStop , output [4:0] hours , output [5:0] mins , secs , output buzzer ); // ... endmodule module Display ( input [7:0] num2 , num1 , num0 , input [2:0] blank , output [6:0] HEX5 , HEX4 , HEX3 , HEX2 , HEX1 , HEX0 ); // ... endmodule
最新发布
05-24
首先,我们需要一个模块来控制时钟的各种模式和计时。以下是 Clock 模块的代码: ``` module Clock ( input clk, input mode, input inc, input dec, output reg [4:0] hours, output reg [5:0] mins, output reg [5:0] secs, output reg [2:0] blank ); reg [3:0] mode_state = 0; reg [1:0] flash_counter = 0; reg [1:0] flash_rate = 1; reg [1:0] inc_counter = 0; reg [1:0] inc_rate = 0; reg [1:0] dec_counter = 0; reg [1:0] dec_rate = 0; reg [4:0] set_hours = 0; reg [5:0] set_mins = 0; reg [5:0] set_secs = 0; reg [1:0] set_digit = 0; reg [1:0] set_counter = 0; always @ (posedge clk) begin if (mode == 1) begin // stopwatch mode // TODO: implement stopwatch mode end else if (mode == 2) begin // countdown timer mode // TODO: implement countdown timer mode end else begin // clock mode if (flash_counter == 0) begin blank = 3; if (set_digit == 0) begin hours = set_hours; mins = set_mins; secs = set_secs; blank[1] = 1; blank[2] = 1; end else if (set_digit == 1) begin hours = hours; mins = set_mins; secs = set_secs; blank[2] = 1; end else begin hours = hours; mins = mins; secs = set_secs; end end else begin blank = 0; if (set_digit == 0) begin hours = set_hours; mins = set_mins; secs = set_secs; if (flash_counter == flash_rate) begin blank[1] = 1; blank[2] = 1; end end else if (set_digit == 1) begin hours = hours; mins = set_mins; secs = set_secs; if (flash_counter == flash_rate) begin blank[2] = 1; end end else begin hours = hours; mins = mins; secs = set_secs; if (flash_counter == flash_rate) begin blank[0] = 1; end end end if (inc == 1) begin if (set_digit == 0) begin if (inc_counter == 0) begin set_hours = IFAdvance(set_hours, 1); end else begin set_hours = IFAdvance(set_hours, 10); end end else if (set_digit == 1) begin if (inc_counter == 0) begin set_mins = IFAdvance(set_mins, 1); end else begin set_mins = IFAdvance(set_mins, 10); end end else begin if (inc_counter == 0) begin set_secs = IFAdvance(set_secs, 1); end else begin set_secs = IFAdvance(set_secs, 10); end end if (inc_counter == inc_rate) begin inc_counter = 0; inc_rate = 0; end else begin inc_counter = inc_counter + 1; if (inc_counter == 2) begin inc_rate = 1; end else if (inc_counter == 6) begin inc_rate = 2; end end end else if (dec == 1) begin if (set_digit == 0) begin if (dec_counter == 0) begin set_hours = IFAdvance(set_hours, -1); end else begin set_hours = IFAdvance(set_hours, -10); end end else if (set_digit == 1) begin if (dec_counter == 0) begin set_mins = IFAdvance(set_mins, -1); end else begin set_mins = IFAdvance(set_mins, -10); end end else begin if (dec_counter == 0) begin set_secs = IFAdvance(set_secs, -1); end else begin set_secs = IFAdvance(set_secs, -10); end end if (dec_counter == dec_rate) begin dec_counter = 0; dec_rate = 0; end else begin dec_counter = dec_counter + 1; if (dec_counter == 2) begin dec_rate = 1; end else if (dec_counter == 6) begin dec_rate = 2; end end end else begin inc_counter = 0; inc_rate = 0; dec_counter = 0; dec_rate = 0; end if (mode_state == 0) begin if (set_digit == 0) begin if (flash_counter == flash_rate) begin blank[1] = 1; blank[2] = 1; end end else if (set_digit == 1) begin if (flash_counter == flash_rate) begin blank[2] = 1; end end else begin if (flash_counter == flash_rate) begin blank[0] = 1; end end if (set_counter == 0) begin set_hours = hours; set_mins = mins; set_secs = secs; end if (mode == 2) begin mode_state = 2; end else if (mode == 1) begin mode_state = 1; end else if (mode_state == 2) begin mode_state = 1; end else begin mode_state = 0; end end else if (mode_state == 1) begin if (set_counter == 0) begin flash_counter = flash_counter + 1; if (flash_counter == 2) begin flash_counter = 0; end end if (mode == 2) begin mode_state = 2; end else if (mode == 0) begin mode_state = 0; set_digit = 0; end end else begin // mode_state == 2 if (set_digit == 0) begin blank[1] = 0; blank[2] = 0; end else if (set_digit == 1) begin blank[2] = 0; end else begin blank[0] = 0; end if (mode == 1) begin mode_state = 1; end else if (mode == 0) begin mode_state = 0; set_digit = 0; end if (inc == 1 || dec == 1) begin set_counter = set_counter + 1; if (set_counter == 2) begin set_counter = 0; set_digit = set_digit + 1; if (set_digit == 3) begin set_digit = 0; end end end else begin set_counter = 0; end end if (mode_state == 0 && flash_counter == 0) begin secs = IFAdvance(secs, 1); if (secs == 0) begin mins = IFAdvance(mins, 1); if (mins == 0) begin hours = IFAdvance(hours, 1); end end end end end endmodule ``` 接下来,我们需要一个模块来实现秒表功能。以下是 StopWatch 模块的代码: ``` module StopWatch ( input clk, input reset, input startStop, output reg [5:0] mins, output reg [5:0] secs, output reg [6:0] hundredths ); reg [3:0] state = 0; // 0 = stopped, 1 = running reg [6:0] counter = 0; always @ (posedge clk) begin if (reset == 1) begin state = 0; counter = 0; mins = 0; secs = 0; hundredths = 0; end else if (startStop == 1) begin if (state == 0) begin state = 1; end else begin state = 0; end end if (state == 1) begin hundredths = IFAdvance(hundredths, 1); if (hundredths == 100) begin hundredths = 0; secs = IFAdvance(secs, 1); if (secs == 60) begin secs = 0; mins = IFAdvance(mins, 1); end end end end endmodule ``` 最后,我们需要一个模块来实现倒计时器功能。以下是 CountdownTimer 模块的代码: ``` module CountdownTimer ( input clk, input reset, input inc, input startStop, output reg [4:0] hours, output reg [5:0] mins, output reg [5:0] secs, output reg buzzer ); reg [3:0] state = 0; // 0 = stopped, 1 = running, 2 = alarm reg [4:0] counter_hours = 0; reg [5:0] counter_mins = 0; reg [5:0] counter_secs = 0; reg [6:0] alarm_counter = 0; reg [1:0] alarm_rate = 0; always @ (posedge clk) begin if (reset == 1) begin state = 0; counter_hours = 0; counter_mins = 0; counter_secs = 0; hours = 0; mins = 0; secs = 0; buzzer = 0; end else if (startStop == 1) begin if (state == 0) begin state = 1; counter_hours = hours; counter_mins = mins; counter_secs = secs; end else if (state == 1) begin state = 0; end end if (state == 1) begin if (counter_secs == 0 && counter_mins == 0 && counter_hours == 0) begin state = 2; alarm_counter = 0; alarm_rate = 0; end else begin counter_secs = IFAdvance(counter_secs, -1); if (counter_secs == 59) begin counter_mins = IFAdvance(counter_mins, -1); if (counter_mins == 59) begin counter_hours = IFAdvance(counter_hours, -1); end end end end if (state == 2) begin if (alarm_counter == 0) begin buzzer = 1; end else if (alarm_counter == 3) begin buzzer = 0; end alarm_counter = alarm_counter + 1; if (alarm_counter == 8) begin alarm_counter = 0; alarm_rate = alarm_rate + 1; if (alarm_rate == 3) begin alarm_rate = 0; end end end if (inc == 1) begin if (state == 0) begin hours = IFAdvance(hours, 1); end else if (state == 1) begin if (counter_hours == 0) begin counter_hours = 23; end else begin counter_hours = counter_hours - 1; end end end if (state == 0 && buzzer == 1) begin secs = IFAdvance(secs, 1); if (secs == 60) begin secs = 0; mins = IFAdvance(mins, 1); if (mins == 60) begin mins = 0; hours = IFAdvance(hours, 1); end end end end endmodule ``` 最后,我们需要一个模块来控制7段数码管的显示。以下是 Display 模块的代码: ``` module Display ( input [7:0] num2, input [7:0] num1, input [7:0] num0, input [2:0] blank, output reg [6:0] HEX5, output reg [6:0] HEX4, output reg [6:0] HEX3, output reg [6:0] HEX2, output reg [6:0] HEX1, output reg [6:0] HEX0 ); assign HEX5 = ~(num2[6:0] & ~blank[2]); assign HEX4 = ~(num2[6:0] & blank[2]); assign HEX3 = ~(num1[6:0] & ~blank[1]); assign HEX2 = ~(num1[6:0] & blank[1]); assign HEX1 = ~(num0[6:0] & ~blank[0]); assign HEX0 = ~(num0[6:0] & blank[0]); endmodule ``` 我们将以上四个模块连接在一起,即可完成多功能数字时钟的设计。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值