codeforces Round #642 div.3 ABC题解

A. Most Unstable Arraytime

              limit per test1 second
              memory limit per test256 megabytes
               input standard input
                output standard output

You 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.
Example
input
1 100
2 2
5 5
2 1000000000
1000000000 1000000000
output
2
10
1000000000
2000000000
Note
In the first test case of the example, the only possible array is [100][100] and the answer is obviously 0.
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.

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

B. Two Arrays And Swapstime

limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given two arrays aa and bb both consisting of nn positive (greater than zero) integers. You are also given an integer kk.In one move, you can choose two indices ii and jj (1≤i,j≤n1≤i,j≤n) and swap aiai and bjbj (i.e. aiai becomes bjbj and vice versa). Note that ii andjj can be equal or different (in particular, swap a2a2 with b2b2 or swap a3a3 and b9b9 both are acceptable moves).Your task is to find the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk such moves (swaps).You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤2001≤t≤200) — the number of test cases. Then tt test cases follow.The first line of the test case contains two integers nn and kk (1≤n≤30;0≤k≤n1≤n≤30;0≤k≤n) — the number of elements in aa and bb and the maximum number of moves you can do. The second line of the test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤301≤ai≤30), where aiai is the ii-th element of aa. The third line of the test case contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤301≤bi≤30), where bibi is the ii-th element of bb.OutputFor each test case, print the answer — the maximum possible sum you can obtain in the array aa if you can do no more than (i.e. at most) kk swaps.
Example
input
2 1
1 2
3 4
5 5
5 5 6 6 5
1 2 5 4 3
5 3
1 2 3 4 5
10 9 10 10 9
4 0
2 2 4 3
2 4 2 3
4 4
1 2 2 1
4 4 5 4
output
27
39
11
17
NoteIn the first test case of the example, you can swap a1=1a1=1 and b2=4b2=4, so a=[4,2]a=[4,2] and b=[3,1]b=[3,1].In the second test case of the example, you don’t need to swap anything.In the third test case of the example, you can swap a1=1a1=1 and b1=10b1=10, a3=3a3=3 and b3=10b3=10 and a2=2a2=2 and b4=10b4=10, so a=[10,10,10,4,5]a=[10,10,10,4,5] and b=[1,9,3,2,9]b=[1,9,3,2,9].In the fourth test case of the example, you cannot swap anything.In the fifth test case of the example, you can swap arrays aa and bb, so a=[4,4,5,4]a=[4,4,5,4] and b=[1,2,2,1]b=[1,2,2,1].

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define LL long long
using namespace std;
int a[35],b[35];
int main()
{
 int t,n,k;
 cin>>t;
 while(t--){
  cin>>n>>k;
  LL sum=0;
  for(int i=0;i<n;i++) cin>>a[i];
  for(int i=0;i<n;i++) cin>>b[i];
  sort(a,a+n);
  sort(b,b+n);
  for(int i=0;i<k;i++)
  {
   if(b[n-1-i]>a[i]) a[i]=b[n-1-i];
   else continue;
  }
  for(int i=0;i<n;i++)
  sum+=a[i];
  cout<<sum<<endl;
 }
 return 0;
}

C. Board Movestime

limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a board of size n×nn×n, where nn is odd (not divisible by 22). Initially, each cell of the board contains one figure.In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i,j)(i,j) you can move the figure to cells:(i−1,j−1)(i−1,j−1);(i−1,j)(i−1,j);(i−1,j+1)(i−1,j+1);(i,j−1)(i,j−1);(i,j+1)(i,j+1);(i+1,j−1)(i+1,j−1);(i+1,j)(i+1,j);(i+1,j+1)(i+1,j+1);Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. n2−1n2−1 cells should contain 00 figures and one cell should contain n2n2 figures).You have to answer tt independent test cases.InputThe first line of the input contains one integer tt (1≤t≤2001≤t≤200) — the number of test cases. Then tt test cases follow.The only line of the test case contains one integer nn (1≤n<5⋅1051≤n<5⋅105) — the size of the board. It is guaranteed that nn is odd (not divisible by 22).It is guaranteed that the sum of nn over all test cases does not exceed 5⋅1055⋅105 (∑n≤5⋅105∑n≤5⋅105).OutputFor each test case print the answer — the minimum number of moves needed to get all the figures into one cell.
Example
input
3
1
5
499993
output
0
40
41664916690999888

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdio>
#define LL long long
using namespace std;
int main()
{
 LL t,n;
 cin>>t;
 while(t--){
  cin>>n;
  if(n==1) {cout<<"0"<<endl;continue;} 
  LL sum=0,m=n/2;
  for(LL i=1;i<=m;i++)
   sum=sum+i*i;
  sum=sum*8;
  cout<<sum<<endl;
 }
 return 0;
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值