Most Unstable Array CodeForces - 1353A(数学+贪心+建设性算法)

题意:

给定 n, m,构造出一个长度为 n 的数组 a,使得数组的和为 m,在此条件下∑i=1n−1∣ai−ai−1∣\sum_{i=1}^{n-1}|a_{i}−a_{i-1}|i=1n1aiai1 最大是多少?

题目:

You are given two integers n and m. You have to construct the array a of length n 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 m and the value ∑i=1n−1∣ai−ai−1∣\sum_{i=1}^{n-1}|a_{i}−a_{i-1}|i=1n1aiai1 is the maximum possible. Recall that |x| is the absolute value of x.

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] then the value above for this array is |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 t independent test cases.

Input

The first line of the input contains one integer t (1≤t≤104) — the number of test cases. Then t test cases follow.

The only line of the test case contains two integers n and m (1≤n,m≤109) — the length of the array and its sum correspondingly.

Output

For each test case, print the answer — the maximum possible value of ∑i=1n−1∣ai−ai−1∣\sum_{i=1}^{n-1}|a_{i}−a_{i-1}|i=1n1aiai1 for the array a consisting of n non-negative integers with the sum m.

Example

Input

5
1 100
2 2
5 5
2 1000000000
1000000000 1000000000

Output

0
2
10
1000000000
2000000000

Note

In the first test case of the example, the only possible array is [100] and the answer is obviously 0.

In the second test case of the example, one of the possible arrays is [2,0] and the answer is |2−0|=2.

In the third test case of the example, one of the possible arrays is [0,2,0,3,0] and the answer is |0−2|+|2−0|+|0−3|+|3−0|=10.

思维:

挺有趣的思维水题。
可以注意到,n=1时答案为0,n=2时答案为m,n=3时整个差分之和不超过2m,即答案为2m.最大的情况就是0 a1 0 a2 0 a3…这么构造(a1+a2+a3…=m)或者0 0 0 m 0 0 0(m一定要放在中间)

AC代码

#include<stdio.h>
#include<string.h>
int t,n,m;
int main()
{scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);if(n==1)printf("0\n");else if(n==2)printf("%d\n",m);else printf("%d\n",2*m);}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/309748.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[C++11]共享智能指针shared_ptr指定删除器

当智能指针管理的内存对应的引用计数变为 0 的时候&#xff0c;这块内存就会被智能指针析构掉了。另外&#xff0c;我们在初始化智能指针的时候也可以自己指定删除动作&#xff0c;这个删除操作对应的函数被称之为删除器&#xff0c;这个删除器函数本质是一个回调函数&#xff…

基于 abp vNext 和 .NET Core 开发博客项目 - 再说Swagger,分组、描述、小绿锁

在开始本篇正文之前&#xff0c;解决一个 疯疯过 指出的错误&#xff0c;再次感谢指正。步骤如下&#xff1a;删掉.Domain.Shared层中的项目引用&#xff0c;添加nuget依赖包Volo.Abp.Identity.Domain.Shared&#xff0c;可以使用命令&#xff1a;Install-Package Volo.Abp.Ide…

[C++11]独占的智能指针unique_ptr的初始化和使用

std::unique_ptr 是一个独占型的智能指针&#xff0c;它不允许其他的智能指针共享其内部的指针&#xff0c;可以通过它的构造函数初始化一个独占智能指针对象&#xff0c;但是不允许通过赋值将一个 unique_ptr 赋值给另一个 unique_ptr。std::unique_ptr 不允许复制&#xff0c…

Two Arrays And Swaps CodeForces - 1353B(贪心+分类)

题意&#xff1a; 给定两个数组&#xff0c;你可以交换 k 次 两个数组的元素&#xff0c;问最后 a 数组的和最大可以是多少&#xff1f; 题目&#xff1a; You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also giv…

Abp v2.8.0发布 路线图

ABP框架和ABP商业版v2.8已经发布.这篇文章将涵盖这些发布中的新增内容和项目的中期路线图.ABP框架2.8有哪些新增内容?你可在GitHub的发行说明中看到所有的变更.这篇博客只包括重要的一些功能/变更.SignalR集成包我们已经发布了一个新的包用来集成SignalR到基于ABP框架应用程序…

贵州大学计算机专业的导师是谁,贵州大学计算机科学与信息学院导师介绍:王以松...

贵州大学计算机科学与信息学院导师介绍&#xff1a;王以松王以松&#xff0c;男&#xff0c;副教授&#xff0c;硕士研究生导师。主要研究方向&#xff1a;人工智能(知识表示与推理、逻辑程序设计)&#xff0c;语义网络等。 Em作者佚名次阅读2012-01-04王以松&#xff0c;男&am…

Board Moves CodeForces - 1353C(数学)

题意&#xff1a; 有一个 nn 的矩阵&#xff08;n 为奇数&#xff09;&#xff0c;每个矩阵单元有一个物品&#xff0c;每次操作你可将一个单元里的一个物品移动到该单元周围的八个单元里&#xff0c;问最后只有一个单元有物品的情况下&#xff0c;最少要多少次操作&#xff1…

BitArray虽好,但请不要滥用,又一次线上内存暴增排查

一&#xff1a;背景1. 讲故事前天写了一篇大内存排查在园子里挺火&#xff0c;这是做自媒体最开心的事拉&#xff0c;干脆再来一篇满足大家胃口&#xff0c;上个月我写了一篇博客提到过使用bitmap对原来的List<CustomerID>进行高强度压缩&#xff0c;将原来的List内存压缩…

2011年计算机基础知识试卷,2011年计算机一级考试理论试题:第六部分多选题

第6部分 信息与计算机基础知识 多选1.[ABC]通常来说&#xff0c;影响汉字输入速度的因素有 ______。(A)码长(B)重码率(C)是否有词组输入(D)有无提示行2.[BC]软件由______和______两部分组成。(A)数据(B)文档(C)程序(D)工具3.[BC]可以作为计算机存储容量的单位是______。(A)字母…

Constructing the Array CodeForces - 1353D(数据结构+分类+建设性算法)

题意&#xff1a; 有长度为 n 的数组 a &#xff0c;全为 0&#xff0c;接下来循环 n 次&#xff0c;每次选出一段最长的连续区间 [l, r]&#xff08;全为 0 &#xff0c;如果一样长&#xff0c;就选最左边的)。 如果 r−l1 是奇数&#xff0c;那么 a[lr2]ia[\frac{lr}{2}]ia…

[翻译]用于.NET Core的Windows窗体设计器发布

本文由微信公众号《开发者精选资讯》翻译首发&#xff0c;转载请注明来源今天我们很高兴地宣布&#xff0c;.NET Core 项目的 Windows 窗体设计器现在可以在 Visual Studio 2019 16.6 版中作为预览使用&#xff01;我们在 Visual Studio 16.7 预览版 1 中也提供了更新的设计器版…

运动会加油稿计算机学院,信息工程学院运动会加油稿

信息工程学院运动会加油稿1.你的汗水洒在跑道&#xff0c;浇灌着成功的花朵开放。你的欢笑飞扬在赛场&#xff0c;为班争光数你最棒。跑吧&#xff0c;追吧在这广阔的赛场上&#xff0c;你似骏马似离铉的箭。跑吧&#xff0c;追吧你比虎猛比豹强2你们挥舞着充满力量的双臂看着实…

K-periodic Garland CodeForces - 1353E(暴力+贪心+dp)

题意&#xff1a; 给定长为 n 的 0, 1 字符串&#xff0c;你可以通过一次操作改变一个字符&#xff08;0 变 1 or 1 变 0&#xff09;&#xff0c;问最少几次操作可以使任意相邻两个 1 之间的距离为 k ? 题目&#xff1a; You are given a garland consisting of n lamps. …

【视频回放与课件】零基础入门AI开发

今天上午&#xff0c;受广州图书馆邀请&#xff0c;在第一讲《零代码上手人工智能》的基础上&#xff0c;以《零基础入门AI开发》为主题&#xff0c;分四步解锁人工智能学习的概念与开发工具&#xff0c;让您在一小时内轻松掌握人工智能开发要领。本次课程内容主要包括&#xf…

三年级计算机群鸭戏水教案导入,三年级下册信息技术教案-3.7群鸭戏水-插入自选图形|清华版.doc...

第七课??《群鸭戏水——自选图形》??【教学内容分析】?本课&#xff0c;是小学信息技术(清华版)三年级下册第七课——自选图形的内容&#xff0c;通过前面章节的学习&#xff0c;学生已经学会了插入剪贴画和图片的操作。本节课创设了森林里要开动物运动会&#xff0c;请同…

Sequence with Digits CodeForces - 1355A(暴力+数学)

题意&#xff1a; 定义&#xff1a; an1anminDigit(an)maxDigit(an)。 给定 a1 和 k&#xff0c;求 ak &#xff1f; 题目&#xff1a; Let’s define the following recurrence: an1anminDigit(an)⋅maxDigit(an). Here minDigit(x) and maxDigit(x) are the minimal and …