Not Adjacent Matrix、Same Differences、Arranging The Sheep

1. Not Adjacent Matrix

We will consider the numbers a and b as adjacent if they differ by exactly one, that is, |a−b|=1.

We will consider cells of a square matrix n×n as adjacent if they have a common side, that is, for cell (r,c) cells (r,c−1), (r,c+1), (r−1,c) and (r+1,c) are adjacent to it.

For a given number n, construct a square matrix n×n such that:

Each integer from 1 to n2 occurs in this matrix exactly once;
If (r1,c1) and (r2,c2) are adjacent cells, then the numbers written in them must not be adjacent.

Input

The first line contains one integer t (1≤t≤100). Then t test cases follow.

Each test case is characterized by one integer n (1≤n≤100).

Output

For each test case, output:

-1, if the required matrix does not exist;
the required matrix, otherwise (any such matrix if many of them exist).
The matrix should be outputted as n lines, where each line contains n integers.

Example

Input
3
1
2
3
Output
1
-1
2 9 7
4 6 3
1 8 5

代码

#include "stdio.h"
#include "string.h"
int main()
{
    
    
	int i,m,n,j,k,l;
	scanf("%d",&l);
	while(l--)
	{
    
    
		k=1;
		scanf("%d",&n);
		if(n==1)
		printf("1\n");
		else if(n==2)
		printf("-1\n");
		else
		{
    
    
			int a[n+3][n+3];
			for(i=1;i<=n;i++)
			{
    
    
				for(j=1;j<=n;j++)
				{
    
    
					if((i+j)%2==0)
					{
    
    
						a[i][j]=k++;
					}
					
				}
			}
			for(i=1;i<=n;i++)
			{
    
    
				for(j=1;j<=n;j++)
				{
    
    
					if((i+j)%2)
					a[i][j]=k++;
				}
			}
			for(i=1;i<=n;i++)
			{
    
    
				for(j=1;j<=n;j++)
				{
    
    
					printf("%d ",a[i][j]);
				}
				printf("\n");
			}
		}
	}
}
/*n=3时
有三个大的for循环
第一个相邻的数字不要放东南西北四个方向,那我们就不先填东南西北四个方向
1 ? 2
?3 ?
4 ? 5

第二个
1 6 2
7 3 8
4 9 5
第三个输出 */ 

2. Same Differences

You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and aj−ai=j−i.

Input

The first line contains one integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2⋅105).

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.

Output

For each test case output the number of pairs of indices (i,j) such that i<j and aj−ai=j−i.

Example

Input
4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6
Output
1
3
3
10

思路

aj−ai=j−i.转化成: ai-i=bj-j;

代码

#include "stdio.h"
#include "algorithm"
using namespace std;
#include "map"
#define ll long long
int main()
{
    
    
	ll i,m,n,j,k,l;
	scanf("%lld",&n);
	while(n--)
	{
    
    
		k=0;
		map<ll,ll>p;
		scanf("%lld",&m);
		for(i=1;i<=m;i++)
		{
    
    
			scanf("%lld",&j);
			p[j-i]++;//map开出的q,相当于book数组,但book不能标记负数 
		}
		for(i=-m;i<=m;i++)
			if(p[i]>1)
				k+=p[i]*(p[i]-1)/2;
		printf("%lld\n",k);
	}
}

3. Arranging The Sheep

You are playing the game “Arranging The Sheep”. The goal of this game is to make the sheep line up. The level in the game is described by a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep). In one move, you can move any sheep one square to the left or one square to the right, if the corresponding square exists and is empty. The game ends as soon as the sheep are lined up, that is, there should be no empty cells between any sheep.

For example, if n=6 and the level is described by the string “**.*…”, then the following game scenario is possible:

the sheep at the 4 position moves to the right, the state of the level: “.";
the sheep at the 2 position moves to the right, the state of the level: "
...";
the sheep at the 1 position moves to the right, the state of the level: ".
..";
the sheep at the 3 position moves to the right, the state of the level: ".
..";
the sheep at the 2 position moves to the right, the state of the level: "…
*.”;
the sheep are lined up and the game ends.
For a given level, determine the minimum number of moves you need to make to complete the level.

Input

The first line contains one integer t (1≤t≤104). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤106).

The second line of each test case contains a string of length n, consisting of the characters ‘.’ (empty space) and ‘*’ (sheep) — the description of the level.

It is guaranteed that the sum of n over all test cases does not exceed 106.

Output

For each test case output the minimum number of moves you need to make to complete the level.

Example

Input
5
6
**.*…
5


3
..
3

10
.
.**
Output
1
0
0
0
9

思路

到羊的中位数的距离最短

代码

#include "stdio.h"
#include "math.h"
int a[1999999];
char b[1999999];
int main() 
{
    
    
	int m,j,k,l,i;
	scanf("%d",&m);
	while(m--)
	{
    
    
		
		k=1;
		scanf("%lld",&l);
		scanf("%s",b);
		for(i=0;i<l;i++)
		{
    
    
			if(b[i]=='*')
			a[k++]=i+1;
		}
		long long mid=a[(k/2)];
		long long n=0;//要用long long不然过不去
		for(i=1;i<k;i++)
		{
    
    
			n+=abs(mid-a[i])-abs(k/2-i);
		}
		printf("%lld\n",n);
	}
}
/*
到羊的中位数的距离最短 
序号 1 2 3 4 5 6 7 8 9 10 
	 * . * . . . * . * *
看出羊的中位数为 7(共5只羊,第三只羊处的位子为中位数) 
abs(mid-a[i]):每只羊到羊的中位数的距离,分别为
6 4 0 2 3
又因为不是每只羊都要到中位数,(中位数只能有一只羊)
故五只羊离中位数的距离分别为
2 1 0 1 2
就是上面的abs(k/2-i)
k/2是羊数的中位数 3
i是第几只羊 
则abs(mid-a[i])-abs(k/2-i),五只羊分别为
4 3 0 1 1
故答案为 9;
*/ 

猜你喜欢

转载自blog.csdn.net/weixin_53623850/article/details/116737541