Playing in a Casino

B. Playing in a Casino

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

(有张图传不上来)

Galaxy Luck, a well-known casino in the entire solar system, introduces a new card game.

In this game, there is a deck that consists of n� cards. Each card has m� numbers written on it. Each of the n� players receives exactly one card from the deck.

Then all players play with each other in pairs, and each pair of players plays exactly once. Thus, if there are, for example, four players in total, then six games are played: the first against the second, the first against the third, the first against the fourth, the second against the third, the second against the fourth and the third against the fourth.

Each of these games determines the winner in some way, but the rules are quite complicated, so we will not describe them here. All that matters is how many chips are paid out to the winner. Let the first player's card have the numbers a1,a2,…,am�1,�2,…,��, and the second player's card — b1,b2,…,bm�1,�2,…,��. Then the winner of the game gets |a1−b1|+|a2−b2|+⋯+|am−bm||�1−�1|+|�2−�2|+⋯+|��−��| chips from the total pot, where |x||�| denotes the absolute value of x�.

To determine the size of the total pot, it is necessary to calculate the winners' total winnings for all games. Since there can be many cards in a deck and many players, you have been assigned to write a program that does all the necessary calculations.

Input

Each test consists of several test cases. The first line contains one integer t� (1≤t≤10001≤�≤1000) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n� and m� (1≤n⋅m≤3⋅1051≤�⋅�≤3⋅105) — the number of cards in the deck and the count of numbers on the one card.

Each of the following n� lines of the test case set contains m� integers ci,j��,� (1≤ci,j≤1061≤��,�≤106) — a description of the i�-th card.

It is guaranteed that the total n⋅m�⋅� in all tests does not exceed 3⋅1053⋅105.

Output

For each test case, print one number — the total amount of winnings from all games.

Example

input

Copy

 
 

3

3 5

1 4 2 8 5

7 9 2 1 4

3 8 5 3 1

1 4

4 15 1 10

4 3

1 2 3

3 2 1

1 2 1

4 2 7

output

Copy

50
0
31

Note

Consider the first test case.

In the game between the first and second player, the winner receives |1−7|+|4−9|+|2−2|+|8−1|+|5−4|=19|1−7|+|4−9|+|2−2|+|8−1|+|5−4|=19 chips.

In the game between the first and third player, the winner receives |1−3|+|4−8|+|2−5|+|8−3|+|5−1|=18|1−3|+|4−8|+|2−5|+|8−3|+|5−1|=18 in chips.

In the game between the second and third player, the winner receives |7−3|+|9−8|+|2−5|+|1−3|+|4−1|=13|7−3|+|9−8|+|2−5|+|1−3|+|4−1|=13 chips.

The total is 19+18+13=5019+18+13=50 chips.

 题意大概就是,给出一个矩阵,每一行的各个数与后边各行分别取差值,相加就是答案;

但是直接做肯定会超时;

思路:

首先将每一列的元素进行从小到大的排列;

for(int i = 0 ; i < m ; i ++)
    sort(v[i].begin(),v[i].end());

直接一次循环,根据这个数减去数的次数和这个数被减去的次数,答案加上这个值;

for(int i = 0 ; i < m ; i ++)
      for(int j = 0 ; j < n ; j ++)
            ans += (j - (n - 1 - j))* v[i][j];

代码:

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll T , n , m , k ;
vector<vector<int>> v;
int main(){
    cin >> T;
    while(T --){
        cin >> n >> m ;
        v.clear();
        v.resize(m + 5);
        for(int i = 0 ; i < n ; i ++)
            for(int j = 0 ; j < m  ; j ++){
                cin >> k;
                v[j].push_back(k);
            }
        for(int i = 0 ; i < m ; i ++)
            sort(v[i].begin(),v[i].end());
        ll ans = 0;
        for(int i = 0 ; i < m ; i ++)
            for(int j = 0 ; j < n ; j ++)
                ans += (j - (n - 1 - j))* v[i][j];
        cout << ans << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值