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. Hibernate: Truly Understanding the Second-Level and Query Caches--reference

    I've written multiple articles here at Javalobby on the Hibernate O/R mapping tool, and they are usu ...

  2. C#解决微信支付Exception has been thrown by the target of an invocation(调用的目标发生了异常)的问题

    今天搭建微信扫码支付环境的时候,一样的配置参数,调用连接提示错误 错误:调用的目标发生了异常 然后跟踪到执行 MD5 md5 = System.Security.Cryptography.MD5.Cr ...

  3. HTTPS证书制作

    openssl 安装可参照之前文章 1.mkdir /etc/ssl/xip.ioopenssl genrsa -out /etc/ssl/xip.io/xip.io.key 1024openssl ...

  4. 用expect做自动应答脚本

    Expect是一个用来实现自动交互功能的软件套件 (Expect [is a] software suite for automating interactive tools).使用它系统管理员可以创 ...

  5. C#一些小知识点

    1. 在Load时候由代码来做控件PictureBox,并且用代码将图片加载进去: private void Form2_Load(object sender, EventArgs e) { Dire ...

  6. Thinkphp 使用gmail发送邮件

    1.Thinkphp 发送邮件内容来自:http://www.thinkphp.cn/code/32.html /** * 系统邮件发送函数 * @param string $to 接收邮件者邮箱 * ...

  7. Android M Developer Preview - API Preview(一)

    API Overview The M Developer Preview gives you an advance look at the upcoming release for the Andro ...

  8. android N刷机

    1. 打开开发者选项2. 在开发者选项页面打开 oem解锁3. 运行 adb reboot bootloader4. 运行 fastboot flashing unlock5. 解压你下载的刷机包 6 ...

  9. xmlDoc.SelectNodes用法(获取不到节点时注意事项)

    注:以下举例仅针对xml自定义了命名空间的情况,如果是其他情况,请参照他人博客~ using System;using System.Collections.Generic;using System. ...

  10. OpenJudge/Poj 1004 Financial Management

    1.链接地址: http://poj.org/problem?id=1004 http://bailian.openjudge.cn/practice/1004 2.题目: 总时间限制: 1000ms ...