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 ...
随机推荐
- Shell脚本中$0、$?、$!等的意义
变量说明$$ Shell本身的PID(ProcessID)$! Shell最后运行的后台Process的PID$? 最后运行的命令的结束代码(返回值)$- 使用Set命令设定的Flag一览$* 所有参 ...
- UI设计初学者教程:色彩基础知识
编辑:千锋UI设计 初学设计都会先认识三原色,通常我们说的三原色指的是颜料三原色:红.黄.蓝:其实三原色还有色光三原色:红.绿.蓝.我们通常说的红黄蓝就是减色法三原色,而红绿蓝是加色法三原色.可能这么 ...
- 第二周java学习收获
JAVA学习第二周总结 教材学习内容总结 第二章:讲了标识符与关键字,以及基本数据类型:逻辑类型.整数类型.字符类型.浮点类型. 输入输出,System.in,System.out和数组. 第三章:运 ...
- 模板 Template
package ${enclosing_package}; import java.io.IOException;import javax.servlet.ServletException;impor ...
- python正则表达式获取两段标记内的字符串
比如获取绿色字符串 ModelData.PayTableData =[{"}, {"}, {"}]; ModelData.PayTableData1 =[{"} ...
- mybatis 插入数据并返回主键值
<insert id="insert" parameterType="com.pojo.TSubject" useGeneratedKeys=" ...
- jdom解析xml
这次把代码整理了一下,打包发上来了,程序中需要用到的jar包也一起打在里面了,有兴趣的朋友可以解压后直接导入的eclipse里运行! xml是一种广为使用的可扩展标记语言,java中解析xml的方式有 ...
- Homebrew -- mac 缺失包补充工具
https://brew.sh/index_zh-cn.htmlhttps://brew.sh/ 非root权限下运行 # github 源代码 https://github.com/Homebrew ...
- [ExecuteInEditMode]
ExecuteInEditMode属性的作用是在EditMode下也可以执行脚本.Unity中默认情况下,脚本只有在运行的时候才被执行,加上此属性后,不运行程序,也能执行脚本. 与PlayMode不同 ...
- 【转】Linux修改SSH端口和禁止Root远程登陆
Linux修改ssh端口22 vi /etc/ssh/ssh_config vi /etc/ssh/sshd_config 然后修改为port 8888 以root身份service sshd res ...