首页 > 其他分享 >hey_left 2 Codeforces Round 918 (Div. 4) 续

hey_left 2 Codeforces Round 918 (Div. 4) 续

时间:2024-01-15 16:25:04浏览次数:26  
标签:index return int sum Codeforces hey 918 first

题目链接

F.

常规的树状数组求逆序对
需要注意的是,因为是下标与值的映射,所以数值不能为负数,也不能太大
然后传参数的时候,参数是最大数值
切记切记

#include <bits/stdc++.h>
using namespace std;

const int N=2e5+10;

template <typename T>
struct TreeArray{
    vector<T> tree;

    TreeArray(T n){
        tree.resize(n+1,T(0));
    }

    void update(T index,T value){
        while(index<tree.size()){
            tree[index]+=value;
            index+=index&(-index);
        }
    }

    T query(T index){
        T sum=0;
        while(index>0){
            sum+=tree[index];
            index-=index&(-index);
        }
        return sum;
    }
};


void solve(){
    int n;cin>>n;
    pair<int,int>a[N];
    for(int i=1;i<=n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a+1,a+1+n);
//    for(int i=1;i<=n;i++){
//        cout<<a[i].first<<' '<<a[i].second<<endl;
//    }
    TreeArray<int>ta(100000);
    int sum=0;
    for(int i=1;i<=n;i++){
        ta.update(a[i].second,1);
        //cout<<i<<' '<<ta.query(a[i].second)<<endl;
        sum+=max(0,i-ta.query(a[i].second));
    }
    cout<<sum<<'\n';
}


signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int hey_left=1;
    //cin>>hey_left;
    while(hey_left--){
        solve();
    }
    return 0;
}

虽然但是,这道题还是没有解决
因为数据有负数且很大
要进行离散,再用普通树状数组

举一个简单的例子:
1 2 3
1 2 100
它们的逆序对数是一样的
也就是说我们不一定要记录真实的数值,数字间只要满足同样的相对大小即可
一列数字 1 3 2
记录下输入顺序 1 2 3
然后按升序排序 1 2 3
对应的原输入顺序 1 3 2
排好序的数 值映射到1-n
但我们的输入顺序是不变的
所以再开一个数组c,然后便利序列 ,c[输入顺序]=i
具体看代码吧,表达能力有限
得到的b数组就可以用普通树状数组做了

给出离散部分的代码:

pair<int,int>b[N];
bool cmp(pair<int,int>x,pair<int,int>y){//一维是数值,二维是输入顺序
    if(x.first!=y.first)return x.first<y.first;
    else return x.second<y.second;//就是说排序时有相同的数值,那么就按输入顺序排
}
for(int i=1;i<=n;i++){
        cin>>b[i].first;
        b[i].second=i;
}
sort(b+1,b+1+n,cmp);
int c[N];
    for(int i=1;i<=n;i++){
        c[b[i].second]=i;
    }

至此,终于过了
感觉归并排序就没有这个问题,但对递归有天然的恐惧,就用这个吧

#include <bits/stdc++.h>
using namespace std;

const int N=2e5+10;
#define int long long
template <typename T>
struct TreeArray{
    vector<T> tree;

    TreeArray(T n){
        tree.resize(n+1,T(0));
    }

    void update(T index,T value){
        while(index<tree.size()){
            tree[index]+=value;
            index+=index&(-index);
        }
    }

    T query(T index){
        T sum=0;
        while(index>0){
            sum+=tree[index];
            index-=index&(-index);
        }
        return sum;
    }
};
bool cmp(pair<int,int>x,pair<int,int>y){
    if(x.first!=y.first)return x.first<y.first;
    else return x.second<y.second;
}
pair<int,int>b[N];
void solve(){
    int n;cin>>n;
    pair<int,int>a[N];
    for(int i=1;i<=n;i++){
        cin>>a[i].first>>a[i].second;
    }
    sort(a+1,a+1+n);
    for(int i=1;i<=n;i++){
        cin>>b[i].first;
        b[i].second=i;
    }
    sort(b+1,b+1+n,cmp);
    int c[N];
    for(int i=1;i<=n;i++){
        c[b[i].second]=i;
    }
    TreeArray<int>ta(n);
    int sum=0;
    for(int i=1;i<=n;i++){
        ta.update(c[i],1);
        //cout<<i<<' '<<ta.query(a[i].second)<<endl;
        sum+=max(0ll,i-ta.query(c[i]));
    }
    cout<<sum<<'\n';
}


signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int hey_left=1;
    cin>>hey_left;
    while(hey_left--){
        solve();
    }
    return 0;
}

标签:index,return,int,sum,Codeforces,hey,918,first
From: https://www.cnblogs.com/wwww-/p/17962496

相关文章

  • Codeforces Round 919 (Div. 2)(A~D) 题解
    CodeforcesRound919(Div.2)(A~D)题解A.SatisfyingConstraints题意:给你一些条件让你求出满足条件的整数有多少个。模拟即可。#include<bits/stdc++.h>usingnamespacestd;typedeflonglongll;constllMAXN=2e5+5;llTex=1,n;voidAC(){ cin>>n; lll=......
  • Codeforces Round 919 (Div. 2)
    CodeforcesRound919(Div.2)A-SatisfyingConstraints#include<bits/stdc++.h>#defineendl'\n'#defineintlonglongusingnamespacestd;constintN=1e6+10;voidsolve(){ intn; intl=-1; intr=1e9+10; cin>>......
  • CodeForces 1920F2 Smooth Sailing (Hard Version)
    洛谷传送门CF传送门首先需要知道的一个trick:判断一个点是否在一个闭合回路内部,从这个点向任意方向引一条射线,若不考虑相切,那么和回路的交点为奇数时这个点在回路内部,否则在外部。那么这题要判断一个回路是否包含全部的island,可以找到任意一个island向右引一条射线。给每......
  • Hey left 1 Codeforces Round 918 (Div. 4)
    题目链接A.3个数,其中2个数相同,输出不相同的那个可以用ifelse判断,较为麻烦用的map,输出出现一次的#include<bits/stdc++.h>usingnamespacestd;constintN=1e5+10;voidsolve(){map<int,int>mp;for(inti=0,x;i<3;i++){cin>>x;mp[x]++;......
  • CodeForces 1237H Balanced Reversals
    洛谷传送门CF传送门容易想到把\(s,t\)分成长度为\(2\)的段考虑。容易发现\(00,11\)的个数在操作过程中不会改变,所以若两串的\(00\)或\(11\)个数不相等则无解。考虑依次对\(i=2,4,\ldots,n\)构造\(s[1:i]=t[n-i+1:n]\)。对于\(s_{j-1}s_j=y......
  • Codeforces [Hello 2024]
    CodeforcesHello2024主打一个昏了头A.WalletExchange#include<bits/stdc++.h>#defineendl'\n'//#defineintlonglongusingnamespacestd;constintN=2e5+10;inta,b;voidsolve(){ cin>>a>>b; if((a+b)&1)cout<<......
  • CodeForces 1379E Inverse Genealogy
    洛谷传送门CF传送门\(n\)为偶数显然无解。否则我们可以构造一棵\(n\)个点的完全二叉树,当\(n+1\)是\(2\)的幂时满足\(m=1\),否则\(m=0\)。当\(n\ge5\)时可以递归至\((n-2,m-1)\),再挂一个叶子即可。但是可能会出现\(n+1\)不是\(2\)的幂,但\(n-......
  • codeforces比赛(3):codeforces good_bye_2023
    A、2023跳转原题点击此:A题地址1、题目大意  在一个乘积可能等于2023的数组a中去掉了k个数,得到新的长度为n的b数列。请你输出k个数,使得这k个数与b数列相乘为2023.如果不存在则输出No。2、题目解析  因为这道题的n和k都是不超过5,所以我们只需要算出b数组的乘积是否是2023的......
  • cf 918(D-G) div4
    cf918(D-G)div4D.UnnaturalLanguageProcessing算法分析:string模拟+贪心贪心策略:把元音字母看作0,辅音字母作为1,因为是等价的,构造字符串,寻找a.find("101"),a.find("10");判断合理性,贪心选择101还是10,最后把原数组打印,erase删除前面的"101"/"10"串,直到串删为空不要偷懒......
  • Codeforces Round 918 (Div. 4) (前缀和,权值树状数组,二维偏序, python + golang)
    Dashboard-CodeforcesRound918(Div.4)-Codeforces  fromcollectionsimport*defsolve():a,b,c=list(map(int,input().split()))hs=defaultdict(int)hs[a]+=1hs[b]+=1hs[c]+=1foriinhs:ifhs[i]=......