CodeForces 609B The Best Gift
2016-01-25 15:33
417 查看
B. The Best Gift
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books
on sale from one of m genres.
In the bookshop, Jack decides to buy two books of different genres.
Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.
The books are given by indices of their genres. The genres are numbered from 1 to m.
Input
The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10)
— the number of books in the bookstore and the number of genres.
The second line contains a sequence a1, a2, ..., an,
where ai (1 ≤ ai ≤ m)
equals the genre of the i-th book.
It is guaranteed that for each genre there is at least one book of that genre.
Output
Print the only integer — the number of ways in which Jack can choose books.
It is guaranteed that the answer doesn't exceed the value 2·109.
Sample test(s)
input
output
input
output
Note
The answer to the first test sample equals 5 as Sasha can choose:
the first and second books,
the first and third books,
the first and fourth books,
the second and third books,
the third and fourth books.
思路:简单题目,找出所有书的种类数量,然后进行两两排列就行了
AC代码如下:
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Emily's birthday is next week and Jack has decided to buy a present for her. He knows she loves books so he goes to the local bookshop, where there are n books
on sale from one of m genres.
In the bookshop, Jack decides to buy two books of different genres.
Based on the genre of books on sale in the shop, find the number of options available to Jack for choosing two books of different genres for Emily. Options are considered different if they differ in at least one book.
The books are given by indices of their genres. The genres are numbered from 1 to m.
Input
The first line contains two positive integers n and m (2 ≤ n ≤ 2·105, 2 ≤ m ≤ 10)
— the number of books in the bookstore and the number of genres.
The second line contains a sequence a1, a2, ..., an,
where ai (1 ≤ ai ≤ m)
equals the genre of the i-th book.
It is guaranteed that for each genre there is at least one book of that genre.
Output
Print the only integer — the number of ways in which Jack can choose books.
It is guaranteed that the answer doesn't exceed the value 2·109.
Sample test(s)
input
4 3 2 1 3 1
output
5
input
7 4 4 2 3 1 2 4 3
output
18
Note
The answer to the first test sample equals 5 as Sasha can choose:
the first and second books,
the first and third books,
the first and fourth books,
the second and third books,
the third and fourth books.
思路:简单题目,找出所有书的种类数量,然后进行两两排列就行了
AC代码如下:
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int maxn=20; int a[maxn]; int main(){ int n,m; while(cin>>n>>m){ memset(a,0,sizeof(a)); for(int i=0;i<n;i++){ int tmp; cin>>tmp; a[tmp]++; } int ans=0; for(int i=0;i<=m;i++){ for(int j=i+1;j<=m;j++){ ans+=a[i]*a[j]; } } cout<<ans<<endl; } return 0; }
相关文章推荐
- Unity 多场景打包
- final--java
- 设计模式之策略模式
- RHCE 学习笔记(14)- 文件系统 (1)
- Python with MYSQL - sytax problem
- 响应式布局设计
- web 序列化
- 对1001. A+B Format (20)的描述
- 静态资源独立域名发布
- int *p = new int[10]; int *p = new int(10); http://bbs.csdn.net/topics/340186719
- Jquery中trigger和triggerHandler的区别
- 三种服务器端动态开发语言的比较
- dojo中的事件处理
- JSONP详解
- arcgis10动态地图的制作
- 编程时间从1970年1月1号0点开始计算的原因
- dojo.connect用法(转)
- ArcEngine地理坐标和投影坐标的转换
- ArcGIS中的三种查询
- TIFF World File(TFW)格式说明