CF1447B Numbers Box

Posted 诸葛阵御

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF1447B Numbers Box相关的知识,希望对你有一定的参考价值。

题目描述

You are given a rectangular grid with nn rows and mm columns. The cell located on the ii -th row from the top and the jj -th column from the left has a value a_ijaij​ written in it.

You can perform the following operation any number of times (possibly zero):

  • Choose any two adjacent cells and multiply the values in them by -1−1 . Two cells are called adjacent if they share a side.

Note that you can use a cell more than once in different operations.

You are interested in XX , the sum of all the numbers in the grid.

What is the maximum XX you can achieve with these operations?

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt ( 1 \\le t \\le 1001≤t≤100 ). Description of the test cases follows.

The first line of each test case contains two integers nn , mm ( 2 \\le n2≤n , m \\le 10m≤10 ).

The following nn lines contain mm integers each, the jj -th element in the ii -th line is a_ijaij​ ( -100\\leq a_ij\\le 100−100≤aij​≤100 ).

输出格式

For each testcase, print one integer XX , the maximum possible sum of all the values in the grid after applying the operation as many times as you want.

题意翻译

给定一个 n 行 m 列 (2≤n,m≤10) 的整数矩阵 a,第 i 行第 j 列的元素 ai,j​ 满足 (−100≤ai,j​≤100)。

你可以进行任意次操作,对于每次操作,你可以将两个相邻的方格上的元素一起乘上 −1。

注意:当且仅当两个方格有公共,我们认为两个方格是相邻的。

有 t(1≤t≤100) 组数据。

Translated by Error_Eric.

输入输出样例

输入 #1复制

2
2 2
-1 1
1 1
3 4
0 -1 -2 -3
-1 -2 -3 -4
-2 -3 -4 -5

输出 #1复制

2
30

说明/提示

In the first test case, there will always be at least one -1−1 , so the answer is 22 .

In the second test case, we can use the operation six times to elements adjacent horizontally and get all numbers to be non-negative. So the answer is: 2\\times 1 + 3\\times2 + 3\\times 3 + 2\\times 4 + 1\\times 5 = 302×1+3×2+3×3+2×4+1×5=30 .

思路

不要被“相邻的方格”所束缚住,若

 -1  1  1  -1

>1  -1  1  -1

>1  1  -1  -1

>1   1  1  1

-1  1  1

1   1   1

1   1   -1

>1  -1  1

 1   1   1

 1   1   -1

>1     1

  1   -1  1

  1    1  -1

> 1   1   1

   1   1    1

   1   -1   -1

> 1   1   1

   1    1   1

   1    1    1

可跳格*-1,此情况下只需考虑负数个数即可

若为偶数,则令其全为正数即可取最大值

若为奇数,则令其绝对值最小的数为负,其余为正,即可取最大值

代码

#include <stdio.h>
#include <iostream>
#include <algorithm>
using namespace std;
int cmp(int a,int b) 
	return a<b;

int main() 
	int n;
	cin>>n;
	for (int i=0;i<n;i++) 
		int n,m;
		cin>>n>>m;
		int a[n*m+3];	//无需二维,只需知道负数个数即可 
		int fu=0;
		for (int j=0;j<n*m;j++) 
			cin>>a[j];
			if (a[j]<0) 
				fu++;	//记录有多少个负数 
				a[j]=a[j]*-1;	//使负数变为整数 
			
		
		int sum=0;
		if (fu%2==0) 	//偶数个负数,即全部变为正相加即可 
			for (int j=0;j<n*m;j++) 
				sum=sum+a[j];
			
		 else 	//奇数个,令绝对值最小的数为负,其余为正相加即可 
			sort(a,a+n*m,cmp);
			a[0]=-1*a[0];
			for (int j=0;j<n*m;j++) 
				sum=sum+a[j];
			
		
		cout<<sum<<endl;
	
	return 0;

以上是关于CF1447B Numbers Box的主要内容,如果未能解决你的问题,请参考以下文章

CF1447B Numbers Box

CF1350E Orac and Game of Life(BFS)

CF1237F Balanced Domino Placements

cf 712E Memory and Casinos

CF401D Roman and Numbers

[CF403D]Beautiful Pairs of Numbers