[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 ...
随机推荐
- phpstudy的设置目录列表显示403找不到
小白视角具体步骤如下 在phpstudy的->其它选项菜单->phpstudy设置->允许目录列表 phpstudy->其他选项菜单->站点域名管理(新增并保存) 然后会 ...
- Python初学者常见错误详解
Python初学者常见错误详解 0.忘记写冒号 在 if.elif.else.for.while.class.def 语句后面忘记添加 “:” if spam == 42 print('Hello ...
- python学习-10 运算符1
1.加+,减-,乘*,除/ 例如: a = 1 b = 2 c = a + b print(c) 运算结果: 3 Process finished with exit code 0 a = 1 b = ...
- fiddler笔记:统计选项卡(Statistics)
Request Count 选中的Session数. Bytes sent Http请求头和请求体中向外发送的字节总数. Bytes received HTTP请求头和请求体中接收到的所有字节数. R ...
- Python--类的调用
类的调用 实例化 class Luffy: school = 'luffy' def __init__(self,name,age): self.Name = name self.Age = age ...
- python 基础(十五)--time和datatime模块
time模块 time.time():返回当前时间,时间戳 time.clock():返回cpu时间(?),当有time.sleep()时,time.clock()不计时 time.localtime ...
- 【组成原理】BYTE ME!
题目描述 Parity is an important concept in data transmission. Because the process is not error proof, p ...
- Scala学习十四——模式匹配和样例类
一.本章要点 match表达式是更好的switch,不会有意外调入下一个分支 如果没有模式能够匹配,会抛出MatchError,可以用case _模式避免 模式可以包含一个随意定义的条件,称做守卫 你 ...
- MQTT协议探究(三)
1 回顾与本次目标 1.1 回顾 主题通配符 主题语义和用法 WireShark进行抓包分析了报文 报文分析: SUBSCRIBE--订阅主题 SUBACK--订阅确认 UNNSUBSCRIBE--取 ...
- 事件处理程序EventUtil
/**********事件处理程序***********EventUtil.js*浏览器兼容,<高三>13章 P354*2014-12-8************************* ...