[Luogu] 火柴排队
https://www.luogu.org/problemnew/show/P1966
离散化
树状数组求逆序对个数
#include <bits/stdc++.h> using namespace std;
const int N = 1e5 + ;
const int Mod = ; struct Node {
int num, wher;
} G_1[N], G_2[N];
int n;
int Map1[N], Map2[N], imp[N], work[N];
int Tree[N];
long long Answer; #define gc getchar() inline int read() {
int x = ; char c = gc;
while(c < '' || c > '') c = gc;
while(c >= '' && c <= '') x = x * + c - '', c = gc;
return x;
} inline bool cmp(Node a, Node b) {return a.num < b.num;}
inline int Lowbit(int x) {return x & (-x);}
inline int Ask(int x) {int ret = ; while(x) {ret += Tree[x]; x -= Lowbit(x);} return ret;}
void Add(int x) {while(x <= n) {Tree[x] ++; x += Lowbit(x);}} int main () {
n = read();
for(int i = ; i <= n; i ++) G_1[i].num = read(), G_1[i].wher = i;
for(int i = ; i <= n; i ++) G_2[i].num = read(), G_2[i].wher = i;
sort(G_2 + , G_2 + n + , cmp);
sort(G_1 + , G_1 + n + , cmp);
for(int i = ; i <= n; i ++) Map1[G_1[i].wher] = i;
for(int i = ; i <= n; i ++) Map2[G_2[i].wher] = i;
for(int i = ; i <= n; i ++) imp[Map2[i]] = i;
for(int i = ; i <= n; i ++) work[i] = imp[Map1[i]];
for(int i = ; i <= n; i ++) {
int A = Ask(work[i]);
Add(work[i]);
Answer += (i - - A);
while(Answer >= Mod) Answer -= Mod;
}
cout << Answer;
return ;
}
[Luogu] 火柴排队的更多相关文章
- luogu P1966 火柴排队 (逆序对)
luogu P1966 火柴排队 题目链接:https://www.luogu.org/problemnew/show/P1966 显然贪心的想,排名一样的数相减是最优的. 证明也很简单. 此处就不证 ...
- [Luogu 1966] noip13 火柴排队
[Luogu 1966] noip13 火柴排队 Problem 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之 ...
- luoguP1966 火柴排队(NOIP2013)(归并排序)
luogu P1966 火柴排队 题目 #include<iostream> #include<cstdlib> #include<cstdio> #include ...
- Codevs 3286 火柴排队 2013年NOIP全国联赛提高组 树状数组,逆序对
题目:http://codevs.cn/problem/3286/ 3286 火柴排队 2013年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : ...
- 洛谷P1966 【火柴排队】
题解 P1966 [火柴排队] 说明: 在数学中有个公式: (a1-b1)^2+(a2-b2)^2<(a2-b1)^2+(a1-b2)^2 (你可以自己试着证一下) 两列火柴对应的两根火柴在各列 ...
- [树状数组+逆序对][NOIP2013]火柴排队
火柴排队 题目描述 涵涵有两盒火柴,每盒装有n根火柴,每根火柴都有一个高度.现在将每盒中的火柴各自排成一列,同一列火柴的高度互不相同,两列火柴之间的距离定义为:∑ (ai-bi)2,i=1,2,3,. ...
- 【刷题】洛谷 P1966 火柴排队
题目描述 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相同, 两列火柴之间的距离定义为: ∑(ai-bi)^2 其中 ai 表示 ...
- [NOIP2013提高&洛谷P1966]火柴排队 题解(树状数组求逆序对)
[NOIP2013提高&洛谷P1966]火柴排队 Description 涵涵有两盒火柴,每盒装有 n 根火柴,每根火柴都有一个高度. 现在将每盒中的火柴各自排成一列, 同一列火柴的高度互不相 ...
- LOJ2609. NOIP2013 火柴排队 【树状数组】
LOJ2609. NOIP2013 火柴排队 LINK 题目大意: 给你两个数列,定义权值∑i=1(ai−bi)^2 问最少的操作次数,最小化权值 首先需要发现几个性质 最小权值满足任意i,j不存在a ...
随机推荐
- ~ android与ios的区别
Oracle与Mysql的区别 项目类别 android ios 应用上 可以使用常用的android模拟器,来模拟各种android设备 只能直接使用iphone或ipad进行测试 开发语言 基于L ...
- 【思维】ABC
题目描述 You are given a string s consisting of A, B and C.Snuke wants to perform the following operatio ...
- Algorithm negotiation failed
#用pycharm工具ssh client 报 algorithm negotiation failed#导致原因:是ssh升级后,为了安全,默认不再采用原来一些加密算法,我们手工添加进去即可#目前出 ...
- MySQL 索引的优化
一.MySQL如何使用索引(index) 1.1 索引概述 索引用于快速查找具有特定列值的行. 如果不使用索引,MySQL必须从表的第一行开始,然后扫描整个表来寻找符合条件的行.这种情况下,表越大,扫 ...
- mysql replace substring 字符串截取处理
SELECT a1,a2,replace(a2, "豫ICP备16006180号-", "") a22,a3,a4,a5 FROM `aaab` order b ...
- springboot内置tomcat配置虚拟路径
在Springboot中默认的静态资源路径有:classpath:/METAINF/resources/,classpath:/resources/,classpath:/static/,classp ...
- iot平台异构对接文档
iot平台异构对接文档 准备工作 平台提供的XAgent开发指南.pdf demo程序xagent-ptp-demo 平台上添加产品得到产品id和key 部署时需要插件的基础程序<xlink-x ...
- CSS用户界面样式之cursor/outline/resize
1. 鼠标样式cursor 检测鼠标指针在对象上移动的鼠标指针采用何种系统预定于的光标形状 常用属性: default 小白 hands小手 /pointer move移动 text文本 2. 轮廓 ...
- 【Day4】1.JsonPath使用案例
import json python_data = [ { 'username': 'normal', 'vip': True, }, { 'username':None, 'vip':False } ...
- openresty获取nginx中的变量
在OpenResty中如何引用这些变量呢? 规则很简单, 如$remote_addr, 在OpenResty里面使用就是ngx.var.remote_adddr.