AtCoder Beginner Contest 439 - D - Kadomatsu Subsequence

Time Limit: 2 sec / Memory Limit: 1024 MiB

Score : 425 points

Problem Statement

You are given an integer sequence A=(,,...,) of length N.

Find the number of triples of integers (i,j,k) that satisfy all of the following:

  • 1≤i,j,k≤N
  • ::=7:5:3
  • min(i,j,k)=j or max(i,j,k)=j.

Constraints

  • All input values are integers.
  • 1≤N≤
  • 1≤Ai≤

Input

The input is given from Standard Input in the following format:

复制代码
N
​  … 

Output

Output the answer.


Sample Input 1

cpp 复制代码
10
3 10 7 10 7 6 7 6 5 14

Sample Output 1

复制代码
7

The seven triples of integers (i,j,k) that satisfy the conditions are:

  • (3,9,1)
    • ::=7:5:3, and max(i,j,k)=j.
  • (5,9,1)
    • ::=7:5:3, and max(i,j,k)=j.
  • (7,9,1)
    • ::=7:5:3, and max(i,j,k)=j.
  • (10,2,6)
    • ::=14:10:6=7:5:3, and min(i,j,k)=j.
  • (10,2,8)
    • ::=14:10:6=7:5:3, and min(i,j,k)=j.
  • (10,4,6)
    • ::=14:10:6=7:5:3, and min(i,j,k)=j.
  • (10,4,8)
    • ::=14:10:6=7:5:3, and min(i,j,k)=j.

Sample Input 2

cpp 复制代码
6
210 210 210 210 210 210

Sample Output 2

复制代码
0

Sample Input 3

cpp 复制代码
21
49 30 50 21 35 15 21 70 35 9 50 70 21 49 30 50 70 15 9 21 30

Sample Output 3

复制代码
34

考察点

1.数据统计与哈希映射

需要统计每个值在序列中的出现位置,由于值域很大(1≤Ai​≤),但序列长度有限(1≤N≤),使用哈希表将每个值映射到其索引列表是高效的选择。

cpp 复制代码
unordered_map<int,vector<int>>pos;
for(int i=0;i<n;i++){
    cin>>a[i];
    pos[a[i]].push_back(i+1);
}

2.条件转化与枚举优化

比例条件 ​::​=7:5:3 转化为 存在正整数t,使得​=7t、=5t、​=3t。

通过枚举中间值5t(即)来减少枚举量:只需检查每个是5的倍数的值,计算对应的7t和3t是否存在于序列中。

cpp 复制代码
for(int j=1;j<=n;j++){
    int val=a[j-1];
    if(val%5)continue;
    int t=val/5;
    int val3=t*3,val7=t*7;

    /****************************/

}

3.索引条件处理

条件 min(i,j,k)=j 或 max(i,j,k)=j 意味着 j 必须是三个索引中最小值或最大值。

这转化为对于每个候选的 j(对应值5t),分别计算:

当j最小时:统计值7t的索引中大于j的数量,以及值3t的索引中大于j的数量,两者乘积即为贡献。

当j最大时:统计值7t的索引中小于j的数量,以及值3t的索引中小于j的数量,两者乘积即为贡献。

4.二分查找加速

由于索引列表有序,对于每个j,需要在另外两个值的索引列表中快速查询大于或小于j的元素个数。使用二分查找可以将每次查询的复杂度降至O(log n),确保整体效率。

cpp 复制代码
vector<int>&pos3=pos[val3];
vector<int>&pos7=pos[val7];

//情况一:max(i,j,k)=j
auto p3=lower_bound(pos3.begin(),pos3.end(),j);
auto p7=lower_bound(pos7.begin(),pos7.end(),j);
ll c=p3-pos3.begin(),d=p7-pos7.begin();
ans+=c*d;

//情况2:min(i,j,k)=j
p3=upper_bound(pos3.begin(),pos3.end(),j);
p7=upper_bound(pos7.begin(),pos7.end(),j);
c=pos3.end()-p3,d=pos7.end()-p7;
ans+=c*d;

5.复杂度控制

整体算法的时间复杂度为O(N log N),适合1≤N≤。关键在于避免暴力枚举三元组,而是通过枚举中间值、利用哈希表和二分查找来高效计数。


代码

(要点已注释)

cpp 复制代码
#include<bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    int n;
    cin>>n;
    vector<int>a(n);
    unordered_map<int,vector<int>>pos;
    for(int i=0;i<n;i++){
        cin>>a[i];
        pos[a[i]].push_back(i+1);
    }
    ll ans=0;//长整型防止溢出
    for(int j=1;j<=n;j++){
        int val=a[j-1];
        if(val%5)continue;
        int t=val/5;
        int val3=t*3,val7=t*7;

        vector<int>&pos3=pos[val3];//注意:是引用,而不是拷贝(vector<int>pos3=pos[val3],会超时)
        vector<int>&pos7=pos[val7];

        //情况一:max(i,j,k)=j
        auto p3=lower_bound(pos3.begin(),pos3.end(),j);
        auto p7=lower_bound(pos7.begin(),pos7.end(),j);
        ll c=p3-pos3.begin(),d=p7-pos7.begin();
        ans+=c*d;

        //情况2:min(i,j,k)=j
        p3=upper_bound(pos3.begin(),pos3.end(),j);
        p7=upper_bound(pos7.begin(),pos7.end(),j);
        c=pos3.end()-p3,d=pos7.end()-p7;
        ans+=c*d;
    }
    cout<<ans;
    return 0;
}
相关推荐
Forever Nore1 小时前
学完C语言力扣第一题做不来正常吗
数据结构·算法
hansang_IR1 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘1 小时前
day7.c++继承
c++
沫璃染墨2 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌2 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
Tisfy3 小时前
LeetCode 3731.找出缺失的元素:哈希 / 排序
算法·leetcode·哈希算法·排序·哈希表
醉城夜风~3 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
lucas_AI3 小时前
Q-CueGraph:你的多模态大模型会 zoom,但真的知道该看哪儿吗?
人工智能·算法
烟花落o4 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门
kaixin_啊啊4 小时前
test_机器学习算法学习
学习·算法·机器学习