codeforces_459D_(线段树,离散化,求逆序数)

本文解析了CodeForces上的一道题目D.Pashmak and Parmida's problem,介绍了使用线段树求解逆序数的方法,并通过离散化简化处理过程。

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

链接:http://codeforces.com/problemset/problem/459/D

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 indices k such that: l ≤ k ≤ r andak = x. His task is to calculate the number of pairs of indicies i, j (1 ≤ i < j ≤ n) such that f(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 contains n space-separated integers a1, a2, ..., an(1 ≤ ai ≤ 109).

Output

Print a single integer — the answer to the problem.

Examples
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

题意:f(1,i,ai)表示(1,i)中等于ai的个数,f(j,n,aj)表示(j,n)中与aj相等的个数,求有多少对(i,j)。(特么,之前题意看错了,想了一上午没想明白,结果。。。)
思路:可以用数组fro统计每个位置(包括该位置)其前面有多少个与它相等的数,用beh数组统计每个位置其后有多少个与它相等的数。
  样例:
    7
    1 2 1 1 2 2 1
    fro:{1,1,2,3,2,3,4}  
    beh:{4,3,3,2,2,1,1}

问题就转化为求第一个序列相对第二个序列的逆序数(因为相当于i可以从fro中查看,j可以从beh中查看)
线段树求逆序数。。。
可以从前往后处理,也可以从后往前处理。
貌似从后往前更好理解。
从后往前处理,即,将beh依次插入树中,每插一个,查询其对应的fro的元素(1,fro[i]-1)区间也就是小于fro[i]的个数(查当前树中比fro[i]小的个数)
从前往后跟以上思路相反,插fro查beh(差当前树中比beh[i]大的个数)。
代码中第一次接触到离散化,以后可以借鉴。
此题还需多加理解。
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<map>
using namespace std;
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct Node
{
    int l,r;
    int num;
} tree[1000000<<2];

int num[1000005];
int fro[1000005],beh[1000005],sum[1000005];
map<int,int> m1;

void build(int l,int r,int rt)
{
    tree[rt].l=l;
    tree[rt].r=r;
    tree[rt].num=0;
    if(tree[rt].l==tree[rt].r)
        return;
    int mid=(l+r)/2;
    build(lson);
    build(rson);
}

void update(int x,int l,int r,int rt)
{
    tree[rt].num++;
    if(tree[rt].l==tree[rt].r&&tree[rt].l==x)
        return;
    int mid=(l+r)/2;
    if(x<=mid)
        update(x,lson);
    else
        update(x,rson);
}

int query(int L,int R,int l,int r,int rt)
{
    if(L>R)
        return 0;
    if(L==l&&R==r)
        return tree[rt].num;
    int mid=(l+r)/2;
    if(R<mid)
        return query(L,R,lson);
    else if(L>mid)
        return query(L,R,rson);
    else
        return query(L,mid,lson)+query(mid+1,R,rson);

}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d",&num[i]);
    int cnt=1;
    for(int i=1; i<=n; i++)  ///离散化,以后可以借鉴
    {
        if(!m1[num[i]])
            m1[num[i]]=cnt++;
        num[i]=m1[num[i]];
    }
    cnt=0;
    memset(sum,0,sizeof(sum));
    for(int i=1; i<=n; i++)
    {
        sum[num[i]]++;
        fro[i]=sum[num[i]];
        cnt=max(cnt,fro[i]);
    }
    memset(sum,0,sizeof(sum));
    for(int i=n; i>=1; i--)
    {
        sum[num[i]]++;
        beh[i]=sum[num[i]];
    }
    build(1,cnt,1);
    long long ans=0;
    for(int i=n; i>1; i--)
    {
        update(beh[i],1,cnt,1);
        //cout<<"**"<<endl;
        ans+=query(1,fro[i-1]-1,1,cnt,1);
        //cout<<ans<<'*'<<endl;
    }
    printf("%I64d\n",ans);
    return 0;
}

 

转载于:https://www.cnblogs.com/jasonlixuetao/p/5475333.html

内容概要:该研究通过在黑龙江省某示范村进行24小时实地测试,比较了燃煤炉具与自动/手动进料生物质炉具的污染物排放特征。结果显示,生物质炉具相比燃煤炉具显著降低了PM2.5、CO和SO2的排放(自动进料分别降低41.2%、54.3%、40.0%;手动进料降低35.3%、22.1%、20.0%),但NOx排放未降低甚至有所增加。研究还发现,经济性和便利性是影响生物质炉具推广的重要因素。该研究不仅提供了实际排放数据支持,还通过Python代码详细复现了排放特征比较、减排效果计算和结果可视化,进一步探讨了燃料性质、动态排放特征、碳平衡计算以及政策建议。 适合人群:从事环境科学研究的学者、政府环保部门工作人员、能源政策制定者、关注农村能源转型的社会人士。 使用场景及目标:①评估生物质炉具在农村地区的推广潜力;②为政策制定者提供科学依据,优化补贴政策;③帮助研究人员深入了解生物质炉具的排放特征和技术改进方向;④为企业研发更高效的生物质炉具提供参考。 其他说明:该研究通过大量数据分析和模拟,揭示了生物质炉具在实际应用中的优点和挑战,特别是NOx排放增加的问题。研究还提出了多项具体的技术改进方向和政策建议,如优化进料方式、提高热效率、建设本地颗粒厂等,为生物质炉具的广泛推广提供了可行路径。此外,研究还开发了一个智能政策建议生成系统,可以根据不同地区的特征定制化生成政策建议,为农村能源转型提供了有力支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值