A. Most Unstable Array

我想过
认真面对比赛,坚持到最后一秒,表面看似没有收获,实际上是在为以后的种种可能埋下伏笔。但是,今晚我做出来了A题,比以往效率大大增加。就是用了自己反思总结的方法,但是我放弃比赛了。要睡觉,因为个人认为现在更重要的是学习的节奏,明天要早起。现在心态还行,把重点放在周天的比赛,这几天多刷题,多回顾网课。
A. Most Unstable Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers nn and mm. You have to construct the array aa of length nn consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly mm and the value ∑i=1n−1|ai−ai+1|∑i=1n−1|ai−ai+1| is the maximum possible. Recall that |x||x| is the absolute value of xx.In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array a=[1,3,2,5,5,0]a=[1,3,2,5,5,0] then the value above for this array is |1−3|+|3−2|+|2−5|+|5−5|+|5−0|=2+1+3+0+5=11|1−3|+|3−2|+|2−5|+|5−5|+|5−0|=2+1+3+0+5=11. Note that this example doesn’t show the optimal answer but it shows how the required value for some array is calculated.You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.The only line of the test case contains two integers nn and mm (1≤n,m≤1091≤n,m≤109) — the length of the array and its sum correspondingly.OutputFor each test case, print the answer — the maximum possible value of ∑i=1n−1|ai−ai+1|∑i=1n−1|ai−ai+1| for the array aa consisting of nn non-negative integers with the sum mm.ExampleinputCopy5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000
outputCopy0
2
10
1000000000
2000000000
NoteIn the first test case of the example, the only possible array is [100][100] and the answer is obviously 00.In the second test case of the example, one of the possible arrays is [2,0][2,0] and the answer is |2−0|=2|2−0|=2.In the third test case of the example, one of the possible arrays is [0,2,0,3,0][0,2,0,3,0] and the answer is |0−2|+|2−0|+|0−3|+|3−0|=10|0−2|+|2−0|+|0−3|+|3−0|=10.
我也用一句话来总结
给定一个长度n和为m的数组,求从1到n-1相邻两数差的最大和。

 #include <iostream>
using namespace std;
int main()
{
 int n,m,t;
 cin>>t;
 while(t--)
 {
  cin>>n>>m;
  if(n==1)
   cout<<0<<endl;
  else if(n==2)
   cout<<m<<endl;
  else
   cout<<(m*2)<<endl;
 }
 return 0;
}


这样做对不对,以后会给自己评

价的。晚安。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值