找结论——势能

简介: 找结论——势能

势能就是i + a[i],很有用的结论 对于能完成交换,从A变成B,势能数组一定相同 找出最小交换次数,贪心的方案是:最近的先交换 找到最近的在可以用数组数组和set实现 具体细节见代码


#include<bits/stdc++.h>
#define debug1(a) cout<<#a<<'='<< a << endl;
#define debug2(a,b) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<endl;
#define debug3(a,b,c) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<endl;
#define debug4(a,b,c,d) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<endl;
#define debug5(a,b,c,d,e) cout<<#a<<" = "<<a<<"  "<<#b<<" = "<<b<<"  "<<#c<<" = "<<c<<"  "<<#d<<" = "<<d<<"  "<<#e<<" = "<<e<<endl;
#define debug0(x) cout << "debug0: " << x << endl
#define fr(t, i, n)for (long long i = t; i < n; i++)
#define YES cout<<"Yes"<<endl
#define NO cout<<"No"<<endl
#define fi first
#define se second
#define int long long
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
//#pragma GCC optimize(3,"Ofast","inline")
//#pragma GCC optimize(2)
const int N = 2e5+10;
int aa[N];
int lowbit(int x)  // 返回末尾的1
{return x & -x;}
void add(int x,int w){
    for(int i = x;i < N;i += lowbit(i)){
        aa[i] += w;
    }
}
int query(int u){
    int res = 0;
    for(int i = u;i > 0;i -= lowbit(i)){
        res += aa[i];
    }
    return res;
}
int a[N],b[N];
multiset<int> A,B;
void solve() 
{
    int n;cin >> n;
    
    for(int i = 1; i <= n; i++)cin >> a[i],A.insert(a[i] + i);
    for(int i = 1; i <= n; i++)cin >> b[i],B.insert(b[i] + i);
    if(A != B)
    {
        cout << -1 << endl;
        return ;
    }
    set<PII> s;
    int ans = 0;
    for(int i = 1; i <= n; i++)s.insert(PII{a[i] + i, i});
    for(int i = 1; i <= n; i++)
    {
        set<PII>::iterator it = s.lower_bound(PII{b[i]+i,0});
        int t = query((*it).second) + (*it).second;
        
        add(1,1);
        assert((*it).second>0);
        add((*it).second,-1);//这里相对位置不用加1,因为这个是相对于a数组原始位置而言的
        ans += t - i;
              
        s.erase(it);
    }
    cout << ans << endl;
}
signed main()
{
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int T = 1;//cin >> T;
    while(T--){
        solve();
    }
    return 0;
}
相关文章
|
5月前
|
机器学习/深度学习 数据挖掘 Python
统计回归模型中的一些概念解释
统计回归模型中的一些概念解释
|
7月前
|
C# C++
|
7月前
|
监控 算法 数据安全/隐私保护
思维模型No.52|一种保证效果的学习方法:费曼技巧
思维模型No.52|一种保证效果的学习方法:费曼技巧
56 0
|
9月前
|
编解码 JavaScript
解释基本的3D理论
本文介绍了所有基本理论,这些理论在开始使用 3D 时很有用。
69 0
解释基本的3D理论
|
前端开发 中间件
ChatGPT都只能, 解释一半的代码, 是啥样的
我发现啊, 有的人还不会用ChatGPT, 真的有点出乎我的认知, 我觉得一些理所应知的事情, 他们不知
246 0
|
存储
评价电脑内存条的好坏的参数有哪些?底层原理是什么?
评价电脑内存条的好坏的参数有哪些?底层原理是什么?
720 0
|
机器学习/深度学习 人工智能 分布式计算
因果推断:效应估计的常用方法及工具变量讨论
日常工作中很多的策略/产品的效果是无法设计完美的随机实验的,要求我们从观察性数据中去(拟合随机试验)发现因果关系、测算因果效应。
1445 0
因果推断:效应估计的常用方法及工具变量讨论
|
IDE 程序员 开发工具
一道面试题的最终答案
一道面试题的最终答案
87 1
|
搜索推荐
提出好问题引出一个好答案
在你和你想要的东西之间,只差一连串更好的问题。 -- 《巨人的方法》
170 0
提出好问题引出一个好答案
|
测试技术
软件测试面试题:解释常用的性能指标的名称与具体含义
软件测试面试题:解释常用的性能指标的名称与具体含义
87 0