P1966 火柴排队题解
P1966 火柴排队
题意:有两列火柴,各有各的高度,两两高度不同,每次只能交换相邻的数,问最少交换多少次,使得 ∑ ( a i − b i ) 2 \sum (a_i-b_i)^2 ∑(ai−bi)2最小。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#include <queue>
#include <cmath>
#include <set>
#include <map>
#include <bitset>
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define _for(n,m,i) for (register int i = (n); i < (m); ++i)
#define _rep(n,m,i) for (register int i = (n); i <= (m); ++i)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define PI acos(-1)
#define eps 1e-4
#define rint register int
#define F(x) ((x)/3+((x)%3==1?0:tb))
#define G(x) ((x)<tb?(x)*3+1:((x)-tb)*3+2)
using namespace std;
typedef long long LL;
typedef pair<LL, int> pli;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<LL, LL> pll;
typedef pair<double, double> pdd;
typedef map<int, int> mii;
typedef map<LL, int> mli;
typedef map<char, int> mci;
typedef map<string, int> msi;
template<class T>
void read(T &res) {int f = 1; res = 0;char c = getchar();while(c < '0' || c > '9') { if(c == '-') f = -1; c = getchar(); }while(c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = getchar(); }res *= f;
}
const int ne[8][2] = {1, 0, -1, 0, 0, 1, 0, -1, -1, -1, -1, 1, 1, -1, 1, 1};
const LL INF = 1e18;
const int N = 1e5+10;
const LL Mod = 99999997;
const int M = 110;
LL tmp[N], c[N];
int n, cnt;
LL tr[N];
int lowbit(int x) { return x & -x; }
void upd(int p, LL d) {for( ; p <= n; p += lowbit(p) ) tr[p] = (tr[p] + d) % Mod;
}
LL que(int p) {LL r = 0;for( ; p > 0; p -= lowbit(p) ) r = (r + tr[p]) % Mod;return r;
}
struct xx {int v, id;bool operator < (const xx &c) const {return v < c.v;}
}a[N], b[N];
int main() {scanf("%d", &n);_rep(1, n, i) { scanf("%lld", &a[i].v); a[i].id = i; }_rep(1, n, i) { scanf("%lld", &b[i].v); b[i].id = i; }sort(a+1, a+n+1); sort(b+1, b+n+1);_rep(1, n, i) c[a[i].id] = b[i].id;LL ans = 0;_rep(1, n, i) {upd(c[i], 1); ans = (ans + i - que(c[i]))%Mod;}printf("%lld\n", ans);return 0;
}
P1966 火柴排队题解相关推荐
- P1966 火柴排队
题面:https://www.luogu.org/problem/P1966 本题就是要将a序列第k大的元素和序列b中第k大的元素的位置换成一样的. 那么可以将a,b数组分别离散化,此时只要将a,b数 ...
- 洛谷 P1966 火柴排队 —— 思路
题目:https://www.luogu.org/problemnew/show/P1966 首先,一个排列相邻交换变成另一个排列的交换次数就是逆序对数: 随便画一画,感觉应该是排个序,大的对应大的, ...
- 洛谷P1966 火柴排队
题目描述 涵涵有两盒火柴,每盒装有 nn 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为:\sum (a_i-b_i)^2∑(a ...
- 洛谷 P1966 火柴排队【树状数组】
题目描述 涵涵有两盒火柴,每盒装有n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为 ∑ni=1(ai−bi)2 ∑ i = 1 ...
- 洛谷 P1966 火柴排队
题意 给定2个等长序列a.b,要求通过交换使 \[\sum_{i=1}^{n}(a_i-b_i)^2\] 最小. 分析 看着这个式子,我突然想到了方差.很明显,方差反应数据的波动程度,所以让数据集中就 ...
- 洛谷P1966 火柴排队(逆序对)
题意 题目链接 Sol 不算很难的一道题 首先要保证权值最小,不难想到一种贪心策略,即把两个序列中rank相同的数放到同一个位置 证明也比较trivial.假设\(A\)中有两个元素\(a, b\), ...
- 洛谷P1966 火柴排队(cdq分治 + 逆序对)
2020.6.8 练二分了当然要练练分治,这些基础的处理手段真的还是蛮好用的. 这道题是问ai 和bi的平方和最小,最少需要的相邻的交换次数.手动模拟了一下感觉没什么思路.但是后来注意到每次只能移动相 ...
- 洛谷 1966 loj 2069 火柴排队 题解
博客观赏效果更佳 题意简述 给定两个数列 a , b a,b a,b,长度均为 n ( < = 100000 ) n(<=100000) n(<=100000), a , b a,b ...
- [NOIp2013] luogu P1966 火柴排队
磕了瓶魔爪. 题目描述 你有两个长度为 N N N 的数组 a , b a,b a,b,试重新排列 a a a 数组使得 S = ∑ i = 1 n ( a i − b i ) 2 S=\sum_{i ...
最新文章
- android EditText 限定中文个数与英文个数的解决方式
- 仓库管理系统gitlab
- Canvas贝塞尔三级曲线
- cocos2dx--cocos2dx3.1.1执行报无法解析的外部符号
- NET流行高性能JSON框架-Json.NET
- CentOS 7 安装 Greenplum6 (附 dotNET Core 示例)
- react jquery_2019年React简介(面向仅了解jQuery的人们)
- 医疗项目 开源_医疗保健受开源影响最大的行业之一
- HashMap(JDK1.8)
- 阿里巴巴与小毛驴的故事——贪心算法
- Linux启动U盘制作
- matpower安装问题
- 激光雷达产业深度研究报告:三大思考
- Python零基础福利来了!
- MAC电脑配置环境变量
- ps -- 将图片背景设置成透明的
- 倒计时器 (WPF)
- javaCSGO赛事管理系统springbootvueweb
- L2-025 分而治之 (25 分)详解
- 舵机速度控制原理细解