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 ...
随机推荐
- CD(01背包)
You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is o ...
- 【Ts 1】 maven初识
PS:本篇博客,就是对于maven的一个简单的总结,认识.可能更多的是借鉴别人的看法,然后结合自己的使用,再加以说明. 首先,什么是maven: Apache Maven is a software ...
- react.js 父子组件数据绑定实时通讯
import React,{Component} from 'react' import ReactDOM from 'react-dom' class ChildCounter extends Co ...
- ubuntu 12.04 64bit 安装 teamviewer 8.0
1. 在http://www.teamviewer.com下载teamviewer_linux_x64.deb 2.sudo dpkg -i teamviewer_linux_x64.deb 3.如果 ...
- 蓝桥杯 算法提高 金属采集 [ 树形dp 经典 ]
传送门 算法提高 金属采集 时间限制:1.0s 内存限制:256.0MB 锦囊1 锦囊2 锦囊3 问题描述 人类在火星上发现了一种新的金属!这些金属分布在一些奇怪的地方,不妨叫 ...
- Linux 端口开放
Linux(CentOS): 系统缺省值为32768-61000. 修改方法: 在/etc/sysctl.conf中,增加以下配置:(开放20000-50000为完成端口) net.ipv4.ip_l ...
- python学习之-- redis模块管道/订阅发布
redis 模块操作剩余其他常用操作 delete(*names):删除任意的数据类型exists(name):检测redis的name是否存在keys(pattern='*'):根据模型获取redi ...
- POJ 2488 A Knight's Journey【DFS】
补个很久之前的题解.... 题目链接: http://poj.org/problem?id=2488 题意: 马走"日"字,让你为他设计一条道路,走遍所有格,并输出字典序最小的一条 ...
- c标准库函数 strcat
函数原型:extern char *strcat(char *dest,char *src) 参数说明:dest为一个目的字符串的指针,即被连接的字符串(在前),src为一个源字符串的指针(在后).所 ...
- [CERC2015]Digit Division
题目描述 We are given a sequence of n decimal digits. The sequence needs to be partitioned into one or m ...