P1102题库链接:https://www.luogu.org/problem/P1102

难度:普及-

算法标签:模拟,数论,排序,HASH,概率论,二分查找

1.朴素 O(n^2) 得分76

将输入所有的数依次作为被减数,除此数外其他数依次作为减数,每当有一组的差为1时,方案数ans + 1

 #include <cstdio>
using namespace std;
int main()
{
int n, c, s[], ans = ;
scanf("%d%d", &n, &c);
for(int i = ; i < n; ++i)
scanf("%d", &s[i]);
for(int i = ; i < n; ++i)
{
for(int j = ; j < n; ++j)
{
if(i == j) continue;
if(s[i] - s[j] == c) ++ans;
}
}
printf("%d\n", ans);
return ;
}

2.桶优化 O(n) 得分84

每输入一个数,所对应的桶增加,因为A - B = C -> A - C = B,枚举每一种A的可能值,从而用A - C求出B,若A,B存在(即桶不为0),则方案数ans += t[A] * t[B]

 #include <cstdio>
using namespace std;
int main()
{
int n, c, ans = , t[] = {};
scanf("%d%d", &n, &c);
for(int i = ; i < n; ++i)
{
int x;
scanf("%d", &x);
++t[x];
}
for(int i = c; i <= ; ++i)
if(t[i] != && t[i - c] != )
ans += t[i] * t[i - c];
printf("%d\n", ans);
return ;
}

3.map优化 AC

 #include <cstdio>
#include <map>
using namespace std;
long long a[];
map<long long, long long> m;
int main()
{
long long n, c, ans = ;
scanf("%lld%lld", &n, &c);
for(int i = ; i < n; ++i)
{
scanf("%lld", &a[i]);
++m[a[i]];
a[i] -= c;
}
for(int i = ; i < n; ++i)
ans += m[a[i]];
printf("%lld\n", ans);
return ;
}

最新文章

  1. js语言精粹读书笔记一
  2. WCF 套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的
  3. NeuSoft(4)编写字符设备驱动
  4. js isnull 赋值方法
  5. javascript跨域通信(二):window.name实现的跨域数据传输
  6. CSS文档流与块级元素和内联元素
  7. 了解Unix进程(1)
  8. QT 的信号与槽
  9. [jQuery编程挑战]004 针对选择框词典式排序
  10. cocos2d-x中常见的场景切换
  11. //相当于深拷贝一份dataArray。这样才不会改变dataArray本身的值
  12. 利用PYTHON设计计算器功能
  13. ⑦bootstrap按钮 图片 辅助使用基础案例
  14. Ubuntu18.04格式化U盘为NTFS的方法
  15. C++ 常用算法
  16. 矿难让显卡压了那么多货咋办?NV如是说
  17. [转帖] 一文看懂:&quot;边缘计算&quot;究竟是什么?为何潜力无限?
  18. dart --- 更符合程序员编程习惯的javascript替代者
  19. js准确获取当前页面url网址信息
  20. MySQL 事务 是对数据进行操作,对结构没有影响,比如创建表、删除表,事务就不起作用

热门文章

  1. Django框架的前奏(安装及介绍)
  2. 【转】浅述WinForm多线程编程与Control.Invoke的应用
  3. bzoj5104 Fib数列(BSGS+二次剩余)
  4. [学习笔记]set的使用
  5. 11)const
  6. Uncaught TypeError: Cannot read property &#39;querySelector&#39; of null
  7. 估计量|估计值|置信度|置信水平|非正态的小样本|t分布|大样本抽样分布|总体方差|
  8. numpy模块介绍
  9. [LC] 404. Sum of Left Leaves
  10. 2019ICPC南京网络赛B super_log(a的b塔次方)