HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化
题目,要求找出有多少对这样的东西,四个数,并且满足num[a]<num[b] &&num[c]>num[d]
要做这题,首先要懂得用树状数组,我设,下面的小于和大于都是严格的小于和大于
dpL_min[i]:表示在第i个数往左,(不包括第i个),有多少个数是少于num[i]的
dpL_max[i]:表示在第i个数往左,(不包括第i个),有多少个数是大于num[i]的
dpR_min[i]:表示在第i个数往右,(不包括第i个),有多少个数是小于num[i]的
dpR_max[i]:表示在第i个数往右,(不包括第i个),有多少个数是大于num[i]的
首先我们能预处理出所有的sumab对数,表示在1--n中有多少对这样的ab对。
sumcd同理。然后默认的ans=sumab*sumcd了
但是有重复的呀。有四种情况是重复的,就是a==d || a==c || b==c || a==c
那么,我们枚举每一个i,表示当前是a==d=num[i],就是把a和d现在相同,且数字是num[i],那么要减去的值就是dpR_max[i]*dpL_max[i]; dpR_max[i]表示有多少个数能和num[a]组合,变成num[a]<num[b]的对数。同理dpL_max[i]
再来一个例子吧.。假如现在是a==c=num[i],那么ans -= dpR_max[i] * dpR_min[i]; dpR_max[i] 表示有多少个数能和num[a]组合,变成num[a]<num[b]的对数。dpR_min[i]表示有多少个数能和num[c]结合,变成num[c]>num[d]这样的对数。
这里和网上说的枚举i作为d值是不同的哦,网上的解释我感觉上是说不通的。反正我是想不明白。我这里的枚举每个i,作为他们相同的数子。
有没可能是a==c && b==d呢?可能的,矛盾了。
这里要注意的还有数字是严格大于,在离散的时候注意一下就可以了
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
int n;
const int maxn = + ;
struct data
{
int val,pos;
}book[maxn];
int a[maxn];
int c[maxn];//树状数组
int lowbit (int x)//得到x二进制末尾0的个数的2次方 2^num
{
return x&(-x);
}
void add (int pos,int val)//在第pos位加上val这个值
{
while (pos<=n) //n是元素的个数
{
c[pos] += val;
pos += lowbit(pos);
}
return ;
}
int get_sum (int pos) //求解:1--pos的总和
{
int ans = ;
while (pos)
{
ans += c[pos];
pos -= lowbit(pos);
}
return ans;
}
bool cmp (struct data a,struct data b)
{
return a.val < b.val;
}
int dpL_min[maxn];
int dpL_max[maxn];
int dpR_min[maxn];
int dpR_max[maxn];
void init ()
{
memset(c,,sizeof c);
memset(dpR_max,,sizeof dpR_max);
memset(dpR_min,,sizeof dpR_min);
memset(dpL_max,,sizeof dpL_max);
memset(dpL_min,,sizeof dpL_min);
}
void work ()
{
init();
for (int i=;i<=n;++i)
{
scanf("%d",&book[i].val);
book[i].pos = i;
}
sort(book+,book++n,cmp);
for (int i=;i<=n;++i)
{
if (i>= && book[i].val == book[i-].val) a[book[i].pos] = a[book[i-].pos];
else a[book[i].pos]=i; //从小到大离散
}
for (int i=;i<=n;++i)
{
dpL_min[i] = get_sum(a[i]-);
dpL_max[i] = get_sum(n)-get_sum(a[i]);
add(a[i],);
}
memset(c,,sizeof c);
for (int i=n;i>=;--i)
{
dpR_min[i] = get_sum(a[i]-);
dpR_max[i] = get_sum(n) - get_sum(a[i]);
add(a[i],);
} LL sumab = ;
LL sumcd = ;
for (int i=;i<=n;++i) sumab += dpL_min[i];
for (int i=;i<=n;++i) sumcd += dpL_max[i];
LL ans = sumab * sumcd; for (int i=;i<=n;++i)
{
ans -= dpL_max[i] * dpR_max[i]; //a==d
ans -= dpL_max[i] * dpL_min[i]; // b==d
ans -= dpR_max[i] * dpR_min[i]; // a==c;
ans -= dpL_min[i] * dpR_min[i]; //c==b
}
printf ("%I64d\n",ans);
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(scanf("%d",&n)!=EOF && n) work();
return ;
}
HDU 5792 L - World is Exploding 。容斥原理 + 树状数组 + 离散化的更多相关文章
- HDU 5792:World is Exploding(树状数组求逆序对)
http://acm.hdu.edu.cn/showproblem.php?pid=5792 World is Exploding Problem Description Given a sequ ...
- HDU 5792 World is Exploding(树状数组+离散化)
http://acm.split.hdu.edu.cn/showproblem.php?pid=5792 题意: 思路: lmin[i]:表示左边比第i个数小的个数. lmax[i]:表示左边比第i个 ...
- HDU 2227 Find the nondecreasing subsequences (DP+树状数组+离散化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2227 Find the nondecreasing subsequences ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- HDU 5792 World is Exploding (树状数组)
World is Exploding 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5792 Description Given a sequence ...
- HDU 4777 Rabbit Kingdom --容斥原理+树状数组
题意: 给一个数的序列,询问一些区间,问区间内与区间其他所有的数都互质的数有多少个. 解法: 直接搞有点难, 所谓正难则反,我们求区间内与其他随便某个数不互质的数有多少个,然后区间长度减去它就是答案了 ...
随机推荐
- Percona Xtrabackup 备份MySQL 实例(转)
老规矩,开场白,刚开始用mysqldump,备份100G+的数据库,再加上服务器繁忙,备份速度像蜗牛似的,于是寻找更高效的备份方法.网上都说用xtrabackup比较适合备份大的数据库,而且备份效率也 ...
- 【转】 Pro Android学习笔记(六一):Preferences(5):组织Preference
目录(?)[-] PreferenceCategory Child Preference PreferenceCategory 如果有多个preference,我们希望能在他们组织在一起.有两种方式, ...
- java代码Math.sqrt
总结:这个判断小数的题目,当时全只2有一个人想出了结果.老师很开心.我很桑心~~~~ 我没想到要取膜,我只想到了除以等于0就够了.至于中间的“取膜”,我没凑齐来,还是不够灵活 package com. ...
- Ruby中的include
Ruby中的include语句应注意以下两个问题: 1.include与文件无关.C语言中,#include预处理指令在编译期将一个文件的内容插入到另一个文件中.Ruby语句只是简单地产生一个指向指定 ...
- WPF 界面提示加载出错
当WPF界面代码中包含多个 VisualStateManager.VisualStateGroups 时会导致界面无法正常显示,把这些代码注释掉就可以展示出界面.
- JAVA基础知识总结1(概述)
JAVA概述: 1991 年Sun公司的James Gosling等人开始开发名称为 Oak 的语言,希望用于控制嵌入在有线电视交换盒.PDA等的微处理器. 1994年将Oak语言更名为Java. J ...
- groupadd添加新组
一.groupadd命令用于将新组加入系统. 格式groupadd [-g gid] [-o]] [-r] [-f] groupname 主要参数 -g gid:指定组ID号. -o:允许组ID号,不 ...
- C++中const型数据的小结
由于与对象又管的const型数据种类较多,形式又有些相似,往往难记,容易混淆,因此总结一下相关用法,具体用法可查看下方链接 C++中对象的常引用 C++中指向对象的常指针和指向常对象的指针 C++中的 ...
- SpringBoot02 Controller的使用、数据库操作、事物管理、修改banner
1 Controller的使用 特点:编程技巧和SpringMVC几乎完全一样 注意:@RestController = @Controller + @ResponseBody 注意:读取路径参数和请 ...
- 292C Beautiful IP Addresses
传送门 题目 The problem uses a simplified TCP/IP address model, please read the statement carefully. An I ...