SPOJ:Just One Swap(统计&思维)
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(统计&思维)的更多相关文章
- SPOJ - COT2 离线路径统计
题意:求\(u\)到\(v\)的最短路径的不同权值种类个数 树上莫队试水题,这一篇是上篇的弱化部分,但可测试以下结论的正确性 设\(S(u,v)\):\(u-v\)最短路径所覆盖的点集 \(S(u,v ...
- [spoj DISUBSTR]后缀数组统计不同子串个数
题目链接:https://vjudge.net/contest/70655#problem/C 后缀数组的又一神奇应用.不同子串的个数,实际上就是所有后缀的不同前缀的个数. 考虑所有的后缀按照rank ...
- SPOJ:The Next Palindrome(贪心&思维)
A positive integer is called a palindrome if its representation in the decimal system is the same wh ...
- CentOS 6.5中安装使用dstat资源统计工具
目录 1 dstat工具的使用 1.1 什么是dstat 1.2 dstat的基本使用 1.3 检测界面各参数的含义 1.4 dstat 的高级用法 2 dstat工具的安装 2.1 (推荐)通过yu ...
- Analyzing the Analyzers 分析分析师 —— 数据科学部门如何建
很多牛逼的公司都宣称在建立数据科学部门,这个部门该如何组建,大家都在摸石头过河. O‘reilly Strata今年 六月份发布了报告 <Analyzing the Analyzers>, ...
- 零基础数据分析与挖掘R语言实战课程(R语言)
随着大数据在各行业的落地生根和蓬勃发展,能从数据中挖金子的数据分析人员越来越宝贝,于是很多的程序员都想转行到数据分析, 挖掘技术哪家强?当然是R语言了,R语言的火热程度,从TIOBE上编程语言排名情况 ...
- 进程管理工具htop/glances/dstat的使用
进程管理工具htop/glances/dstat的使用 Linux中进程的相关知识 1.什么是进程呢? 通俗的来说进程是运行起来的程序.唯一标示进程的是进程描述符(PID). 2.进程的分类 1)根据 ...
- 学习ASP.NET Core Razor 编程系列六——数据库初始化
学习ASP.NET Core Razor 编程系列目录 学习ASP.NET Core Razor 编程系列一 学习ASP.NET Core Razor 编程系列二——添加一个实体 学习ASP.NET ...
- 使用 ASP.NET Core MVC 创建 Web API(二)
使用 ASP.NET Core MVC 创建 Web API 使用 ASP.NET Core MVC 创建 Web API(一) 六.添加数据库上下文 数据库上下文是使用Entity Framewor ...
随机推荐
- XV6第一个进程
第一个进程 本章通过第一个进程的创建来解释 xv6 是如何开始运行的,让我们得以一窥 xv6 提供的各个抽象是如何实现和交互的.xv6 尽量复用了普通操作的代码来建立第一个进程,避免单独为其撰写代码. ...
- 67. @Transactional的类注入失败【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] Spring的代理模式有两种:java自带的动态代理模式和cglib代理模式,cglib代码模式适用于没有接口的类,而java自带适用于接口类,默 ...
- hihoCoder #1055 : 刷油漆 [ 树形dp ]
传送门 结果:Accepted 提交时间:2015-05-11 10:36:08 #1055 : 刷油漆 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 上回说到 ...
- poj2773求第K个与m互质的数
//半年前做的,如今回顾一下,还是有所收货的,数的唯一分解,.简单题. #include<iostream> #include<cstring> using namespace ...
- SQL SERVER 2012 第五章 创建和修改数据表 の SQL SERVER中的对象名
[ServerName.[DataBaseName.[SchemeName.]]]ObjectName 服务器名,数据库名,模式名,对象名 其中模式是一个新出的坑爹的东西.
- Codeforces 659A Round House【水题,细节】
题目链接: http://codeforces.com/contest/659/problem/A 题意: 一个圈,按逆时针编号,给定起点,方向和步数,问终点在几号? 分析: 很简单的模拟...注意答 ...
- hihocoder 1579(排列组合)
题意 给出一个长度为n的字符串的sa数组,n<=1e5,问有多少种不同的字符串的sa数组正好是输入的sa数组(字符串每个位置都是小写字母) 分析 sa数组描述的是字符的大小关系,而不是确切的字符 ...
- 在d盘中创建一个文件夹 在文件夹里创建三个txt文本
import java.io.File; import java.io.IOException; public class FileDemo { public static void main(Str ...
- HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和)
HDU 1588 Gauss Fibonacci(矩阵高速幂+二分等比序列求和) ACM 题目地址:HDU 1588 Gauss Fibonacci 题意: g(i)=k*i+b;i为变量. 给出 ...
- GAN Generative Adversarial Network 生成式对抗网络-相关内容
参考: https://baijiahao.baidu.com/s?id=1568663805038898&wfr=spider&for=pc Generative Adversari ...