当前位置: 代码迷 >> 综合 >> D. Same Differences B. Interesting Subarray
  详细解决方案

D. Same Differences B. Interesting Subarray

热度:72   发布时间:2023-11-23 07:02:55.0

D. Same Differences

题:
You are given an array a of n integers. Count the number of pairs of indices (i,j) such that i<j and aj?ai=j?i.

Input
The first line contains one integer t (1≤t≤10^4). Then t test cases follow.

The first line of each test case contains one integer n (1≤n≤2?10^5).

The second line of each test case contains n integers a1,a2,…,an (1≤ai≤n) — array a.

It is guaranteed that the sum of n over all test cases does not exceed 2?10^5.

Output
For each test case output the number of pairs of indices (i,j) such that i<j and aj?ai=j?i.

Example
input
4
6
3 5 1 4 6 6
3
1 2 3
4
1 3 3 4
6
1 6 3 4 5 6
output
1
3
3
10

题意:
给出n个元素的数组,要求找到任意两个元素配对( i , j ),下标 i < j , 并且 aj - ai = j - i ,输出不重复的配对数。

思路:
根据题目给的等式,移动一下项,可以发现,aj - j = ai - i ( i < j )时,则这两个元素配对,首先遍历,用map将每个元素和下标的差值存起来,遍历完后把所有差值相等的用组合数(无顺序)求然后加起来即答案。
注意要开long long ,(之前开的long wa了好多次,后来找输出例才发现溢出了orz,以后还是直接long long 吧,不想用long 了,吃亏太多了)

ps:小学数学题,一开始没看出来,傻傻的双for模拟然后意料之中TLM了。吐槽一下这把cf,我以为是div2,开了两道题就走了,掉大分了,不过当时状态确实也不太好QAQ

#include<iostream>
#include<map>
using namespace std;typedef long long ll ;map< ll , ll >p ;int main()
{
    ios::sync_with_stdio(false) ;cin.tie(0) ;cout.tie(0) ;int t ;cin >> t ;while (t--){
    p.clear() ;ll n , i , x , sum = 1 ; cin >> n ;for(i = 1; i <= n; ++i){
    cin >> x ;p[x - i]++ ;}int flag = 0 ;ll ss = 0 ;for( auto &j : p ){
    sum = 1 ;if ( j.second >= 2 ){
    flag = 1 ;sum *= j.second * (j.second - 1) / 2 ;ss += sum ;}}if ( !flag )ss = 0 ;cout << ss << endl ;}return 0 ;
}

B. Interesting Subarray

题:
For an array a of integers let’s denote its maximal element as max(a), and minimal as min(a). We will call an array a of k integers interesting if max(a)?min(a)≥k. For example, array [1,3,4,3] isn’t interesting as max(a)?min(a)=4?1=3<4 while array [7,3,0,4,3] is as max(a)?min(a)=7?0=7≥5.

You are given an array a of n integers. Find some interesting nonempty subarray of a, or tell that it doesn’t exist.

An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.

Input
The first line contains integer number t (1≤t≤10000). Then t test cases follow.

The first line of each test case contains a single integer n (2≤n≤2?10^5) — the length of the array.

The second line of each test case contains n integers a1,a2,…,an (0≤ai≤10^9) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 2?10^5.

Output
For each test case, output “NO” in a separate line if there is no interesting nonempty subarray in a.

Otherwise, output “YES” in a separate line. In the next line, output two integers l and r (1≤l≤r≤n) — bounds of the chosen subarray. If there are multiple answers, print any.

You can print each letter in any case (upper or lower).

Example
input
3
5
1 2 3 4 5
4
2 0 1 9
2
2019 2020
output
NO
YES
1 4
NO

Note
In the second test case of the example, one of the interesting subarrays is a=[2,0,1,9]: max(a)?min(a)=9?0=9≥4.

题意:
找到是否存在一个子数组(可以是该数组本身),如果满足这个数组里元素最大值减去元素最小值大于等于该元素的长度,则输出“YES”并且下一行输出该数组第一个元素的位置和最后一个元素的位置。找不到则输出“NO”。

思路:
只要遍历有没有相邻两个数之差大于二就可以了。如果存在满足如题所说条件的数组,那么必有相邻两数之差大于二。

/之前还有一个想法是找到一个最小值然后往两边遍历找到这样一个数组,但是这样是不可行的,因为数组元素是可以重复的
比如这个例子:
在这里插入图片描述

#include<iostream>
#include<cmath>
using namespace std ;int main()
{
    int t , a[200005] ;cin >> t ;while( t-- ){
    int n , mm , nn ;cin >> n ;bool flag = 0 ;cin >> a[1] ;for( int i = 2 ; i <= n ; ++i ){
    cin >> a[i] ;if( abs(a[i] - a[i-1])> 1 ){
    flag = 1 ;mm = i-1 , nn = i ; }}if( flag ){
    cout << "YES" << endl ;cout << mm << " " << nn << endl ;}elsecout << "NO" << endl ;}return 0 ;
}
  相关解决方案