Codeforces Round #261 (Div. 2) D. Pashmak and Parmida's problem

本文介绍了一种使用动态规划(DP)算法解决特定序列问题的方法,包括输入输出规范、问题描述、实例分析以及代码实现。重点在于通过树状数组(线段树的一种实现方式)优化查询效率,适用于处理涉及区间查询和更新的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Pashmak and Parmida's problem
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Parmida is a clever girl and she wants to participate in Olympiads this year. Of course she wants her partner to be clever too (although he's not)! Parmida has prepared the following test problem for Pashmak.

There is a sequence a that consists of n integers a1, a2, ..., an. Let's denote f(l, r, x) the number of indicesk such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indiciesi, j (1 ≤ i < j ≤ n) such thatf(1, i, ai) > f(j, n, aj).

Help Pashmak with the test.

Input

The first line of the input contains an integer n(1 ≤ n ≤ 106). The second line containsn space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Sample test(s)
Input
7
1 2 1 1 2 2 1
Output
8
Input
3
1 1 1
Output
1
Input
5
1 2 3 4 5
Output
0
 

题意:给出数组A,定义f(l,r,x)为A[]的下标l到r之间,等于x的元素数。i和j符合f(1,i,a[i])>f(j,n,a[j]),求i和j的种类数。

题解:使用树状数组统计小于某数的元素数量。

STL(map)+树状数组

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;

int a[1000005];

int data[1000005];

int mark[1000005];

int lowbit(int x)
{
	return x&(-x);
}
void add(int p,int num)
{
	if(p<=0)return;
	while(p<=1000000)
	{
		a[p]+=num;
		p+=lowbit(p);
	}
}

int sum(int p)
{
	int ans=0;
	while(p>0)
	{
		ans+=a[p];
		p-=lowbit(p);
	}
	return ans;
}

int main()
{
	map<int,int> dicta,dictb;
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&data[i]);
	}
	for(int i=n-1;i>=0;i--)
	{
		dictb[data[i]]++;
		mark[i]=dictb[data[i]];
		add(dictb[data[i]],1);
	}
	long long ans=0;
	for(int i=0;i<n;i++)
	{
		dicta[data[i]]++;
		add(mark[i],-1);
		ans+=sum(dicta[data[i]]-1);
	}
	printf("%I64d\n",ans);
	return 0;
}


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值