文章目录

  • 1. Not Adjacent Matrix
    • Input
    • Output
    • Example
    • 代码
  • 2. Same Differences
    • Input
    • Output
    • Example
    • 思路
    • 代码
  • 3. Arranging The Sheep
    • Input
    • Output
    • Example
    • 思路
    • 代码

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;
*/

Not Adjacent Matrix、Same Differences、Arranging The Sheep相关推荐

  1. R语言数据格式转换函数、数据类型判断函数(numeric、character、vector、matrix、data.frame、factor、logical)、R语言数据格式类型转换

    R语言数据格式转换函数.数据类型判断函数(numeric数值.character字符串.vector向量.matrix矩阵.data.frame数据表.factor因子.logical逻辑).R语言数 ...

  2. R语言使用upper.tri函数、lower.tri函数、diag函数改变matrix矩阵上三角形、下三角形、对角线的数值

    R语言使用upper.tri函数.lower.tri函数.diag函数改变matrix矩阵上三角形.下三角形.对角线的数值 目录

  3. LearnGL - 06.1 - Matrix - 矩阵02 - 向量空间、向量空间的维度、为何矩阵乘法要有 [M x N] * [N * P] 的 N 要相等的限制

    文章目录 矩阵乘法为何有维度限制? 什么是向量空间? 什么是向量空间的维度? 理解为何有这个限制 LearnGL - 学习笔记目录 本人才疏学浅,如有什么错误,望不吝指出. 上一篇:LearnGL - ...

  4. Android Paint、Canvas、Matrix使用讲解(一、Paint)

    http://blog.csdn.net/tianjian4592/article/details/44336949 好了,前面主要讲了Animation,Animator 的使用,以及桌面火箭效果和 ...

  5. 马扎克 MAZAK CNC数据采集smart、smooth(smooth-c、smooth-g、smooth-x)、matrix(nexu、nexu2)以及640(640m、640mn、640t)系列

    马扎克公司主要生产CNC车床.复合车铣加工中心.立式加工中心.卧式加工中心. 目前整个设备联网行业比较火,然而马扎克的开通MT协议的高昂费用(8k左右)却让很多公司望而却步, 目前可以采用最优质的解决 ...

  6. Android 绘图基础:Bitmap(位图)与Matrix(矩阵)实现图片5种操作(平移、旋转、错切、缩放、对称)

    Android的Matrix利用数学原理实现图片平移.旋转等操作详解 很庆幸自己的线性代数学的还可以,理解Matrix的矩阵变换完全没有问题.Matrix矩阵实现图片的平移旋转等操作涉及到线性代数问题 ...

  7. R语言使用crossprod函数和tcrossprod函数计算矩阵matrix交叉积(Matrix Cross Product)、crossprod函数、tcrossprod函数计算矩阵和向量的交叉积

    R语言使用crossprod函数和tcrossprod函数计算矩阵matrix交叉积(Matrix Cross Product).crossprod函数.tcrossprod函数计算矩阵和向量的交叉积 ...

  8. 矩阵(Matrix)实现数乘、矩阵加法、矩阵乘法以及行列式的计算。

    [问题描述] 矩阵是线性代数中的重要概念,应用领域非常广泛,在C/C++中,通常将矩阵定义为一个二维数组.本问题中,将输入两个矩阵 A 和 B,实现对矩阵的数乘.矩阵加法.矩阵乘法以及行列式的计算.如 ...

  9. Eigen入门系列 —— Eigen::Matrix矩阵点乘、叉乘、转置、求逆、求和、行列式、迹、数乘

    Eigen入门系列 -- Eigen::Matrix矩阵点乘.叉乘.转置.求逆.求和.行列式.迹.数乘 前言 程序说明 输出结果 代码示例 前言 随着工业自动化.智能化的不断推进,机器视觉(2D/3D ...

  10. Paint、Canvas、Matrix使用讲解(一、Paint)

    好了,前面主要讲了Animation,Animator 的使用,以及桌面火箭效果和水波纹效果,分别使用android框架和自己绘制实现,俗话说,工欲善其事,必先利其器,接下来几篇文章主要讲绘制中我们需 ...

最新文章

  1. 计算机二级能学到知识吗,2017年关于计算机二级msoffice学习知识点
  2. UnboundLocalError: local variable ‘XXX‘ referenced before assignment解决办法
  3. 设置textview背景色为透明
  4. VC控件 Progress Control
  5. 机器学习之线性回归 (Python SKLearn)
  6. Sharepoint学习笔记—架构系列
  7. 图片加载框架---UniversalImageLoader使用(一)
  8. iphone备忘录突然没了_为什么用过iPhone的人都不再想换回安卓?网友:过于真实,哭了...
  9. 《Web性能实践日志》一1.5 浏览器的发展
  10. centos6.9下rpm方式安装mysql后mysql服务无法启动
  11. 部署calico网络的k8s集群
  12. 洛谷P1322 logo语言
  13. guass法matlab
  14. 74ls175四人抢答器电路图_四人智力竞赛抢答器电路原理及设计.doc
  15. Ai作图工业化流程工具
  16. 课时31:永久储存:腌制一缸美味的泡菜
  17. 零基础学 Python 有什么建议?
  18. AMBA总线—APB总线协议详解
  19. Pangu and Stones (hihocoder 1636)
  20. 家用计算机防火墙设置,电脑防火墙设置【解决思路】

热门文章

  1. 自动驾驶 Apollo 源码分析系列,感知篇(三):红绿灯检测和识别
  2. 混沌matlab仿真
  3. js calendar / wannianli
  4. 2019 东北四省赛部分题解 The 13th Chinese Northeast Collegiate Programming Contest
  5. 第三章:Servlet、ServletConfig、ServletContext
  6. 2020叉车司机考试及叉车司机模拟考试题库
  7. day4-数字类型和列表基础
  8. 求不变矩matlab,求HU不变矩七个参数
  9. 王森:程序设计师真情忏悔录
  10. 三国演义亲和度python_用python分析四大名著之三国演义