B. The Best Gift
 传送门:http://codeforces.com/problemset/problem/609/B

  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:

  1. the first and second books,
  2. the first and third books,
  3. the first and fourth books,
  4. the second and third books,
  5. the third and fourth books.

没错我超时了···     test19肯定是个大数据,然而一时想不出什么方法来优化。占坑留着吧    日后再补

 #include <stdio.h>
#include <stdlib.h>
int books[];
int main(int argc, char *argv[])
{
int n,m;
__int64 ways;
while(scanf("%d%d",&n,&m)!=EOF)
{
ways=;
int i,j;
//for(i=0;i<n;i++)
//scanf("%d",&books[i]);
scanf("%d",&books[]);
for(i=;i<n;i++)
{
scanf("%d",&books[i]);
for(j=;j<i;j++)
{
if(books[i]!=books[j])
ways++;
}
}
printf("%I64d\n",ways);
}
return ;
}

今天上cf看了看别人的代码,用数学方法AC了···

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int a[],b[];//b组用来记录书的种类
int main(int argc, char *argv[])
{
int n,m;
int i,count;
int k; while(scanf("%d%d",&n,&m)!=EOF)
{
memset(b,,sizeof());
k=;
for(i=;i<n;i++)
{
scanf("%d",&a[i]);
b[a[i]]++; //相应种类的书籍数目++
}
count=n;
for(i=;i<=m;i++)
{
count=count-b[i];
k+=b[i]*count;//b[i]种类书的数量乘上剩下其他种类书籍的数量
}
printf("%d\n",k);
}
return ;
}

codeforces edu round3的更多相关文章

  1. Codeforces AIM Tech Round3

    打得最烂一场Codeforces,多次都错题,无限WA... A题: 题意:给定n个橘子的大小,大小超过b的丢掉,不足d的补充进来,同时超过d的部分去掉,问要去掉几次 分析:直接模拟即可 #inclu ...

  2. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  3. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  4. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  5. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  6. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  7. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  8. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

  9. CodeForces - 696B Puzzles

    http://codeforces.com/problemset/problem/696/B 题目大意: 这是一颗有n个点的树,你从根开始游走,每当你第一次到达一个点时,把这个点的权记为(你已经到过不 ...

随机推荐

  1. Android(java)学习笔记128:使用proguard混淆android代码

    1.当前是有些工具比如apktool,dextojar等是可以对我们android安装包进行反编译,获得源码的.为了减少被别人破解,导致源码泄露,程序被别人盗取代码,等等.我们需要对代码进行混淆,an ...

  2. c/c++将整数转换为字符串

    #include <iostream> using namespace std; int main(int argc, char **argv) { ; iint i,j; ],e[]; ...

  3. Elasticsearch aggregations API

    聚合能力 Aggregation API 类似 SQL 中的 GROUP BY 语句,可以以某个字段来进行分组. Aggregation API 支持分级分组,多级的分组过程是由外到里的. Aggre ...

  4. kali linux 2.0 折腾笔记

    1. 配置SSH远程登录 root@kali:~# vi /etc/ssh/sshd_config #PermitRootLogin without-password PermitRootLogin ...

  5. [转]Easy Stored Procedure Output Oracle Select

    本文转自:http://www.oraclealchemist.com/oracle/easy-stored-procedure-output/ I answered a question on a ...

  6. [转]JSON序列化与反序列化

    本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/04/09/1708084.html 方法一:引入System.Web.Script.Serializ ...

  7. JAXB - Annotations, The Object Factory: XmlRegistry, XmlElementDecl

    To be able to create objects from XML elements, the unmarshaller must have an object factory with me ...

  8. 使用VERT.X构建分布式企业级应用

    谈到企业应用,就得谈分布式.低耦合.模块化.面向服务.可扩展性等等.早些时候的技术有CORBA和EJB,后面兴起的有WebService和MDB.但是这些技术不是学习.开发门槛高就是不那么轻量化.我现 ...

  9. vs2010打开vs2012的sln文件

    1.找到**.sln文件,然后选择用记事本打开. 2.最前面找到“Microsoft Visual Studio Solution File, Format Version 12.00  # Visu ...

  10. 几个linux命令

    常用linux命令: 普通用户命令: 一.文件和目录查看类命令 1. ls (常用参数 -l 和-h) 蓝颜色:表示目录 绿颜色:表示可执行文件 红颜色:表示压缩文件 白颜色:表示普通文件 青色:表示 ...