Codeforces 567C - Geometric Progression - [map维护]

题目链接:https://codeforces.com/problemset/problem/567/C

题意:

给出长度为 $n$ 的序列 $a[1:n]$,给出公比 $k$,要求你个给出该序列中,长度为 $3$ 的等比子序列的数目。

题解:

首先倒着遍历,用map记录曾经出现过的每个数字的出现次数,然后再用另一个map来记录曾经出现过的所有满足 $(x,kx)$ 的二元组的数目,最后就直接维护答案即可。

AC代码:

#include<bits/stdc++.h>
#define IO (ios::sync_with_stdio(0),cin.tie(0),cout.tie(0))
#define mk make_pair
#define fi first
#define se second
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
const int maxn=2e5+10;
int n;
ll k,a[maxn];
map<ll,ll> mp1;
map<P,ll> mp2;

int main()
{
    IO;

    cin>>n>>k;

    ll mx=-1e10, mn=1e10;
    for(int i=1;i<=n;i++)
        cin>>a[i], mx=max(a[i],mx), mn=min(a[i],mn);

    ll ans=0;
    for(int i=n;i>=1;i--)
    {
        if(mn/(k*k)<=a[i] && a[i]<=mx/(k*k))
            ans+=mp2[mk(a[i]*k,a[i]*k*k)];

        if(mn/k<=a[i] && a[i]<=mx/k)
            mp2[mk(a[i],a[i]*k)]+=mp1[a[i]*k];

        mp1[a[i]]++;
    }
    cout<<ans<<endl;
}

原文地址:https://www.cnblogs.com/dilthey/p/10801806.html

时间: 2024-08-08 12:52:34

Codeforces 567C - Geometric Progression - [map维护]的相关文章

CodeForces 567C. Geometric Progression(map 数学啊)

题目链接:http://codeforces.com/problemset/problem/567/C C. Geometric Progression time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Polycarp loves geometric progressions very much. Since he was on

CodeForces 567C Geometric Progression

Geometric Progression Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 567C Description Polycarp loves geometric progressions very much. Since he was only three years old, he loves only t

CodeForces 567C Geometric Progression 类似dp的递推统计方案数

input n,k 1<=n,k<=200000 a1 a2 ... an 1<=ai<=1e9 output 数组中选三个数,且三个数的下标严格递增,凑成形如b,b*k,b*k*k的种数 做法:先将可以作为第三个数的数放到map中,然后再扫一遍依次统计map中的数作为第三个数的种数,第二个数的种数,第三个数的种数 1 #include<cstdio> 2 #include<map> 3 struct node 4 { 5 int b;//a[i]作为i1的

map Codeforces Round #Pi (Div. 2) C. Geometric Progression

题目传送门 1 /* 2 题意:问选出3个数成等比数列有多少种选法 3 map:c1记录是第二个数或第三个数的选法,c2表示所有数字出现的次数.别人的代码很短,思维巧妙 4 */ 5 /************************************************ 6 * Author :Running_Time 7 * Created Time :2015-8-6 1:07:18 8 * File Name :C.cpp 9 *************************

Codeforces Round #Pi (Div. 2)——map——Geometric Progression

Polycarp loves geometric progressions very much. Since he was only three years old, he loves only the progressions of length three. He also has a favorite integer k and a sequence a, consisting of n integers. He wants to know how many subsequences of

bzoj 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)

题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而这题有一些很艮的地方: 1.如果一个喵的名和姓都被点到,那他只被点到了一次 2.询问的串可能相同 3.如果map中并不包含某个元素,但你强行用数组表示它,那么它会返回0,然后这个元素会被强行插入map并赋值成0 1 #include <map> 2 #include <queue> 3

CodeForces - 670C Cinema (map&amp;模拟)水

CodeForces - 670C Cinema Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status Description Moscow is hosting a major international conference, which is attended by n scientists from different countries. Each of th

codeforces 567C

求长度为三的子序列是公比为k的等比数列的个数 很好想到思路,,,,就是记录之前的数中,是这个数的1/k的有多少个,1/k^2的有多少个,,,,, 主要就是数字太大不能用数组,,,要用map(涨姿势) #include<stdio.h> #include<string.h> #include <algorithm> #include <bits/stdc++.h> using namespace std; long long int a; map<lon

codeforces 589A Email Aliases(map)

Description Polycarp has quite recently learned about email aliases. Of course, he used to suspect that the case of the letters doesn't matter in email addresses. He also learned that a popular mail server in Berland bmail.com ignores dots (character