【题解】A-B
【问题描述】
出题是一件痛苦的事情!
题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的 A+B Problem,改用 A-B 了哈
哈!
好吧,题目是这样的:给出一串数以及一个数字 C,要求计算出所有 A-B=C 的数对的
个数。 (不同位置的数字一样的数对算不同的数对)
【输入】
第一行包括 2 个非负整数 N 和 C,中间用空格隔开。
第二行有 N 个整数,中间用空格隔开,作为要求处理的那串数。
【输出】
输出一行,表示该串数中包含的所有满足 A-B=C 的数对的个数。
【输入输出样例】
dec.in dec.out
4 1 3
1 1 2 3
先快排,比如2,3,4,6,7,要使差为1.
你发现3-2等于1,那么4-2一定是大于1的,所以要减成1,只有2后面那一号才有可能.
所以如果4-3等于1,那么6一定要从3后面那号开始减,于是这题就很简单了.
当然如果出现重复,还是需要记录一下.
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstdlib>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std; ifstream fin("dec.in");
ofstream fout("dec.out"); int cnt_shu=;
long long cha=;
long long shu[]={};
int cnt_cs[]={};
long long sze_jianshu=;
long long cs=; bool qqsort(long long a,long long b);
void xun(int sze_jian); bool qqsort(long long a,long long b){
if(a<b)return ;
return ;
} void xun(int sze_jian){
if(sze_jianshu==sze_jian)sze_jianshu++;
for(int x=sze_jianshu;x<=cnt_shu;x++){
if(shu[x]-shu[sze_jian]==cha){
cs+=cnt_cs[sze_jian]*cnt_cs[x];
sze_jianshu=x;
}
if(shu[x]-shu[sze_jian]<cha)sze_jianshu=x;
else break;
}
return;
} int main(int argc, char *argv[]) {
fin>>cnt_shu>>cha;
if(cha<)cha=-cha;
for(int x=;x<=cnt_shu;x++)fin>>shu[x];
sort(shu+,shu++cnt_shu,qqsort); int da=,gs=,num=shu[];
for(int x=;x<=cnt_shu+;x++){
if(shu[x]==num&&x!=cnt_shu+){
gs++;
continue;
}
shu[da]=num;
num=shu[x];
cnt_cs[da++]=gs;
gs=;
} if(cha==){
long long cs1=;
for(int x=;x<da;x++){
cs1+=(cnt_cs[x]*(cnt_cs[x]-))/;
}
fout<<cs1;
return ;
} sze_jianshu=;
for(int x=;x<da;x++){
xun(x);
}
fout<<cs;
return ;
}
【题解】A-B的更多相关文章
- 2016 华南师大ACM校赛 SCNUCPC 非官方题解
我要举报本次校赛出题人的消极出题!!! 官方题解请戳:http://3.scnuacm2015.sinaapp.com/?p=89(其实就是一堆代码没有题解) A. 树链剖分数据结构板题 题目大意:我 ...
- noip2016十连测题解
以下代码为了阅读方便,省去以下头文件: #include <iostream> #include <stdio.h> #include <math.h> #incl ...
- BZOJ-2561-最小生成树 题解(最小割)
2561: 最小生成树(题解) Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1628 Solved: 786 传送门:http://www.lyd ...
- Codeforces Round #353 (Div. 2) ABCDE 题解 python
Problems # Name A Infinite Sequence standard input/output 1 s, 256 MB x3509 B Restoring P ...
- 哈尔滨理工大学ACM全国邀请赛(网络同步赛)题解
题目链接 提交连接:http://acm-software.hrbust.edu.cn/problemset.php?page=5 1470-1482 只做出来四道比较水的题目,还需要加强中等题的训练 ...
- 2016ACM青岛区域赛题解
A.Relic Discovery_hdu5982 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- poj1399 hoj1037 Direct Visibility 题解 (宽搜)
http://poj.org/problem?id=1399 http://acm.hit.edu.cn/hoj/problem/view?id=1037 题意: 在一个最多200*200的minec ...
- 网络流n题 题解
学会了网络流,就经常闲的没事儿刷网络流--于是乎来一发题解. 1. COGS2093 花园的守护之神 题意:给定一个带权无向图,问至少删除多少条边才能使得s-t最短路的长度变长. 用Dijkstra或 ...
- CF100965C题解..
求方程 \[ \begin{array}\\ \sum_{i=1}^n x_i & \equiv & a_1 \pmod{p} \\ \sum_{i=1}^n x_i^2 & ...
- JSOI2016R3 瞎BB题解
题意请看absi大爷的blog http://absi2011.is-programmer.com/posts/200920.html http://absi2011.is-programmer.co ...
随机推荐
- jsp 页面通过jq处理默认 选中的项 数据是通过遍历显示
jsp页面循环显示里面是<a></a>或者<input> id 以什么开头的id,然后当你点击那个的时候就在那个上面添加样式 <div> <di ...
- 或许有一两点你不知的C语言特性
关键字篇 volatile关键字 鲜为人知的关键字之一volatile,表示变量是'易变的',之所以会有这个关键字,主要是消除编译优化带来的一些问题,看下面的代码 ; int b = a; int c ...
- ThinkPHP HTML标签代码和UBB互相转换
1.UBB 转为 HTML TP的扩展里面自带一个ubb方法,用这个方法就能把用户输入的ubb格式代码转换为HTML标签的代码.这里用到的基本知识就是正则表达式啦,今天先不讲正则表达式. 来看一下TP ...
- [Python笔记]第十六篇:web框架之Tornado
Tornado是一个基于python的web框架,xxxxx 安装 python -m pip install tornado 第一个Tornado程序 安装完毕我们就可以新建一个app.py文件,放 ...
- Meditation Guide
Meditation “Stop!!!” don’t we just scream[vi. 尖叫:呼啸:发出尖锐刺耳的声音:令人触目惊心 ] this in our minds when the da ...
- iOS中的隐式动画
隐式动画就是指 在 非 人为在代码中 定义动画 而系统却默认 自带 的动画 叫做隐式动画. 比如 改变 图层 的颜色 位置 和 透明度 的时候 都会 产生附带的渐变的 ...
- javascript 与 java
- data guard switchover切换异常
data guard switchover切换异常 查看DG数据库备份库发现,switchover_status为SWITCHOVER LATENT SQL> select OPEN_MODE, ...
- 写的一个Makefile
#========================================================================= # # MAKE FILE FOR ROCKY # ...
- Steady Cow Assignment
poj3189:http://poj.org/problem?id=3189 题意:这一题的题意.我看了很长时间才弄懂.就是给你n头牛,m个牛棚,每个牛对每一个牛棚会有一个满值,第i行第j个数表示的是 ...