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 ...
随机推荐
- P3587 [POI2015]POD
题目描述 长度为n的一串项链,每颗珠子是k种颜色之一. 第i颗与第i-1,i+1颗珠子相邻,第n颗与第1颗也相邻.切两刀,把项链断成两条链.要求每种颜色的珠子只能出现在其中一条链中.求方案数量(保证至 ...
- 查询数据库中的表格---通过构造方法将数据存入到List集合中---遍历进行输出
package cn.jy.demo; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.Res ...
- QueryRunner类的八种结果处理集
package cn.jy.demo; import java.sql.Connection; import java.sql.SQLException; import java.util.List; ...
- jedis常用API
一.Redis Client介绍 1.1.简介 Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用. Jedis源码工程地址:https://g ...
- Spring相关知识点
1.注解@qualifier 只能注在属性上 作用:当一个接口有多个实现类时,用Autowired装配时,因为Autowired是按类型装配的(Resource按名称),所以多个实现类会出现冲突,这是 ...
- jvm 启动参数设置(转载)
JVM启动参数 http://onlyor.iteye.com/blog/1722413 博客分类: java java java启动参数共分为三类其一是标准参数(-),所有的JVM实现都必须实现这些 ...
- js 正则表达式,匹配邮箱/手机号/用户名
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C语言基础第四次作业
题目7-2,九九乘法表 1.实验代码: #include<stdio.h> int main() { int N, i, j, q; scanf("%d",&N ...
- TryXXX模式(深入理解c#)
.NET有几个模式很容易根据所涉及的方法名称来识别.例如,BeginXXX和EndXXX暗示着一个异步操作.TryXXX模式的用途在.net1.1升级到2.0期间进行了扩展.他是针对以下情况设计的:有 ...
- python 函数学习sys.argv[]
sys.argv 参数 「argv」是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序.这个变量其实是一个List列表,argv[0] 一般是“被调用的 ...