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. C#_delegate - 用委托实现事件,Display和Log类都使用Clock对象

    //public event SecondChangeHandler OnSecondChange; 若将委托加上event,则视作是事件,不是委托,外围就不能直接对OnSecondChange传值 ...

  2. php 获取远程图片

    一 function gethttpimage($url){      set_time_limit(0);      if(!empty($url)){        $imgUrl=date('Y ...

  3. [引]LINQ to XML 类概述

    本文转自:http://msdn.microsoft.com/zh-cn/library/bb387023.aspx 本主题提供 System.Xml.Linq 命名空间中 LINQ to XML 类 ...

  4. mongodb用mongoose取到的对象不能增加属性

    先定义了一个article的schema var mongoose = require('mongoose'); var Schema = mongoose.Schema; exports.schem ...

  5. Android, JSONLIB , java.lang.NoClassDefFoundError: Failed resolution of: Lnet/sf/json/JSONArray; 原因

    出现这个错误的原因是因为引用的lib库的V4包与程序的V4包不兼容,替换成一致的包就OK了

  6. dedecms 知识点总结

    生成-更新主页html 将自定义字段在arclist调用:   内容模型管理==>频道模型管理==>增加新字段==>列表处理:  使字段可以在列表的底层模板中获得(自定义字段默认仅能 ...

  7. php入门实现留言板

    首先由一个文本文档read.txt liulan.html <!doctype html> <html lang="en"> <head> &l ...

  8. openlayers wfs获取要素

    var wfsProtocol = new OpenLayers.Protocol.WFS.v1_1_0({ url: mapServerUrl + "/wfs", feature ...

  9. Extjs4使用iframe注意事项

    "video" : { render : function(panel, eOpts) { // 因为iframe在video // panel渲染的时候就已经完全移动到video ...

  10. Sophos UTM WebAdmin存在未明漏洞

                                                                                                        ...