题目

//打注释的是我的代码,一直超时,别人三行代码顶我一坨,同是尺取法,为什么 我的复杂度就这么高呢?
#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的更多相关文章

  1. [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 ...

  2. [LeetCode] Palindrome Pairs 回文对

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

  3. 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 ...

  4. 【LeetCode】Palindrome Pairs(336)

    1. Description Given a list of unique words. Find all pairs of distinct indices (i, j) in the given ...

  5. 数论 - Pairs(数字对)

    In the secret book of ACM, it’s said: “Glory for those who write short ICPC problems. May they live ...

  6. 24. Swap Nodes in Pairs

    24. Swap Nodes in Pairs Given a linked list, swap every two adjacent nodes and return its head. For ...

  7. 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 ...

  8. 336-Palindrome Pairs

    336-Palindrome Pairs Given a list of unique words, find all pairs of distinct indices (i, j) in the ...

  9. Palindrome Pairs

    Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...

随机推荐

  1. andorid 帧布局

    framelayout.xml帧布局 <?xml version="1.0" encoding="utf-8"?> <FrameLayout ...

  2. hdu 5532 (LIS) Almost Sorted Array

    http://acm.hdu.edu.cn/showproblem.php?pid=5532 题意大致是一组数中去掉一个数后问剩下的数是否构成非严格单调序列 正反各跑一遍最长非严格连续子序列,存在长度 ...

  3. iOS.Crash.Case-[__NSArrayM objectForKeyedSubscript:]

    1. [__NSArrayM objectForKeyedSubscript:]: unrecognized selector sent to instance - source code and s ...

  4. shell判断文件后缀名是否为特定字符串

    如果文件是 .css文件 或 .js文件,则进行处理. if [ "${file##*.}"x = "css"x ]||[ "${file##*.}& ...

  5. 卸载服务器GitLab

    sudo gitlab-ctl uninstall sudo rpm -e gitlab-ce find / -name gitlab|xargs rm -rf

  6. TortoiseSVN Project Monitor使用

    今天下载了TortoiseSVN Project Monitor,要把一个项目导入 name一直没有输入,一直导入不成功,点击了ok也不给提示,切记要写项目Name啊! 在使用svncheckout时 ...

  7. Partition Equal Subset Sum

    Given a non-empty array containing only positive integers, find if the array can be partitioned into ...

  8. STL基础3:map

    #include <iostream> #include <map> #include <string> using namespace std; #define ...

  9. Spring Boot学习笔记:ApplicationEvent和ApplicationEventListener事件监听

    采用事件监听的好处 以用户注册的业务逻辑为例,用户在填写完信息表单后,提交信息到后台,后台对用户信息进行处理,然后给用户返回处理结果的信息. 如上图所示,用户在注册时,后台需要处理一些系列流程,实际业 ...

  10. setInterval与setTimeout 的区别

    setInterval在执行完一次代码之后,经过了那个固定的时间间隔,它还会自动重复执行代码,而setTimeout只执行一次那段代码     用法: setInterval("alert( ...