Time Limit Exceeded 求逆序对数。
/**
题目:Time Limit Exceeded
链接:https://oj.ejq.me/problem/28
题意:求逆序对数。
思路:树状数组求逆序对数。维护前面有多少个<=当前数的数的个数。
*/
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
const int maxn = 1e6+;
const int mod = 1e9+;
ll c[maxn];
int flag[maxn];
int n;
int a[maxn];
int lowbit(int t)
{
return t&(-t);
}
ll getSum(int pos)
{
ll sum = ;
while(pos>){
sum += c[pos];
pos -= lowbit(pos);
}
return sum;
}
void update(int pos,int add)
{
while(pos<=n){
c[pos]+=add;
pos += lowbit(pos);
}
}
int main()
{
while(scanf("%d",&n)==)
{
memset(flag, , sizeof flag);
for(int i = ; i <= n; i++){
scanf("%d",&a[i]);
flag[a[i]] = ;
}
int index = ;///树状数组从1开始。
for(int i = ; i <= ; i++){
if(flag[i]){
flag[i] = index++;
}
}
memset(c, , sizeof c);
ll ans = ;
for(int i = ; i <= n; i++){
ans += i--getSum(flag[a[i]]);
update(flag[a[i]],);
}
printf("%lld\n",ans);
}
return ;
}
Time Limit Exceeded 求逆序对数。的更多相关文章
- Ultra-QuickSort(树状数组求逆序对数)
Ultra-QuickSort 题目链接:http://poj.org/problem?id=2299 Time Limit: 7000MS Memory Limit: 65536K Total ...
- 怎样求逆序对数(Inverse Number)?
#返回上一级 @Author: 张海拔 @Update: 2014-01-14 @Link: http://www.cnblogs.com/zhanghaiba/p/3520089.html /* * ...
- 用树状数组求逆序对数(poj2299)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 46995 Accepted: 17168 ...
- hdu 4911 Inversion(归并排序求逆序对数)2014多校训练第5场
Inversion Time Limit: 20 ...
- hdu 6318 Swaps and Inversions (线段树求逆序对数)
Swaps and Inversions Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Ultra-QuickSort(归并排序求逆序对数)
Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34283 Accepted: 12295 Description In ...
- POJ-2299 Ultra-QuickSort(用树状数组求逆序对数)
题目链接 ac代码 #include<iostream> #include<cstdio> #include<cstring> #include<algori ...
- POJ 2299 Ultra-QuickSort (求序列的逆序对数)
题意:废话了一大堆就是要你去求一个序列冒泡排序所需的交换的次数. 思路:实际上是要你去求一个序列的逆序队数 看案例: 9 1 0 5 4 9后面比它小的的数有4个 1后面有1个 0后面没有 5后面1个 ...
- Counter Strike HDU 2443 逆序对数
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2443 这个题目尝试了很多种方法都过不去,上网查了一下网友们的的思路,竟然和逆序对数有关系!! 题目大意: ...
随机推荐
- Mybatis添加用户&&Mybatis添加用户返回ID
(1)添加用户(添加User记录) <!--插入用户 --> <insert id="insertUser" parameterType="com.so ...
- Modernizr使用指南(转)
HTML5, CSS3以及相关技术(例如canvas和web sockets)带来了非常有用的特性,可以让我们的web程序提升一个新的level.这些新技术允许我们只用HTML,CSS和JavaScr ...
- Android之Activity 生命周期
作用:用户界面的组件,主要用于和用户进行交互.可以理解为手机屏幕的一屏. 生命周期: Resume:“继续”的意思. 由此可见, Activity有四种基本状态: 1) Running:位于屏幕最前端 ...
- Spring.NET的IoC容器(The IoC container)——简介(Introduction)
简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...
- /etc/sudoer文件配置简析
参考: http://blog.chinaunix.net/uid-26642180-id-3962245.html # User privilege specification root AL ...
- k8s debug记录之kubelet user.slice container monitor failure
在kubernetes中,如果使用其自带的单机启动脚本./hack/local-up-cluster.sh来启动一个本地集群的话,会在kubelet的日志中观察到类似以下内容的日志: Failed t ...
- Netty4 initAndRegister 解析
我们从框架的应用层面来分析,NioEventLoopGroup在netty中的使用. 这是我们需要配置的地方. 紧接着我们进入netty的运行中.ServerBootstrap.bind(PORT); ...
- php中一些函数的用法
addslashes() 定义和用法 addslashes() 函数返回在预定义字符之前添加反斜杠的字符串. 预定义字符是: 单引号(') 双引号(") 反斜杠(\) NULL 提示:该函数 ...
- rap 实现分页效果
1.官网 http://rapapi.org/org/index.do 2.账号(1) react_native_developer 3.结构 4.查看接口 5.访问接口 6.数据格式
- PHP怎么实现站点保存快捷方式
PHP怎么实现站点保存快捷方式 <?php $Shortcut = "[InternetShortcut] URL=http://blog.csdn.net/phpfenghuo/ I ...