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. 说说log4cplus

    <C++ primer 第五版>已经翻了一段时间了,每天早上的班车上看一个小时.书是好书,可惜很多知识还是停留在表面上.每天除了翻书,一是也找到不合适的方法进一步深入,晚上看到新闻联播的老 ...

  2. Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

    1.kill掉列出的进程: [root@localhost mysql]# ps -a | grep -i mysql pts/ :: mysqld_safe pts/ :: mysqld <d ...

  3. Ant构建和部署项目(转)

    原文地址: http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html Ant是一个Apache基金会下的跨平台的构件工具,它可以实 ...

  4. 关于JFace中的进度条对话框(ProgressMonitorDialog类)

    在Windows操作系统中,最常用的进度条对话框就是文件复制时的弹出框,如果想让用户愉快的使用你开发 的软件,那么在执行某个较长时间的操作时候,就应该弹出一个进度条提示框,告诉用户程序正在做什么. 做 ...

  5. readint writeint

    inline int readint() { char c = getchar(); while(!isdigit(c)) c = getchar(); ; while(isdigit(c)) { x ...

  6. JAVA8永久代

    在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...

  7. 用js实现跳转提示页面

    效果图: 网页布局 <p>操作成功</p> <strong>5</strong><span>秒后回到主页</span><a ...

  8. SQL*PLUS中批量执行SQL语句

    SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...

  9. postgresql cast转换类型

    代码 CAST(aa as NUMERIC)

  10. IIS启用SSL

    安全套接字层 (SSL) 是一套提供身份验证.保密性和数据完整性的加密技术.SSL 最常用来在 Web 浏览器和 Web 服务器之间建立安全通信通道.它也可以在客户端应用程序和 Web 服务之间使用. ...