hdu5178 pairs
题目
//打注释的是我的代码,一直超时,别人三行代码顶我一坨,同是尺取法,为什么 我的复杂度就这么高呢?
#include <cstdio>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
const int MAX_N = 100010;
int arr[MAX_N];
int main()
{
int n, s;
long long ans;
int T;
scanf("%d",&T);
while(T--)
{
ans=0;
scanf("%d%d", &n, &s);
for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
int l = 0, r = 0;
stable_sort(arr,arr+n);
int sum = 0;
/* while (true)
{
if(r==n) break;
while(1)
{
++r;
if(r==n) break;
if( arr[r]-arr[l]<=s)
ans++;
}
l++;
r=l;
}*/
int j;
for(int i = 0,j = 0; i < n; i++)
{
while(j + 1 < n && arr[j+1] - arr[i] <= s) j++;
ans += j - i;
}
printf("%lld\n", ans);
}
return 0;
}
hdu5178 pairs的更多相关文章
- [LeetCode] Find K Pairs with Smallest Sums 找和最小的K对数字
You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. Define ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- Leetcode-24 Swap Nodes in Pairs
#24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- 【LeetCode】Palindrome Pairs(336)
1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...
- 数论 - Pairs(数字对)
In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...
- 24. Swap Nodes in Pairs
24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...
- Palindrome Pairs -- LeetCode 336
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
- 336-Palindrome Pairs
336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...
- Palindrome Pairs
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
随机推荐
- 自适应手机网站meta name代码
<meta name="viewport" content="width=device-width,initial-scale=1.0"> co ...
- RNA-seq流程需要进化啦!
RNA-seq流程需要进化啦! Posted on 2015年9月25日 Tophat 首次被发表已经是6年前 Cufflinks也是五年前的事情了 Star的比对速度是tophat的50倍,hisa ...
- NC 6系分配组织方法
分配客户的方法 String[] customer = { cuVO.getPk_customer() }; ICustAssignService cs = NCLocator.getInstance ...
- virualbox问题
出不来64位虚拟系统 bios设置虚拟化可用 2.创建虚拟机 启动不了 提示 不能打开一个... 安装exten 扩张包 3.设备 -- 安装增强功能... 分辨率 设置成功
- AX_Args
Args args; FormRun formRun; ; args = new Args(); args.name(formstr(FormName)); args.caller(); args.r ...
- 20155312 2016-2017-2 《Java程序设计》第八周学习总结
20155312 2016-2017-2 <Java程序设计>第八周学习总结 课堂内容总结 学习模式 游乐园模式-荒野求生模式 学习方法 以代码为中心->遇到不会的类和方法(参数等) ...
- 2018.11.03 NOIP模拟 图(bfs/最短路)
传送门 显然如果AAA到BBB或者CCC到DDD走的不是最短路一定是有一段路径重合了,于是可以O(n2)bfsO(n^2)bfsO(n2)bfs出两点之间的最短距离然后枚举两个点作为重合的端点来更新答 ...
- linux vi操作
1.“i”键,进入编辑状态.可以看到命令的下方出现了“--INSERT--”字样,表示成功进入了编辑模式; 2.“Esc”键,退出编辑模式.并在此时输入“:wq”以退出并保存
- 百度地图经纬度和地址互转(Java代码)
这是基于springmvc+mybatis 的一个controller.如果不是这个框架,可以把方法实体抽到自己写的一个类中,然后再测试 package com.uwitec.controller.s ...
- MYSQL 事务测试
mysql 事务测试 创建张表 lock1 增加字段 id,name . 增加两条记录 1,a 2,b 启动第一个会话 BEGIN; update lock1 set name='c' where i ...