You are given an array of size N. How many distinct arrays can you generate by swapping two numbers for exactly once? The two selected numbers can be equal but their positions in the array must be different.

Input

The first line of the input contains a single integer T, denoting the number of test cases. Every test case starts with an integer N. The next line contains N integers, the numbers of the array.

Output

For each tescase output the answer in a single line.

Constraints:

1 <= T <= 5

1 <= Value of a number in the array <= 100000

2 <= N <= 100000

Example

Input:

1
5
2 3 2 3 3

Output:
7

You can generate the following arrays:

2 3 2 3 3

2 2 3 3 3

2 3 3 2 3

2 3 3 3 2

3 2 2 3 3

3 3 2 2 3

3 3 2 3 2

 

题意:问给定一个数论,问交换两个位置上的数,可以变成多少个新的数组(重复的只统计一次)。

思路:先考虑变后与原来数论不同:对于每个位置,可以和与此数不同的位置交换,每一种合法交换统计了两次,最后除二。

如果某个数出现的次数大于1,则可以产生与原数论相同的数列。ans++;

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn],num[maxn];
long long ans=;
int main()
{
int N,T,i,j; bool F;
scanf("%d",&T);
while(T--){
memset(num,,sizeof(num));
scanf("%d",&N); ans=; F=false;
for(i=;i<=N;i++){
scanf("%d",&a[i]);
num[a[i]]++;
}
for(i=;i<=N;i++){
if(num[a[i]]>) F=true;
ans+=(N-num[a[i]]);
}
ans>>=1LL;
if(F) ans++;
printf("%lld\n",ans);
}
return ;
}

SPOJ:Just One Swap(统计&思维)的更多相关文章

  1. SPOJ - COT2 离线路径统计

    题意:求\(u\)到\(v\)的最短路径的不同权值种类个数 树上莫队试水题,这一篇是上篇的弱化部分,但可测试以下结论的正确性 设\(S(u,v)\):\(u-v\)最短路径所覆盖的点集 \(S(u,v ...

  2. [spoj DISUBSTR]后缀数组统计不同子串个数

    题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...

  3. SPOJ:The Next Palindrome(贪心&思维)

    A positive integer is called a palindrome if its representation in the decimal system is the same wh ...

  4. CentOS 6.5中安装使用dstat资源统计工具

    目录 1 dstat工具的使用 1.1 什么是dstat 1.2 dstat的基本使用 1.3 检测界面各参数的含义 1.4 dstat 的高级用法 2 dstat工具的安装 2.1 (推荐)通过yu ...

  5. Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建

    很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...

  6. 零基础数据分析与挖掘R语言实战课程(R语言)

    随着大数据在各行业的落地生根和蓬勃发展,能从数据中挖金子的数据分析人员越来越宝贝,于是很多的程序员都想转行到数据分析, 挖掘技术哪家强?当然是R语言了,R语言的火热程度,从TIOBE上编程语言排名情况 ...

  7. 进程管理工具htop/glances/dstat的使用

    进程管理工具htop/glances/dstat的使用 Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID). 2.进程的分类 1)根据 ...

  8. 学习ASP.NET Core Razor 编程系列六——数据库初始化

    学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...

  9. 使用 ASP.NET Core MVC 创建 Web API(二)

    使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...

随机推荐

  1. 【HTML/XML 5】使用XSL给XML文档添加样式

    导读:上篇博客中以具体实例分析了HTML和XML在语义上的不同,但是,大家也都发现,XML表现出来的,并没有HTML那样直观或者说美观.其原因是因为XML的表现内容和表现形式被分离.它的表现形式有两种 ...

  2. NYOJ760-See LCS again,有技巧的暴力!

    See LCS again 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...

  3. BC#76.2DZY Loves Balls

    DZY Loves Balls  Accepts: 659  Submissions: 1393  Time Limit: 4000/2000 MS (Java/Others)  Memory Lim ...

  4. [luoguP1168]中位数(主席树+离散化)

    传送门 模板题一道,1A. ——代码 #include <cstdio> #include <algorithm> #define ls son[now][0], l, mid ...

  5. BZOJ 1022: [SHOI2008]小约翰的游戏John【anti-SG】

    Description 小约翰经常和他的哥哥玩一个非常有趣的游戏:桌子上有n堆石子,小约翰和他的哥哥轮流取石子,每个人取的时候,可以随意选择一堆石子,在这堆石子中取走任意多的石子,但不能一粒石子也不取 ...

  6. 【bzoj4260】 Codechef REBXOR trie树

    Input 输入数据的第一行包含一个整数N,表示数组中的元素个数. 第二行包含N个整数A1,A2,…,AN.     Output 输出一行包含给定表达式可能的最大值.   Sample Input ...

  7. 【ZJOI2017 Round1练习&BZOJ4766】D1T2 文艺计算姬(Prufer编码)

    题意:给定一个一边点数为n,另一边点数为m,共有n*m条边的带标号完全二分图K_{n,m},求其生成树个数 mod p. 100%的数据:1 <= n,m,p <= 10^18 思路:这是 ...

  8. 理解流方式上传和form表单上传

    流方式上传: $post_input = 'php://input'; $save_path = dirname( __FILE__ ); $postdata = file_get_contents( ...

  9. 【转】php 之 array_filter、array_walk、array_map的区别

    [转]php 之 array_filter.array_walk.array_map的区别 原文:https://blog.csdn.net/csdnzhangyiwei/article/detail ...

  10. EsAlert

    https://www.cnblogs.com/zhaishaomin/p/7417306.html https://blog.csdn.net/pujiaolin/article/details/5 ...